From 639a1ca83d6a9d5a5a8f42106767eceaeccfc087 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 11:07:00 +0100 Subject: [PATCH 1/8] fix(core): the markdown parser adds no space after a hard break The markdown `a\` + newline + `b` gave the text "a\n b": a space was at the start of the line after the hard break. The same occurred for a hard break from two spaces and for a single newline in a paragraph. The markdown parser wrote a newline after each `
` tag. The HTML parser changes a newline in a text into a space, and it keeps the space after a `
` tag. The markdown parser now writes the `
` tag without a newline after it. Eight snapshots of the format tests had the space. They are now without it. --- .../core/src/api/parsers/markdown/markdownToHtml.ts | 10 ++++++---- .../__snapshots__/markdown/markdown/hardBreak.json | 2 +- .../markdown/blockquoteLazyContinuation.json | 4 ++-- .../__snapshots__/markdown/blockquoteMultiline.json | 4 ++-- .../__snapshots__/markdown/hardBreakBackslash.json | 2 +- .../__snapshots__/markdown/hardBreakMultiple.json | 4 ++-- .../__snapshots__/markdown/hardBreakTwoSpaces.json | 2 +- .../parse/__snapshots__/markdown/lineBreaks.json | 4 ++-- .../__snapshots__/markdown/paragraphLeadingIndent.json | 2 +- 9 files changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.ts index 961192df49..33f19e59b2 100644 --- a/packages/core/src/api/parsers/markdown/markdownToHtml.ts +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.ts @@ -54,9 +54,11 @@ function tryBackslashEscape( return null; } const next = text[i + 1]; - // Hard line break: backslash at end of line + // Hard line break: backslash at end of line. No newline follows the `
`: + // the HTML parser reads that newline as a space at the start of the next + // line. if (next === "\n") { - return { html: "
\n", end: i + 2 }; + return { html: "
", end: i + 2 }; } // Escapable characters if ("\\`*_{}[]()#+-.!~|<>".includes(next)) { @@ -228,7 +230,7 @@ function trySoftBreak( i: number, ): { html: string; end: number } | null { if (text[i] === "\n") { - return { html: "
\n", end: i + 1 }; + return { html: "
", end: i + 1 }; } return null; } @@ -331,7 +333,7 @@ function parseInline(text: string, allowLinks = true): string { text[i - 2] === " " ) { result = result.replace(/ +$/, ""); - result += "
\n"; + result += "
"; i++; continue; } diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreak.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreak.json index 5626739e88..dc18073a94 100644 --- a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreak.json +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreak.json @@ -5,7 +5,7 @@ { "styles": {}, "text": "Line 1 - Line 2", +Line 2", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/blockquoteLazyContinuation.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/blockquoteLazyContinuation.json index 146a54b49b..a9fb97754a 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/blockquoteLazyContinuation.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/blockquoteLazyContinuation.json @@ -5,8 +5,8 @@ { "styles": {}, "text": "This is a quote - that continues here - and here too", +that continues here +and here too", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/blockquoteMultiline.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/blockquoteMultiline.json index 669e44db40..2993f08418 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/blockquoteMultiline.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/blockquoteMultiline.json @@ -5,8 +5,8 @@ { "styles": {}, "text": "Line one - Line two - Line three", +Line two +Line three", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakBackslash.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakBackslash.json index e4ed206bcf..c6cfdcf8f0 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakBackslash.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakBackslash.json @@ -5,7 +5,7 @@ { "styles": {}, "text": "Line one - Line two", +Line two", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakMultiple.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakMultiple.json index 359a3b1cbc..dac5b2a4de 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakMultiple.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakMultiple.json @@ -5,8 +5,8 @@ { "styles": {}, "text": "Line one - Line two - Line three", +Line two +Line three", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakTwoSpaces.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakTwoSpaces.json index e4ed206bcf..c6cfdcf8f0 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakTwoSpaces.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/hardBreakTwoSpaces.json @@ -5,7 +5,7 @@ { "styles": {}, "text": "Line one - Line two", +Line two", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/lineBreaks.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/lineBreaks.json index 359a3b1cbc..dac5b2a4de 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/lineBreaks.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/lineBreaks.json @@ -5,8 +5,8 @@ { "styles": {}, "text": "Line one - Line two - Line three", +Line two +Line three", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/paragraphLeadingIndent.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/paragraphLeadingIndent.json index 613cf56ae2..e511c777ec 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/paragraphLeadingIndent.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/paragraphLeadingIndent.json @@ -5,7 +5,7 @@ { "styles": {}, "text": "aaa - bbb", +bbb", "type": "text", }, ], From c1cbce5b35c78745419fdfdb2ce266b040c55c4f Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 11:08:08 +0100 Subject: [PATCH 2/8] fix(core): the markdown parser removes the indent of a continuation line A paragraph line after the first can have any indent (CommonMark example 223). The markdown parser removed only 3 spaces of it. The rest went into the HTML after the `
` tag. Before the previous commit, the HTML parser changed the newline and this indent into one space. Without the newline, the full remaining indent (or a tab) shows at the start of the line. The markdown parser now removes all spaces and tabs at the start of each paragraph line after the first. The first line is not changed. --- .../api/parsers/markdown/markdownToHtml.ts | 7 +++++-- .../markdown/paragraphContinuationIndent.json | 21 +++++++++++++++++++ .../parse/parseTestInstances.ts | 9 ++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/paragraphContinuationIndent.json diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.ts index 33f19e59b2..876eeaaa12 100644 --- a/packages/core/src/api/parsers/markdown/markdownToHtml.ts +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.ts @@ -1129,14 +1129,17 @@ function tokenize(markdown: string): Token[] { paraLines.push(nextLine); i++; } - // CommonMark allows up to 3 leading spaces of indent on paragraph lines. + // CommonMark allows up to 3 leading spaces of indent on the first + // paragraph line, and any indent on the lines after it. Each of these + // lines comes after a line break, so its indent would show as whitespace + // at the start of the line. // Also strip trailing whitespace from the final line so a trailing // hard-break sequence (` \n` at end of paragraph) doesn't leak as // literal trailing spaces in the rendered output. tokens.push({ type: "paragraph", content: paraLines - .map((l) => l.replace(/^ {1,3}/, "")) + .map((l, index) => l.replace(index === 0 ? /^ {1,3}/ : /^[ \t]+/, "")) .join("\n") .replace(/[ \t]+$/, ""), }); diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/paragraphContinuationIndent.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/paragraphContinuationIndent.json new file mode 100644 index 0000000000..3b8024bf98 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/paragraphContinuationIndent.json @@ -0,0 +1,21 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "aaa +bbb +ccc", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index 33f4be5e4d..b74fc499cb 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -2238,4 +2238,13 @@ Some text after.`, }, executeTest: testParseMarkdown, }, + // A paragraph line after the first can have any indent; the indent is + // stripped (CommonMark ex. 223) + { + testCase: { + name: "paragraphContinuationIndent", + content: `aaa\n bbb\n\tccc`, + }, + executeTest: testParseMarkdown, + }, ]; From aebcf9d7fcec7abfd0ab6f9775c752ae9d919cbe Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 11:11:53 +0100 Subject: [PATCH 3/8] fix(core): the markdown parser reads \r\n and \r as line ends Markdown with Windows line ends (\r\n) did not parse correctly. A list item, a code fence and a hard break were not found: for example, `* Item\r\n` gave a paragraph with the text "* Item ". With \r alone as the line end, the full document became one paragraph. The parser split the text into lines at \n only, so each line ended with \r (or the text was one line). CommonMark gives \n, \r\n and \r as line endings. The parser now splits the text at these three line endings. --- .../parsers/markdown/markdownToHtml.test.ts | 37 +++++++++++++++++++ .../api/parsers/markdown/markdownToHtml.ts | 4 +- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 packages/core/src/api/parsers/markdown/markdownToHtml.test.ts diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts new file mode 100644 index 0000000000..46ad2cc658 --- /dev/null +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts @@ -0,0 +1,37 @@ +// @vitest-environment node +import { describe, expect } from "vite-plus/test"; +import { markdownToHtml } from "./markdownToHtml.js"; + +describe("markdownToHtml", () => { + // CommonMark: a line ending is "\n", "\r\n" or "\r". A file from Windows + // has "\r\n". + it.each([ + ["\\r\\n", "\r\n"], + ["\\r", "\r"], + ])("reads %s as a line end", (_, lineEnd) => { + const lines = [ + "# Heading", + "", + "* Bullet", + " * Nested bullet", + "", + "1. Numbered", + "", + "- [x] Checked", + "", + "> Quote", + "", + "Line one\\", + "line two", + "", + "```js", + "const a = 1;", + "```", + "", + ]; + + expect(markdownToHtml(lines.join(lineEnd))).toBe( + markdownToHtml(lines.join("\n")), + ); + }); +}); diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.ts index 876eeaaa12..295ce36e75 100644 --- a/packages/core/src/api/parsers/markdown/markdownToHtml.ts +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.ts @@ -805,7 +805,9 @@ function isHtmlBlockStart(line: string): boolean { // ─── Block-Level Tokenizer ────────────────────────────────────────────────── function tokenize(markdown: string): Token[] { - const lines = markdown.split("\n"); + // A line ends with "\n", "\r\n" or "\r" (CommonMark). A file from Windows + // has "\r\n". + const lines = markdown.split(/\r\n?|\n/); const tokens: Token[] = []; let i = 0; let prevLineWasBlank = true; // treat start of document as after blank From cd0801c585a7886b30c367512c23614fde129a3d Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 11:14:58 +0100 Subject: [PATCH 4/8] fix(core): the markdown parser reads backslash escapes in link URLs A link URL with parentheses changed in a markdown export and parse round trip. The exporter writes the URL `https://example.com/(x)` as `[a](https://example.com/\(x\))`. The parser gave the URL back with the backslashes: `https://example.com/\(x\)`. Each round trip added more backslashes. A backslash in a URL had the same fault. The parser used the backslash escapes to find the end of the URL, but it did not remove the backslashes from the URL. The parser now removes the backslash of each backslash escape in the URL of a link or an image. The escapable characters are the same as in text. --- .../api/parsers/markdown/markdownToHtml.ts | 18 +++++++++++-- .../markdown/linkWithParenthesesInUrl.json | 25 +++++++++++++++++++ .../exportParseEqualityTestInstances.ts | 18 +++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/linkWithParenthesesInUrl.json diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.ts index 295ce36e75..1c4075b407 100644 --- a/packages/core/src/api/parsers/markdown/markdownToHtml.ts +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.ts @@ -46,6 +46,9 @@ type InlineTokenizer = ( allowLinks: boolean, ) => { html: string; end: number } | null; +/** The characters that a backslash escapes. */ +const ESCAPABLE_CHARACTERS = "\\`*_{}[]()#+-.!~|<>"; + function tryBackslashEscape( text: string, i: number, @@ -61,7 +64,7 @@ function tryBackslashEscape( return { html: "
", end: i + 2 }; } // Escapable characters - if ("\\`*_{}[]()#+-.!~|<>".includes(next)) { + if (ESCAPABLE_CHARACTERS.includes(next)) { return { html: escapeHtml(next), end: i + 2 }; } return null; @@ -584,7 +587,18 @@ function parseDestinationAndTitle(raw: string): { } } - return { url, title }; + return { url: removeBackslashEscapes(url), title }; +} + +/** + * Removes the backslash of each backslash escape. As in CommonMark, a + * backslash escape works in a link destination as it does in text. The + * markdown exporter writes `\(`, `\)` and `\\` in a link destination. + */ +function removeBackslashEscapes(text: string): string { + return text.replace(/\\(.)/g, (match, char: string) => + ESCAPABLE_CHARACTERS.includes(char) ? char : match, + ); } function parseDelimited( diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/linkWithParenthesesInUrl.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/linkWithParenthesesInUrl.json new file mode 100644 index 0000000000..aeebedf45c --- /dev/null +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/linkWithParenthesesInUrl.json @@ -0,0 +1,25 @@ +[ + { + "children": [], + "content": [ + { + "content": [ + { + "styles": {}, + "text": "Link", + "type": "text", + }, + ], + "href": "https://example.com/wiki/Page_(topic)", + "type": "link", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts index 28c9fb34fd..adbe7fe9fd 100644 --- a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts +++ b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts @@ -698,6 +698,24 @@ export const exportParseEqualityTestInstancesMarkdown: TestInstance< }, executeTest: testExportParseEqualityMarkdown, }, + { + testCase: { + name: "markdown/linkWithParenthesesInUrl", + content: [ + { + type: "paragraph", + content: [ + { + type: "link", + content: "Link", + href: "https://example.com/wiki/Page_(topic)", + }, + ], + }, + ], + }, + executeTest: testExportParseEqualityMarkdown, + }, { testCase: { name: "markdown/plainUrlLink", From ee143e6f05b83df4377e7f80d95ed47a39ff5b0c Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 11:19:30 +0100 Subject: [PATCH 5/8] fix(core): the markdown parser does not read a code span as a code fence A paragraph that starts with a code span of three backticks, for example ```a``b```, became the start of a code block. All the blocks after it became the content of this code block. The markdown exporter writes such a code span for code that contains two backticks. The parser read each line that starts with three backticks as a code fence. In CommonMark, the info string after a backtick fence cannot contain a backtick, so such a line is not a fence. The three places that test for the start of a code fence (a new block, a line that continues a paragraph, and a line that continues a quote) now use this rule. A fence of tildes is not changed. --- .../parsers/markdown/markdownToHtml.test.ts | 24 +++++++++++++++++++ .../api/parsers/markdown/markdownToHtml.ts | 11 +++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts index 46ad2cc658..dc3ccef5ed 100644 --- a/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts @@ -34,4 +34,28 @@ describe("markdownToHtml", () => { markdownToHtml(lines.join("\n")), ); }); + + // The info string of a backtick code fence cannot contain a backtick + // (CommonMark). Thus a line that starts with a code span of three backticks + // is text. The markdown exporter writes such a code span for code that + // contains two backticks. + describe("a line that starts with a code span of three backticks", () => { + it("is a paragraph and not the start of a code block", () => { + expect(markdownToHtml("```a``b```\n\nnext")).toBe( + "

a``b

next

", + ); + }); + + it("continues the paragraph before it", () => { + expect(markdownToHtml("text\n```a``b```")).toMatch( + /^

text
\s*a``b<\/code><\/p>$/, + ); + }); + + it("continues the quote before it", () => { + expect(markdownToHtml("> quote\n```a``b```")).toMatch( + /^

quote
\s*a``b<\/code><\/p><\/blockquote>$/, + ); + }); + }); }); diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.ts index 1c4075b407..7e7dcf8065 100644 --- a/packages/core/src/api/parsers/markdown/markdownToHtml.ts +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.ts @@ -836,8 +836,11 @@ function tokenize(markdown: string): Token[] { continue; } - // Fenced code block (0-3 leading spaces allowed per CommonMark) - const fenceMatch = line.match(/^ {0,3}(`{3,}|~{3,})(.*)$/); + // Fenced code block (0-3 leading spaces allowed per CommonMark). The info + // string after a backtick fence cannot contain a backtick (CommonMark). + // Thus a line that starts with a code span, for example ```a``b```, is + // not a fence. + const fenceMatch = line.match(/^ {0,3}(`{3,}(?=[^`]*$)|~{3,})(.*)$/); if (fenceMatch) { const fence = fenceMatch[1]; const fenceChar = fence[0]; @@ -954,7 +957,7 @@ function tokenize(markdown: string): Token[] { if (/^(#{1,6})\s/.test(cur)) { break; } // heading - if (/^(`{3,}|~{3,})/.test(cur)) { + if (/^(`{3,}(?=[^`]*$)|~{3,})/.test(cur)) { break; } // code fence if (/^(\s{0,3})([-*_])\s*(\2\s*){2,}$/.test(cur)) { @@ -1116,7 +1119,7 @@ function tokenize(markdown: string): Token[] { if (/^(#{1,6})\s/.test(nextLine)) { break; } - if (/^(`{3,}|~{3,})/.test(nextLine)) { + if (/^(`{3,}(?=[^`]*$)|~{3,})/.test(nextLine)) { break; } if (/^\s{0,3}>/.test(nextLine)) { From 75374ea6daee57feb161d9b0184e6adf8b7812a3 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 11:21:15 +0100 Subject: [PATCH 6/8] fix(core): the markdown export keeps the spaces at the edges of a code span Inline code that starts and ends with a space, for example " a ", lost these two spaces in a markdown export and parse round trip. The export was ` a `, and a markdown parser removes one space from each side of a code span that starts and ends with a space (CommonMark). The exporter now adds one space on each side of such code: ` a `. It did this already for code that starts or ends with a backtick. --- .../api/exporters/markdown/htmlToMarkdown.ts | 8 ++++++- .../inlineCodeWithSurroundingSpaces.json | 21 +++++++++++++++++++ .../exportParseEqualityTestInstances.ts | 18 ++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/inlineCodeWithSurroundingSpaces.json diff --git a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts index f99b3d36dc..a3933762fe 100644 --- a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts +++ b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts @@ -780,7 +780,13 @@ function serializeInlineContent(el: Element): string { ...(text.match(/`+/g) ?? []).map((run) => run.length), ); const fence = "`".repeat(longestRun + 1); - const needsPadding = text.startsWith("`") || text.endsWith("`"); + // A markdown parser removes one space from each side of a code span + // that starts and ends with a space and has other characters + // (CommonMark). Add a space on each side to keep these spaces. + const needsPadding = + text.startsWith("`") || + text.endsWith("`") || + (text.startsWith(" ") && text.endsWith(" ") && /[^ ]/.test(text)); result += fence + (needsPadding ? ` ${text} ` : text) + fence; break; } diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/inlineCodeWithSurroundingSpaces.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/inlineCodeWithSurroundingSpaces.json new file mode 100644 index 0000000000..fcdc775c69 --- /dev/null +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/inlineCodeWithSurroundingSpaces.json @@ -0,0 +1,21 @@ +[ + { + "children": [], + "content": [ + { + "styles": { + "code": true, + }, + "text": " Code text ", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts index adbe7fe9fd..7519894a2c 100644 --- a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts +++ b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts @@ -652,6 +652,24 @@ export const exportParseEqualityTestInstancesMarkdown: TestInstance< }, executeTest: testExportParseEqualityMarkdown, }, + { + testCase: { + name: "markdown/inlineCodeWithSurroundingSpaces", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: " Code text ", + styles: { code: true }, + }, + ], + }, + ], + }, + executeTest: testExportParseEqualityMarkdown, + }, { testCase: { name: "markdown/boldItalic", From d9e837fed50eb91b024de322fe396e6d90a4ce1b Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 20:34:49 +0100 Subject: [PATCH 7/8] fix(core): the markdown parser keeps the indent of a line in a code span A paragraph line after the first can have any indent, and the indent is not a part of the text. The parser removed it from each line of the paragraph before it read the inline content. Thus it also removed the indent of a line inside a code span: `foo` + line end + tab + `bar` in backticks gave the code "foo bar". In CommonMark, a code span has its text as it is: only the line end becomes a space ("foo \tbar"). The three line break rules of the inline parser now skip the indent of the line after the break. The paragraph keeps its lines as they are, except for the indent of the first line (a maximum of 3 spaces, as before). So the indent is removed where a line break is read, and not in a code span. --- .../parsers/markdown/markdownToHtml.test.ts | 20 ++++++++++++++ .../api/parsers/markdown/markdownToHtml.ts | 26 ++++++++++++++----- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts index dc3ccef5ed..fda0f671b8 100644 --- a/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.test.ts @@ -35,6 +35,26 @@ describe("markdownToHtml", () => { ); }); + // A paragraph line after the first can have any indent, and the indent is + // not a part of the text. But a code span has its text as it is: only the + // line end becomes a space (CommonMark). + describe("the indent of a paragraph line after the first", () => { + it.each([ + ["a line end alone", "aaa\n bbb\n\tccc"], + ["a backslash", "aaa\\\n bbb\\\n\tccc"], + ["two spaces", "aaa \n bbb \n\tccc"], + ])("is removed after a line break from %s", (_, markdown) => { + expect(markdownToHtml(markdown)).toBe("

aaa
bbb
ccc

"); + }); + + it.each([ + ["a tab", "`foo\n\tbar`", "

foo \tbar

"], + ["spaces", "`foo\n bar`", "

foo bar

"], + ])("stays in a code span, for %s", (_, markdown, html) => { + expect(markdownToHtml(markdown)).toBe(html); + }); + }); + // The info string of a backtick code fence cannot contain a backtick // (CommonMark). Thus a line that starts with a code span of three backticks // is text. The markdown exporter writes such a code span for code that diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.ts index 7e7dcf8065..f7a7f90736 100644 --- a/packages/core/src/api/parsers/markdown/markdownToHtml.ts +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.ts @@ -49,6 +49,19 @@ type InlineTokenizer = ( /** The characters that a backslash escapes. */ const ESCAPABLE_CHARACTERS = "\\`*_{}[]()#+-.!~|<>"; +/** + * Gives the index after the indent (spaces and tabs) of the line that starts + * at `i`. The line after a line break in a paragraph can have any indent, + * which is not a part of the text (CommonMark). The line break rules use this + * and a code span does not, so the indent of a line in a code span stays. + */ +function skipIndent(text: string, i: number): number { + while (text[i] === " " || text[i] === "\t") { + i++; + } + return i; +} + function tryBackslashEscape( text: string, i: number, @@ -61,7 +74,7 @@ function tryBackslashEscape( // the HTML parser reads that newline as a space at the start of the next // line. if (next === "\n") { - return { html: "
", end: i + 2 }; + return { html: "
", end: skipIndent(text, i + 2) }; } // Escapable characters if (ESCAPABLE_CHARACTERS.includes(next)) { @@ -233,7 +246,7 @@ function trySoftBreak( i: number, ): { html: string; end: number } | null { if (text[i] === "\n") { - return { html: "
", end: i + 1 }; + return { html: "
", end: skipIndent(text, i + 1) }; } return null; } @@ -337,7 +350,7 @@ function parseInline(text: string, allowLinks = true): string { ) { result = result.replace(/ +$/, ""); result += "
"; - i++; + i = skipIndent(text, i + 1); continue; } @@ -1149,16 +1162,15 @@ function tokenize(markdown: string): Token[] { i++; } // CommonMark allows up to 3 leading spaces of indent on the first - // paragraph line, and any indent on the lines after it. Each of these - // lines comes after a line break, so its indent would show as whitespace - // at the start of the line. + // paragraph line. The lines after it can have any indent: the line break + // rules of `parseInline` remove it (see `skipIndent`). // Also strip trailing whitespace from the final line so a trailing // hard-break sequence (` \n` at end of paragraph) doesn't leak as // literal trailing spaces in the rendered output. tokens.push({ type: "paragraph", content: paraLines - .map((l, index) => l.replace(index === 0 ? /^ {1,3}/ : /^[ \t]+/, "")) + .map((l, index) => (index === 0 ? l.replace(/^ {1,3}/, "") : l)) .join("\n") .replace(/[ \t]+$/, ""), }); From 11269469b0e20d632b18c3b545e6105f0209f331 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 16:33:58 +0100 Subject: [PATCH 8/8] fix(core): the markdown export escapes the URL of an image and a video The markdown parser now removes the backslash escapes in the destination of a link and of an image. The export escaped only the URL of a link. Thus the URL of an image or a video lost a backslash in a round trip: \\server\share\image.png became \server\share\image.png. The export now escapes each destination that it writes (image, video, embed), as it does for a link: a backslash and a parenthesis get a backslash before them. --- .../api/exporters/markdown/htmlToMarkdown.ts | 16 ++++++---- .../markdown/mediaWithBackslashInUrl.json | 30 +++++++++++++++++++ .../exportParseEqualityTestInstances.ts | 22 ++++++++++++++ 3 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/mediaWithBackslashInUrl.json diff --git a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts index a3933762fe..531724bb87 100644 --- a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts +++ b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts @@ -559,7 +559,7 @@ function serializeImage(el: HTMLElement, ctx: SerializeContext): string { if (!src) { return "\n\n"; } - return ctx.indent + `![${alt}](${src})\n\n`; + return ctx.indent + `![${alt}](${escapeLinkDestination(src)})\n\n`; } function serializeVideo(el: HTMLElement, ctx: SerializeContext): string { @@ -568,7 +568,7 @@ function serializeVideo(el: HTMLElement, ctx: SerializeContext): string { if (!src) { return "\n\n"; } - return ctx.indent + `![${name}](${src})\n\n`; + return ctx.indent + `![${name}](${escapeLinkDestination(src)})\n\n`; } function serializeAudio(el: HTMLElement, ctx: SerializeContext): string { @@ -589,7 +589,7 @@ function serializeEmbed(el: HTMLElement, ctx: SerializeContext): string { if (!src) { return "\n\n"; } - return ctx.indent + `[](${src})\n\n`; + return ctx.indent + `[](${escapeLinkDestination(src)})\n\n`; } function serializeFigure(el: HTMLElement, ctx: SerializeContext): string { @@ -645,7 +645,7 @@ function serializeMediaFigure( // No caption + has a markdown shorthand → use it. if (!captionText && kind !== "audio") { - return ctx.indent + `![${descriptor}](${src})\n\n`; + return ctx.indent + `![${descriptor}](${escapeLinkDestination(src)})\n\n`; } // The descriptor (alt / data-name) is dropped when it duplicates the @@ -720,6 +720,10 @@ function formatLink(text: string, href: string): string { return `[${text}](${escapeLinkDestination(href)})`; } +/** + * Escapes the backslashes and the parentheses in the destination of a link or + * an image. The markdown parser removes these escapes. + */ function escapeLinkDestination(url: string): string { return url.replace(/[\\()]/g, "\\$&"); } @@ -820,7 +824,7 @@ function serializeInlineContent(el: Element): string { case "img": { const src = childEl.getAttribute("src") || ""; const alt = childEl.getAttribute("alt") || ""; - result += `![${alt}](${src})`; + result += `![${alt}](${escapeLinkDestination(src)})`; break; } case "video": { @@ -832,7 +836,7 @@ function serializeInlineContent(el: Element): string { childEl.getAttribute("data-name") || childEl.getAttribute("title") || ""; - result += `![${name}](${src})`; + result += `![${name}](${escapeLinkDestination(src)})`; break; } case "p": diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/mediaWithBackslashInUrl.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/mediaWithBackslashInUrl.json new file mode 100644 index 0000000000..d95315e9b7 --- /dev/null +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/mediaWithBackslashInUrl.json @@ -0,0 +1,30 @@ +[ + { + "children": [], + "content": undefined, + "id": "1", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "Example", + "showPreview": true, + "textAlignment": "left", + "url": "\\server\share\image.png", + }, + "type": "image", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "Example", + "showPreview": true, + "textAlignment": "left", + "url": "C:\videos\_old\video.mp4", + }, + "type": "video", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts index 7519894a2c..7cc07ee5ac 100644 --- a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts +++ b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts @@ -821,6 +821,28 @@ export const exportParseEqualityTestInstancesMarkdown: TestInstance< }, executeTest: testExportParseEqualityMarkdown, }, + { + testCase: { + name: "markdown/mediaWithBackslashInUrl", + content: [ + { + type: "image", + props: { + url: "\\\\server\\share\\image.png", + name: "Example", + }, + }, + { + type: "video", + props: { + url: "C:\\videos\\_old\\video.mp4", + name: "Example", + }, + }, + ], + }, + executeTest: testExportParseEqualityMarkdown, + }, { testCase: { name: "markdown/video",