fix: address markdown paste review feedback

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Devin FoleyandPaperclip committed 2026-04-04 10:36:03 -07:00
1 parent 25cc910044
commit 8306e30506
3 files changed
+18 -1

No files matched your search

@@ -51,4 +51,9 @@ describe("normalizeMarkdown", () => {
const expected = "base\n nested\nback\n deep";
expect(normalizeMarkdown(input)).toBe(expected);
});
it("leaves mixed tab and space indentation unchanged", () => {
const input = "\t# Title\n body\n\t- item";
expect(normalizeMarkdown(input)).toBe(input);
});
});
+8 -1
View File
@@ -12,11 +12,18 @@ export function normalizeMarkdown(text: string): string {
// Find minimum indentation across non-empty lines
let minIndent = Infinity;
let indentStyle: "spaces" | "tabs" | null = null;
for (const line of lines) {
if (line.trim() === "") continue;
const match = line.match(/^(\s+)/);
if (match) {
minIndent = Math.min(minIndent, match[1].length);
const leadingWhitespace = match[1];
const currentStyle = leadingWhitespace.includes("\t") ? "tabs" : "spaces";
if (indentStyle && indentStyle !== currentStyle) {
return result;
}
indentStyle = currentStyle;
minIndent = Math.min(minIndent, leadingWhitespace.length);
} else {
minIndent = 0;
break;
+5
View File
@@ -1,5 +1,6 @@
import { createRootEditorSubscription$, realmPlugin } from "@mdxeditor/editor";
import { COMMAND_PRIORITY_CRITICAL, PASTE_COMMAND } from "lexical";
import { looksLikeMarkdownPaste } from "./markdownPaste";
import { normalizeMarkdown } from "./normalize-markdown";
/**
@@ -32,6 +33,10 @@ export const pasteNormalizationPlugin = realmPlugin({
// let the default paste handler deal with rich content as-is.
if (clipboardData.getData("text/html")) return false;
// Markdown-looking pastes are handled by MarkdownEditor.tsx via
// insertMarkdown(), so the plugin only owns the plain-text fallback.
if (looksLikeMarkdownPaste(text)) return false;
const cleaned = normalizeMarkdown(text);
if (cleaned === text) return false;