AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(editor): render Markdown pasted as plain text in rich-text mode (#693)
Dariusz Korytocc0da25a9367
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsxindex 48776849..78dfd942 100644--- a/apps/web/src/components/EditorPane.tsx+++ b/apps/web/src/components/EditorPane.tsx@@ -220,6 +220,7 @@ import {} from "@/lib/editor-external-link";import { createFileBatchQueue, processFileUploadBatch } from "@/lib/file-batch";import { resourceToMarkdown } from "@/lib/markdown-resource-paste";+import { pasteBlockMarkdown } from "@/lib/markdown-text-paste";import { MEMO_ID_REMAPPED_EVENT, MEMO_SYNC_ACKNOWLEDGED_EVENT } from "@/lib/sync-events";import { useStandaloneMobileEditor } from "@/hooks/useStandaloneMobileEditor";import { statusSettleMotion } from "@/lib/motion";@@ -1402,11 +1403,11 @@ const RichEditorPane = ({return true;},transformPastedHTML: (html) => wrapDetailsContentHtml(html),- handlePaste: (_view, event) => {+ handlePaste: (view, event) => {const files = getResourceFilesFromDataTransfer(event.clipboardData);if (files.length === 0) {- return false;+ return pasteBlockMarkdown(view, event.clipboardData);}event.preventDefault();diff --git a/apps/web/src/lib/markdown-text-paste.test.mjs b/apps/web/src/lib/markdown-text-paste.test.mjsnew file mode 100644index 00000000..240500a7--- /dev/null+++ b/apps/web/src/lib/markdown-text-paste.test.mjs@@ -0,0 +1,177 @@+import { describe, expect, test } from "bun:test";+import { Editor } from "@tiptap/core";+import { TextSelection } from "@tiptap/pm/state";+import { createEdgeEverDocumentExtensions } from "@edgeever/shared";+import { createEdgeEverMathematics } from "@edgeever/shared/mathematics";+import { ensureTestWindowDom } from "./restore-test-global.mjs";+import { isBlockMarkdownText, pasteBlockMarkdown } from "./markdown-text-paste.ts";++const createEditor = (content = { type: "doc", content: [{ type: "paragraph" }] }) => {+ ensureTestWindowDom();+ return new Editor({+ extensions: createEdgeEverDocumentExtensions({ mathematics: createEdgeEverMathematics() }),+ content,+ });+};++const clipboard = (entries) => ({+ types: Object.keys(entries),+ getData: (type) => entries[type] ?? "",+});++// The unmounted Tiptap view throws on fields it does not have, such as+// `input`, so tests pass the state, dispatch and Shift state ProseMirror keeps.+const viewOf = (editor, shiftKey = false) => ({+ get state() { return editor.state; },+ dispatch: (transaction) => editor.view.dispatch(transaction),+ input: { shiftKey },+});+const paste = (editor, entries, shiftKey) => pasteBlockMarkdown(viewOf(editor, shiftKey), clipboard(entries));++// Excerpt of the sample attached to issue #660.+const ISSUE_SAMPLE = `# 一级标题++## 2. 行内格式++普通文字中包含 **粗体**、*斜体*、~~删除线~~ 和 \`行内代码\`。++- 第一项+- 第二项,包含 **粗体**+ - 第二层第一项++1. 打开笔记。+2. 粘贴 Markdown 文本。++> 引用++\`\`\`js+const count = 1;+\`\`\`++| 列 A | 列 B |+| --- | --- |+| 1 | 2 |+`;++describe("block Markdown detection", () => {+ test("recognises headings, lists, quotes, fences, tables and rules across lines", () => {+ for (const text of [+ "# Title",+ "intro\n- item",+ "1. one\n2. two",+ "> quote\nmore",+ "```\ncode\n```",+ "| a | b |\n| --- | --- |",+ "above\n---\nbelow",+ "Name | Value\n--- | ---\na | b",+ "Name | Value\n:--- | ---:",+ ]) {+ expect(isBlockMarkdownText(text)).toBe(true);+ }+ });++ test("leaves ordinary text and single inline lines to the existing paste path", () => {+ for (const text of ["", "plain sentence", "first line\nsecond line", "**bold** only", "#hashtag\nnext", "- single item",+ "use a | b to pipe\nthen read the output", "A | B\nnot a delimiter"]) {+ expect(isBlockMarkdownText(text)).toBe(false);+ }+ });+});++describe("pasting plain-text Markdown in rich text", () => {+ test("renders the issue sample as structured content", () => {+ const editor = createEditor();+ expect(paste(editor, { "text/plain": ISSUE_SAMPLE })).toBe(true);+ const types = editor.getJSON().content.map((node) => node.type);+ expect(types[0]).toBe("heading");+ expect(types).toEqual(expect.arrayContaining(["heading", "bulletList", "orderedList", "blockquote", "codeBlock", "table"]));+ const paragraph = editor.getJSON().content.find((node) => node.type === "paragraph" && node.content);+ const marks = paragraph.content.flatMap((child) => child.marks?.map((mark) => mark.type) ?? []);+ expect(marks).toEqual(expect.arrayContaining(["bold", "italic", "strike", "code"]));+ expect(editor.getText()).not.toContain("## ");+ editor.destroy();+ });++ test("inserts at the cursor without replacing the surrounding note", () => {+ const editor = createEditor({ type: "doc", content: [+ { type: "paragraph", content: [{ type: "text", text: "Before" }] },+ { type: "paragraph", content: [{ type: "text", text: "After" }] },+ ] });+ editor.view.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, 7)));+ expect(paste(editor, { "text/plain": "## Pasted\n- item" })).toBe(true);+ const json = editor.getJSON().content;+ expect(json[0]).toEqual({ type: "paragraph", content: [{ type: "text", text: "Before" }] });+ expect(json.map((node) => node.type)).toEqual(["paragraph", "heading", "bulletList", "paragraph"]);+ expect(json.at(-1)).toEqual({ type: "paragraph", content: [{ type: "text", text: "After" }] });+ editor.destroy();+ });++ test("joins leading and trailing paragraphs with the text around the cursor", () => {+ const editor = createEditor({ type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text: "Ab" }] }] });+ editor.view.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, 2)));+ expect(paste(editor, { "text/plain": "one\n- item\n\ntwo" })).toBe(true);+ expect(editor.getJSON().content).toEqual([+ { type: "paragraph", content: [{ type: "text", text: "Aone" }] },+ { type: "bulletList", content: [{ type: "listItem", content: [{ type: "paragraph", content: [{ type: "text", text: "item" }] }] }] },+ { type: "paragraph", content: [{ type: "text", text: "twob" }] },+ ]);+ editor.destroy();+ });++ test("keeps rich HTML, code from VS Code and pastes inside code blocks on the default path", () => {+ const editor = createEditor();+ expect(paste(editor, { "text/plain": "# Title\n- item", "text/html": "<h1>Title</h1>" })).toBe(false);+ expect(paste(editor, {+ "text/plain": "/**\n * Adds numbers.\n */",+ "text/html": "<div>…</div>",+ "vscode-editor-data": JSON.stringify({ mode: "typescript" }),+ })).toBe(false);+ expect(paste(editor, { "text/plain": "plain\ntext" })).toBe(false);++ const code = createEditor({ type: "doc", content: [{ type: "codeBlock", content: [{ type: "text", text: "x" }] }] });+ code.view.dispatch(code.state.tr.setSelection(TextSelection.create(code.state.doc, 2)));+ expect(paste(code, { "text/plain": "# Title\n- item" })).toBe(false);+ editor.destroy();+ code.destroy();+ });++ test("converts Markdown copied from VS Code", () => {+ const editor = createEditor();+ expect(paste(editor, {+ "text/plain": "# Notes\n- one\n- two",+ "text/html": "<div style=\"font-family: Menlo\">…</div>",+ "vscode-editor-data": JSON.stringify({ mode: "markdown" }),+ })).toBe(true);+ expect(editor.getJSON().content.map((node) => node.type)).toEqual(["heading", "bulletList"]);+ editor.destroy();+ });++ test("keeps escaped Markdown literal instead of running paste rules again", () => {+ const editor = createEditor();+ expect(paste(editor, { "text/plain": "# Title\n\n\\*not italic\\*\n\n\\`not code\\`" })).toBe(true);+ const json = editor.getJSON().content;+ expect(json.map((node) => node.type)).toEqual(["heading", "paragraph", "paragraph"]);+ expect(json[1]).toEqual({ type: "paragraph", content: [{ type: "text", text: "*not italic*" }] });+ expect(json[2]).toEqual({ type: "paragraph", content: [{ type: "text", text: "`not code`" }] });+ editor.destroy();+ });++ test("leaves paste as plain text to the default path", () => {+ const editor = createEditor();+ expect(paste(editor, { "text/plain": "# title\n- item" }, true)).toBe(false);+ expect(paste(editor, {+ "text/plain": "# title\n- item",+ "text/html": "<div>…</div>",+ "vscode-editor-data": JSON.stringify({ mode: "markdown" }),+ }, true)).toBe(false);+ expect(paste(editor, { "text/plain": "# title\n- item" })).toBe(true);+ editor.destroy();+ });++ test("renders a table written without outer pipes", () => {+ const editor = createEditor();+ expect(paste(editor, { "text/plain": "Name | Value\n--- | ---\na | b" })).toBe(true);+ expect(editor.getJSON().content.map((node) => node.type)).toContain("table");+ editor.destroy();+ });+});diff --git a/apps/web/src/lib/markdown-text-paste.ts b/apps/web/src/lib/markdown-text-paste.tsnew file mode 100644index 00000000..21843a6d--- /dev/null+++ b/apps/web/src/lib/markdown-text-paste.ts@@ -0,0 +1,86 @@+import { markdownToDoc } from "@edgeever/shared";+import { Node as ProseMirrorNode, Slice } from "@tiptap/pm/model";+import type { EditorState, Transaction } from "@tiptap/pm/state";++const HEADING_LINE = /^#{1,6}\s+\S/;+const BLOCK_MARKDOWN_LINES = [+ HEADING_LINE,+ /^\s*(?:```|~~~)/,+ /^\s*(?:[-*+]|\d{1,9}[.)])\s+\S/,+ /^\s*>/,+ /^\s*\|.*\|\s*$/,+ /^\s*(?:-{3,}|\*{3,}|_{3,})\s*$/,+];+// GFM table delimiter row, with or without outer pipes: `--- | :---:`.+const TABLE_DELIMITER_LINE = /^\s*\|?\s*:?-+:?\s*(?:\|\s*:?-+:?\s*)+\|?\s*$/;++/** A delimiter row directly under a header row that contains a pipe. */+const hasTableWithoutOuterPipes = (lines: string[]) =>+ lines.some((line, index) => index > 0 && TABLE_DELIMITER_LINE.test(line) && lines[index - 1]!.includes("|"));++/**+ * Plain text whose lines carry Markdown block structure. A single line keeps+ * the existing inline paste rules unless it is a heading.+ */+export const isBlockMarkdownText = (text: string) => {+ const allLines = text.replace(/\r\n?/g, "\n").split("\n");+ if (hasTableWithoutOuterPipes(allLines)) return true;+ const lines = allLines.filter((line) => line.trim());+ if (lines.length === 0) return false;+ if (lines.length === 1) return HEADING_LINE.test(lines[0]!);+ return lines.some((line) => BLOCK_MARKDOWN_LINES.some((pattern) => pattern.test(line)));+};++const VSCODE_TEXT_MODES = new Set(["markdown", "plaintext"]);++/**+ * VS Code puts styled HTML next to the source text; that HTML carries no Markdown+ * structure. Copies from other languages stay with the code block paste handler.+ */+const isPlainMarkdownSource = (data: DataTransfer) => {+ const vscode = data.getData("vscode-editor-data");+ if (vscode) {+ try {+ const mode = (JSON.parse(vscode) as { mode?: unknown }).mode;+ return typeof mode !== "string" || VSCODE_TEXT_MODES.has(mode);+ } catch {+ return false;+ }+ }+ return !data.getData("text/html");+};++/**+ * Pastes Markdown copied from a plain-text source as rich content, matching what+ * pasting the same text in source mode and switching back produces.+ */+export const pasteBlockMarkdown = (+ view: { state: EditorState; dispatch: (transaction: Transaction) => void },+ data: DataTransfer | null,+) => {+ // ProseMirror tracks Shift for Cmd/Ctrl+Shift+V, which pastes the literal text.+ if ((view as { input?: { shiftKey?: boolean } }).input?.shiftKey) return false;+ if (!data || !isPlainMarkdownSource(data)) return false;+ const { $from } = view.state.selection;+ if ($from.parent.type.spec.code) return false;+ const text = data.getData("text/plain");+ if (!isBlockMarkdownText(text)) return false;++ let doc: ProseMirrorNode;+ try {+ doc = ProseMirrorNode.fromJSON(view.state.schema, markdownToDoc(text));+ } catch {+ return false;+ }+ // Only leading or trailing paragraphs join the surrounding text; headings,+ // lists and other blocks keep their type.+ const isParagraph = (node: ProseMirrorNode | null) => node?.type.name === "paragraph";+ const slice = new Slice(doc.content, isParagraph(doc.firstChild) ? 1 : 0, isParagraph(doc.lastChild) ? 1 : 0);+ // No `uiEvent: "paste"`: Tiptap paste rules would parse the already parsed+ // text again and turn escaped `\*` or `` \` `` back into marks.+ view.dispatch(view.state.tr+ .replaceSelection(slice)+ .scrollIntoView()+ .setMeta("paste", true));+ return true;+};diff --git a/tests/e2e/markdown-text-paste.spec.ts b/tests/e2e/markdown-text-paste.spec.tsnew file mode 100644index 00000000..491743da--- /dev/null+++ b/tests/e2e/markdown-text-paste.spec.ts@@ -0,0 +1,165 @@+import { expect, test, type Locator, type Page } from "@playwright/test";++const createMemo = async (page: Page, title: string) => {+ const notebooksResponse = await page.request.get("/api/v1/notebooks");+ expect(notebooksResponse.ok()).toBe(true);+ const notebooks = await notebooksResponse.json() as { notebooks: Array<{ id: string }> };+ const notebookId = notebooks.notebooks[0]?.id;+ expect(notebookId).toBeTruthy();++ const createResponse = await page.request.post("/api/v1/memos", {+ data: { notebookId, title, contentMarkdown: "" },+ });+ expect(createResponse.status()).toBe(201);+ return (await createResponse.json() as { memo: { id: string } }).memo;+};++const deleteMemo = async (page: Page, memoId: string) => {+ await page.request.delete(`/api/v1/memos/${memoId}`);+ await page.request.delete(`/api/v1/memos/${memoId}?permanent=1`);+};++const dispatchPaste = async (editor: Locator, entries: Record<string, string>) => {+ await editor.evaluate((element, data) => {+ const clipboardData = new DataTransfer();+ for (const [type, value] of Object.entries(data)) clipboardData.setData(type, value);+ element.dispatchEvent(new ClipboardEvent("paste", { bubbles: true, cancelable: true, clipboardData }));+ }, entries);+};++const openMemo = async (page: Page, memo: { id: string }, title: string) => {+ await page.goto("/");+ await page.getByRole("button", { name: "全部笔记", exact: true }).click();+ await page.getByPlaceholder("搜索笔记").fill(title);+ await page.locator(`[data-memo-id="${memo.id}"]`).locator("button").first().click();+ const editor = page.locator(".ProseMirror[contenteditable='true']");+ await expect(editor).toBeEditable();+ await editor.click();+ return editor;+};++// Issue #660: Markdown copied from a plain-text source.+const MARKDOWN = [+ "# 一级标题",+ "",+ "普通文字中包含 **粗体**、*斜体*、~~删除线~~ 和 `行内代码`。",+ "",+ "- 第一项",+ "- 第二项",+ "",+ "1. 打开笔记。",+ "2. 粘贴 Markdown 文本。",+ "",+ "> 引用",+ "",+ "```js",+ "const count = 1;",+ "```",+ "",+ "| 列 A | 列 B |",+ "| --- | --- |",+ "| 1 | 2 |",+].join("\n");++test("renders Markdown pasted as plain text in the rich-text editor", async ({ page }) => {+ const title = `Markdown text paste ${Date.now()}`;+ const memo = await createMemo(page, title);++ try {+ const editor = await openMemo(page, memo, title);+ await dispatchPaste(editor, { "text/plain": MARKDOWN });++ await expect(editor.locator("h1")).toHaveText("一级标题");+ await expect(editor.locator("strong")).toHaveText("粗体");+ await expect(editor.locator("em")).toHaveText("斜体");+ await expect(editor.locator("s")).toHaveText("删除线");+ await expect(editor.locator("p code")).toHaveText("行内代码");+ await expect(editor.locator("ul > li")).toHaveCount(2);+ await expect(editor.locator("ol > li")).toHaveCount(2);+ await expect(editor.locator("blockquote")).toContainText("引用");+ const codeBlocks = editor.getByRole("button", { name: "复制代码" });+ await expect(codeBlocks).toHaveCount(1);+ await expect(editor).toContainText("const count = 1;");+ await expect(editor.locator("table td")).toHaveCount(2);+ await expect(editor).not.toContainText("# 一级标题");+ await expect(editor).not.toContainText("| --- |");++ // One undo step removes the whole paste.+ await page.keyboard.press("ControlOrMeta+z");+ await expect(editor.locator("h1")).toHaveCount(0);+ await expect(editor.locator("table")).toHaveCount(0);+ await page.keyboard.press("ControlOrMeta+Shift+z");+ await expect(editor.locator("h1")).toHaveText("一级标题");++ // A code copy from VS Code stays a code block rather than becoming a list.+ await editor.locator("h1").click();+ await page.keyboard.press("End");+ await page.keyboard.press("Enter");+ await dispatchPaste(editor, {+ "text/plain": "/**\n * Adds numbers.\n */",+ "text/html": "<div><span>/**</span></div>",+ "vscode-editor-data": JSON.stringify({ mode: "typescript" }),+ });+ await expect(codeBlocks).toHaveCount(2);+ await expect(editor.locator("ul > li")).toHaveCount(2);++ await expect.poll(async () => {+ const response = await page.request.get(`/api/v1/memos/${memo.id}`);+ const body = await response.json() as { memo: { contentMarkdown: string } };+ return body.memo.contentMarkdown;+ }, { timeout: 20_000 }).toContain("# 一级标题");+ } finally {+ await deleteMemo(page, memo.id);+ }+});++test("keeps escaped delimiters, paste as plain text and tables without outer pipes", async ({ page }) => {+ const title = `Markdown text paste details ${Date.now()}`;+ const memo = await createMemo(page, title);++ try {+ const editor = await openMemo(page, memo, title);++ // Escaped delimiters stay literal; paste rules do not parse the result again.+ await dispatchPaste(editor, { "text/plain": "# Escaped\n\n\\*not italic\\*\n\n\\`not code\\`" });+ await expect(editor.locator("h1")).toHaveText("Escaped");+ await expect(editor).toContainText("*not italic*");+ await expect(editor).toContainText("`not code`");+ await expect(editor.locator("em")).toHaveCount(0);+ await expect(editor.locator("p code")).toHaveCount(0);++ // A table written without outer pipes.+ await page.keyboard.press("ControlOrMeta+End");+ await dispatchPaste(editor, { "text/plain": "Name | Value\n--- | ---\na | b" });+ await expect(editor.locator("table td")).toHaveCount(2);+ await expect(editor).not.toContainText("--- | ---");++ // Cmd/Ctrl+Shift+V keeps the literal text, also for a VS Code Markdown copy.+ await page.keyboard.press("ControlOrMeta+End");+ await page.keyboard.down("Shift");+ await dispatchPaste(editor, { "text/plain": "# literal heading\n- literal item" });+ await dispatchPaste(editor, {+ "text/plain": "# vscode heading\n- vscode item",+ "text/html": "<div style=\"font-family: Menlo\"><span># vscode heading</span></div>",+ "vscode-editor-data": JSON.stringify({ mode: "markdown" }),+ });+ await page.keyboard.up("Shift");+ await expect(editor).toContainText("# literal heading");+ await expect(editor).toContainText("- literal item");+ await expect(editor).toContainText("# vscode heading");+ await expect(editor.locator("h1")).toHaveCount(1);+ await expect(editor.locator("ul > li")).toHaveCount(0);++ // The same VS Code copy without Shift is converted.+ await dispatchPaste(editor, {+ "text/plain": "# vscode converted\n- vscode item",+ "text/html": "<div style=\"font-family: Menlo\"><span># vscode converted</span></div>",+ "vscode-editor-data": JSON.stringify({ mode: "markdown" }),+ });+ await expect(editor.locator("h1")).toHaveCount(2);+ await expect(editor.locator("h1").nth(1)).toHaveText("vscode converted");+ await expect(editor.locator("ul > li")).toHaveCount(1);+ } finally {+ await deleteMemo(page, memo.id);+ }+});