From aa680a8bb1ba679280d34ef42887e29a6d414a3b Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 11:32:45 +0100 Subject: [PATCH 1/4] fix(core): the markdown export writes adjacent italic runs as one run The external HTML has one element for each run of text. Two adjacent italic runs that are different in a second style (for example strike, underline or a color) are two adjacent `` elements. The markdown serializer wrote each element with its own delimiters. Thus, the italic and struck text `a` before the italic text `b` became `*~~a~~**b*`. The markdown parser reads the `**` in the middle as text. After a parse, the text was `a**b`, and each more round trip added two more asterisks. The serializer now moves the content of an italic element into the italic element directly before it, before it reads the children of an element. The italic delimiter is then there once: `*~~a~~b*`. An italic run directly before a bold and italic run does not change (`*a****b***`): there, the two `` elements are not siblings. --- .../api/exporters/markdown/htmlToMarkdown.ts | 37 +++++++++ .../blocknoteHTML/style/adjacentItalic.html | 26 ++++++ .../html/style/adjacentItalic.html | 12 +++ .../markdown/style/adjacentItalic.md | 3 + .../nodes/style/adjacentItalic.json | 80 +++++++++++++++++++ .../export/exportTestInstances.ts | 38 +++++++++ .../markdown/markdown/adjacentItalic.json | 48 +++++++++++ .../exportParseEqualityTestInstances.ts | 38 +++++++++ 8 files changed, 282 insertions(+) create mode 100644 tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/style/adjacentItalic.html create mode 100644 tests/src/unit/core/formatConversion/export/__snapshots__/html/style/adjacentItalic.html create mode 100644 tests/src/unit/core/formatConversion/export/__snapshots__/markdown/style/adjacentItalic.md create mode 100644 tests/src/unit/core/formatConversion/export/__snapshots__/nodes/style/adjacentItalic.json create mode 100644 tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/adjacentItalic.json diff --git a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts index 44be7074dc..cb48b672ad 100644 --- a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts +++ b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts @@ -783,6 +783,8 @@ function toSingleLine(content: string): string { function serializeInlineContent(el: Element): string { let result = ""; + mergeAdjacentItalicElements(el); + for (const child of Array.from(el.childNodes)) { if (child.nodeType === 3 /* Node.TEXT_NODE */) { result += escapeHtmlLikeText(child.textContent || ""); @@ -882,6 +884,41 @@ function serializeInlineContent(el: Element): string { return result; } +/** + * The external HTML exporter writes each run of text with its own elements. + * Two adjacent italic runs that are different in a second style then give + * two adjacent italic elements, for example `ab`. + * If the serializer writes each element, the italic delimiter closes and + * opens at one position (`*~~a~~**b*`), and the markdown parser reads `**` + * as text. Thus, move the content of each italic element into the italic + * element directly before it (`ab`). The result has one + * delimiter pair: `*~~a~~b*`. The elements are in the serializer's own copy + * of the document (see `htmlToMarkdown`), so the change has no other effect. + */ +function mergeAdjacentItalicElements(parent: Element) { + let element = parent.firstElementChild; + while (element) { + const next = element.nextElementSibling; + if ( + next && + // No text is between the two elements. + next === element.nextSibling && + isItalicElement(element) && + isItalicElement(next) + ) { + element.append(...Array.from(next.childNodes)); + next.remove(); + continue; + } + element = next; + } +} + +function isItalicElement(element: Element): boolean { + const tag = element.tagName.toLowerCase(); + return tag === "em" || tag === "i"; +} + /** * Escape `<` where the markdown parser would read raw HTML or an autolink, so * text like `
` loads back as text. A `<` before a space or digit diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/style/adjacentItalic.html b/tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/style/adjacentItalic.html new file mode 100644 index 0000000000..27284e12d6 --- /dev/null +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/style/adjacentItalic.html @@ -0,0 +1,26 @@ +
+
+
+
+

+ + Struck + + Italic +

+
+
+
+
+
+
+

+ Italic + + Underlined + +

+
+
+
+
\ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/html/style/adjacentItalic.html b/tests/src/unit/core/formatConversion/export/__snapshots__/html/style/adjacentItalic.html new file mode 100644 index 0000000000..6d80c538e0 --- /dev/null +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/html/style/adjacentItalic.html @@ -0,0 +1,12 @@ +

+ + Struck + + Italic +

+

+ Italic + + Underlined + +

\ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/style/adjacentItalic.md b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/style/adjacentItalic.md new file mode 100644 index 0000000000..ee623ad3e6 --- /dev/null +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/style/adjacentItalic.md @@ -0,0 +1,3 @@ +*~~Struck~~Italic* + +*ItalicUnderlined* diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/nodes/style/adjacentItalic.json b/tests/src/unit/core/formatConversion/export/__snapshots__/nodes/style/adjacentItalic.json new file mode 100644 index 0000000000..b4782acdfc --- /dev/null +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/nodes/style/adjacentItalic.json @@ -0,0 +1,80 @@ +[ + { + "attrs": { + "id": "1", + }, + "content": [ + { + "attrs": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "content": [ + { + "marks": [ + { + "type": "italic", + }, + { + "type": "strike", + }, + ], + "text": "Struck", + "type": "text", + }, + { + "marks": [ + { + "type": "italic", + }, + ], + "text": "Italic", + "type": "text", + }, + ], + "type": "paragraph", + }, + ], + "type": "blockContainer", + }, + { + "attrs": { + "id": "2", + }, + "content": [ + { + "attrs": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "content": [ + { + "marks": [ + { + "type": "italic", + }, + ], + "text": "Italic", + "type": "text", + }, + { + "marks": [ + { + "type": "italic", + }, + { + "type": "underline", + }, + ], + "text": "Underlined", + "type": "text", + }, + ], + "type": "paragraph", + }, + ], + "type": "blockContainer", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/export/exportTestInstances.ts b/tests/src/unit/core/formatConversion/export/exportTestInstances.ts index 0b78c8c90c..7906c68051 100644 --- a/tests/src/unit/core/formatConversion/export/exportTestInstances.ts +++ b/tests/src/unit/core/formatConversion/export/exportTestInstances.ts @@ -823,6 +823,44 @@ export const exportTestInstancesBlockNoteHTML: TestInstance< }, executeTest: testExportBlockNoteHTML, }, + { + testCase: { + name: "style/adjacentItalic", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "Struck", + styles: { italic: true, strike: true }, + }, + { + type: "text", + text: "Italic", + styles: { italic: true }, + }, + ], + }, + { + type: "paragraph", + content: [ + { + type: "text", + text: "Italic", + styles: { italic: true }, + }, + { + type: "text", + text: "Underlined", + styles: { italic: true, underline: true }, + }, + ], + }, + ], + }, + executeTest: testExportBlockNoteHTML, + }, // Numbered list with custom start { testCase: { diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/adjacentItalic.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/adjacentItalic.json new file mode 100644 index 0000000000..005e154b73 --- /dev/null +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/adjacentItalic.json @@ -0,0 +1,48 @@ +[ + { + "children": [], + "content": [ + { + "styles": { + "italic": true, + "strike": true, + }, + "text": "Struck", + "type": "text", + }, + { + "styles": { + "italic": true, + }, + "text": "Italic", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": { + "italic": true, + }, + "text": "ItalicUnderlined", + "type": "text", + }, + ], + "id": "2", + "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 b351380003..105fb91f9c 100644 --- a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts +++ b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts @@ -1267,6 +1267,44 @@ export const exportParseEqualityTestInstancesMarkdown: TestInstance< }, executeTest: testExportParseEqualityMarkdown, }, + { + testCase: { + name: "markdown/adjacentItalic", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "Struck", + styles: { italic: true, strike: true }, + }, + { + type: "text", + text: "Italic", + styles: { italic: true }, + }, + ], + }, + { + type: "paragraph", + content: [ + { + type: "text", + text: "Italic", + styles: { italic: true }, + }, + { + type: "text", + text: "Underlined", + styles: { italic: true, underline: true }, + }, + ], + }, + ], + }, + executeTest: testExportParseEqualityMarkdown, + }, { testCase: { name: "markdown/specialCharEscaping", From 61f979f80214ece1cf7cd0e211bcd73b9c7b8dcf Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 21:08:07 +0100 Subject: [PATCH 2/4] fix(core): the markdown export removes hard breaks at block edges Markdown has no syntax for a hard break at the end of a paragraph or a quote. The serializer writes a hard break as a backslash before a newline, and the markdown parser reads a backslash at the end of a block as a literal character. The serializer removed the hard breaks at the edges only for a paragraph, and only if no space or tab was after the last hard break. Thus, a quote that ends with a hard break came back with a backslash at its end. A paragraph that ends with a hard break and a space also came back with a backslash. Now each place that writes the text of a paragraph, a heading, a quote or a list item uses `trimHardBreaks`. The function also removes the spaces and tabs that are after a hard break at the end of the text. A heading and a list item write a hard break inside their text as `
`. A hard break at their edges is removed too, so each block has one rule. A quote that starts with a hard break loses that hard break now, as a paragraph does. Before, a quote kept it (`> \` before `> a`). --- .../api/exporters/markdown/htmlToMarkdown.ts | 35 ++-- .../blocknoteHTML/hardbreak/blockEdges.html | 102 ++++++++++ .../html/hardbreak/blockEdges.html | 49 +++++ .../markdown/hardbreak/blockEdges.md | 13 ++ .../nodes/hardbreak/blockEdges.json | 185 ++++++++++++++++++ .../export/exportTestInstances.ts | 38 ++++ .../markdown/hardBreakAtBlockEdges.json | 124 ++++++++++++ .../hardBreakBeforeTrailingSpace.json | 19 ++ .../exportParseEqualityTestInstances.ts | 50 +++++ 9 files changed, 601 insertions(+), 14 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/hardbreak/blockEdges.html create mode 100644 tests/src/unit/core/formatConversion/export/__snapshots__/html/hardbreak/blockEdges.html create mode 100644 tests/src/unit/core/formatConversion/export/__snapshots__/markdown/hardbreak/blockEdges.md create mode 100644 tests/src/unit/core/formatConversion/export/__snapshots__/nodes/hardbreak/blockEdges.json create mode 100644 tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakAtBlockEdges.json create mode 100644 tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakBeforeTrailingSpace.json diff --git a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts index cb48b672ad..09fa10bf94 100644 --- a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts +++ b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts @@ -111,9 +111,7 @@ function serializeNode(node: Node, ctx: SerializeContext): string { // ─── Block Serializers ─────────────────────────────────────────────────────── function serializeParagraph(el: HTMLElement, ctx: SerializeContext): string { - const content = serializeInlineContent(el); - // Trim leading/trailing hard breaks (matching remark behavior) - const trimmed = trimHardBreaks(content); + const trimmed = serializeBlockText(el); if (ctx.inListItem) { return trimmed; } @@ -123,7 +121,7 @@ function serializeParagraph(el: HTMLElement, ctx: SerializeContext): string { function serializeHeading(el: HTMLElement, ctx: SerializeContext): string { const level = parseInt(el.tagName[1], 10); const prefix = "#".repeat(level) + " "; - const content = toSingleLine(serializeInlineContent(el)); + const content = toSingleLine(serializeBlockText(el)); return ctx.indent + prefix + content + "\n\n"; } @@ -141,7 +139,7 @@ function serializeBlockquote(el: HTMLElement, ctx: SerializeContext): string { for (const child of blockChildren) { const tag = child.tagName.toLowerCase(); if (tag === "p") { - parts.push(serializeInlineContent(child as HTMLElement)); + parts.push(serializeBlockText(child)); } else { const innerCtx: SerializeContext = { indent: "", inListItem: false }; parts.push(serializeNode(child, innerCtx).trim()); @@ -150,7 +148,7 @@ function serializeBlockquote(el: HTMLElement, ctx: SerializeContext): string { content = parts.join("\n\n"); } else { // No block-level children — treat entire content as inline - content = serializeInlineContent(el); + content = serializeBlockText(el); } const lines = content.split("\n"); @@ -325,12 +323,10 @@ function serializeListItem( const summary = details.querySelector("summary"); const summaryP = summary?.querySelector("p"); firstContentEl = details; - inlineContent = summaryP ? serializeInlineContent(summaryP) : ""; + inlineContent = summaryP ? serializeBlockText(summaryP) : ""; } else { firstContentEl = getFirstContentElement(el, checkbox); - inlineContent = firstContentEl - ? serializeInlineContent(firstContentEl) - : ""; + inlineContent = firstContentEl ? serializeBlockText(firstContentEl) : ""; } // The marker line ends with a single `\n` so that consecutive list items @@ -352,7 +348,7 @@ function serializeListItem( } const childTag = child.tagName.toLowerCase(); if (childTag === "p") { - const content = serializeInlineContent(child as HTMLElement); + const content = serializeBlockText(child); // Continuation paragraph needs a blank line to separate it from the // previous content; CommonMark would otherwise treat it as a soft // wrap of that content. @@ -382,7 +378,7 @@ function serializeListItem( } else if (childTag === "p") { // Continuation paragraph within list item — requires blank line before // so it isn't read as part of the marker line's text. - const content = serializeInlineContent(child as HTMLElement); + const content = serializeBlockText(child); result += "\n" + childIndent + indentLines(content, childIndent) + "\n"; } else { // Other block-level children (code blocks, blockquotes, etc.) already @@ -938,6 +934,16 @@ function escapeHtmlLikeText(text: string): string { * if the trailing space was escaped. */ +/** + * Serialize the inline content of a block, for use as the text of the block: + * without hard breaks at its edges. Markdown has no syntax for a hard break + * at the end of a block (the parser reads the backslash there as a literal + * character), nor for one at the start of a heading or a list item. + */ +function serializeBlockText(el: Element): string { + return trimHardBreaks(serializeInlineContent(el)); +} + /** * Trim leading/trailing hard breaks from inline content. * Matches remark behavior where
at start/end of paragraph is dropped. @@ -945,7 +951,8 @@ function escapeHtmlLikeText(text: string): string { function trimHardBreaks(content: string): string { // Remove leading hard breaks let result = content.replace(/^(\\\n)+/, ""); - // Remove trailing hard breaks produced by `
` - result = result.replace(/(\\\n)+$/, ""); + // Remove trailing hard breaks produced by `
`, also with spaces or tabs + // after them: markdown ignores those at the end of a block. + result = result.replace(/(\\\n[ \t]*)+$/, ""); return result; } diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/hardbreak/blockEdges.html b/tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/hardbreak/blockEdges.html new file mode 100644 index 0000000000..e2307cfea7 --- /dev/null +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/hardbreak/blockEdges.html @@ -0,0 +1,102 @@ +
+
+
+
+

+ Heading +
+

+
+
+
+
+
+
+

+
+ Bullet List Item +

+
+
+
+
+
+
+

+ Numbered List Item +
+

+
+
+
+
+
+
+
+ +
+

+ Check List Item +
+

+
+
+
+
+
+
+
+ Quote +
+
+
+
+
+
+
+
+ +
+
+

+ Toggle List Item +
+

+
+
+
+
+
+

+ Child Paragraph +
+

+
+
+
+
+
+
+
+
+
\ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/html/hardbreak/blockEdges.html b/tests/src/unit/core/formatConversion/export/__snapshots__/html/hardbreak/blockEdges.html new file mode 100644 index 0000000000..9e439638c5 --- /dev/null +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/html/hardbreak/blockEdges.html @@ -0,0 +1,49 @@ +

+ Heading +
+

+
    +
  • +

    +
    + Bullet List Item +

    +
  • +
+
    +
  1. +

    + Numbered List Item +
    +

    +
  2. +
+
    +
  • + +

    + Check List Item +
    +

    +
  • +
+
+ Quote +
+
+
    +
  • +
    + +

    + Toggle List Item +
    +

    +
    +

    + Child Paragraph +
    +

    +
    +
  • +
\ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/hardbreak/blockEdges.md b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/hardbreak/blockEdges.md new file mode 100644 index 0000000000..d0f3786fd5 --- /dev/null +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/hardbreak/blockEdges.md @@ -0,0 +1,13 @@ +# Heading + +* Bullet List Item + +1. Numbered List Item + +* [ ] Check List Item + +> Quote + +* Toggle List Item + + Child Paragraph diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/nodes/hardbreak/blockEdges.json b/tests/src/unit/core/formatConversion/export/__snapshots__/nodes/hardbreak/blockEdges.json new file mode 100644 index 0000000000..3162245329 --- /dev/null +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/nodes/hardbreak/blockEdges.json @@ -0,0 +1,185 @@ +[ + { + "attrs": { + "id": "1", + }, + "content": [ + { + "attrs": { + "backgroundColor": "default", + "isToggleable": false, + "level": 1, + "textAlignment": "left", + "textColor": "default", + }, + "content": [ + { + "text": "Heading", + "type": "text", + }, + { + "type": "hardBreak", + }, + ], + "type": "heading", + }, + ], + "type": "blockContainer", + }, + { + "attrs": { + "id": "2", + }, + "content": [ + { + "attrs": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "content": [ + { + "type": "hardBreak", + }, + { + "text": "Bullet List Item", + "type": "text", + }, + ], + "type": "bulletListItem", + }, + ], + "type": "blockContainer", + }, + { + "attrs": { + "id": "3", + }, + "content": [ + { + "attrs": { + "backgroundColor": "default", + "start": undefined, + "textAlignment": "left", + "textColor": "default", + }, + "content": [ + { + "text": "Numbered List Item", + "type": "text", + }, + { + "type": "hardBreak", + }, + ], + "type": "numberedListItem", + }, + ], + "type": "blockContainer", + }, + { + "attrs": { + "id": "4", + }, + "content": [ + { + "attrs": { + "backgroundColor": "default", + "checked": false, + "textAlignment": "left", + "textColor": "default", + }, + "content": [ + { + "text": "Check List Item", + "type": "text", + }, + { + "type": "hardBreak", + }, + ], + "type": "checkListItem", + }, + ], + "type": "blockContainer", + }, + { + "attrs": { + "id": "5", + }, + "content": [ + { + "attrs": { + "backgroundColor": "default", + "textColor": "default", + }, + "content": [ + { + "text": "Quote", + "type": "text", + }, + { + "type": "hardBreak", + }, + ], + "type": "quote", + }, + ], + "type": "blockContainer", + }, + { + "attrs": { + "id": "6", + }, + "content": [ + { + "attrs": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "content": [ + { + "text": "Toggle List Item", + "type": "text", + }, + { + "type": "hardBreak", + }, + ], + "type": "toggleListItem", + }, + { + "content": [ + { + "attrs": { + "id": "7", + }, + "content": [ + { + "attrs": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "content": [ + { + "text": "Child Paragraph", + "type": "text", + }, + { + "type": "hardBreak", + }, + ], + "type": "paragraph", + }, + ], + "type": "blockContainer", + }, + ], + "type": "blockGroup", + }, + ], + "type": "blockContainer", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/export/exportTestInstances.ts b/tests/src/unit/core/formatConversion/export/exportTestInstances.ts index 7906c68051..1465b80857 100644 --- a/tests/src/unit/core/formatConversion/export/exportTestInstances.ts +++ b/tests/src/unit/core/formatConversion/export/exportTestInstances.ts @@ -2958,6 +2958,44 @@ export const exportTestInstancesBlockNoteHTML: TestInstance< }, executeTest: testExportBlockNoteHTML, }, + { + testCase: { + name: "hardbreak/blockEdges", + content: [ + { + type: "heading", + content: "Heading\n", + }, + { + type: "bulletListItem", + content: "\nBullet List Item", + }, + { + type: "numberedListItem", + content: "Numbered List Item\n", + }, + { + type: "checkListItem", + content: "Check List Item\n", + }, + { + type: "quote", + content: "Quote\n", + }, + { + type: "toggleListItem", + content: "Toggle List Item\n", + children: [ + { + type: "paragraph", + content: "Child Paragraph\n", + }, + ], + }, + ], + }, + executeTest: testExportBlockNoteHTML, + }, { testCase: { name: "hardbreak/styles", diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakAtBlockEdges.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakAtBlockEdges.json new file mode 100644 index 0000000000..ccc3d1749e --- /dev/null +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakAtBlockEdges.json @@ -0,0 +1,124 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Heading", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "isToggleable": false, + "level": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Bullet List Item", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Numbered List Item", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Check List Item", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "checked": false, + "textAlignment": "left", + "textColor": "default", + }, + "type": "checkListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Quote", + "type": "text", + }, + ], + "id": "5", + "props": { + "backgroundColor": "default", + "textColor": "default", + }, + "type": "quote", + }, + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Child Paragraph", + "type": "text", + }, + ], + "id": "7", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Toggle List Item", + "type": "text", + }, + ], + "id": "6", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakBeforeTrailingSpace.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakBeforeTrailingSpace.json new file mode 100644 index 0000000000..a78bde1a73 --- /dev/null +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakBeforeTrailingSpace.json @@ -0,0 +1,19 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Paragraph", + "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 105fb91f9c..f9dbbcfccc 100644 --- a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts +++ b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts @@ -936,6 +936,56 @@ export const exportParseEqualityTestInstancesMarkdown: TestInstance< }, executeTest: testExportParseEqualityMarkdown, }, + { + testCase: { + name: "markdown/hardBreakAtBlockEdges", + content: [ + { + type: "heading", + content: "Heading\n", + }, + { + type: "bulletListItem", + content: "\nBullet List Item", + }, + { + type: "numberedListItem", + content: "Numbered List Item\n", + }, + { + type: "checkListItem", + content: "Check List Item\n", + }, + { + type: "quote", + content: "Quote\n", + }, + { + type: "toggleListItem", + content: "Toggle List Item\n", + children: [ + { + type: "paragraph", + content: "Child Paragraph\n", + }, + ], + }, + ], + }, + executeTest: testExportParseEqualityMarkdown, + }, + { + testCase: { + name: "markdown/hardBreakBeforeTrailingSpace", + content: [ + { + type: "paragraph", + content: "Paragraph\n ", + }, + ], + }, + executeTest: testExportParseEqualityMarkdown, + }, { testCase: { name: "markdown/hardBreakInHeading", From 444c72b82292c6cad7806740676f25e200b91dfb Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 21:09:01 +0100 Subject: [PATCH 3/4] fix(core): a hard break in a table cell stays in the cell in markdown The markdown export wrote a hard break in a table cell as a backslash and a newline. A table row is one line of markdown, so the newline cut the row. The parse of that markdown gave a table with an extra row. If the break was in the first row, the parse gave no table at all. The export now writes each hard break in a cell as a `
` tag. The markdown parser reads that tag as a hard break, so the cell is the same after an export and a parse. A hard break at the edge of a cell stays in the cell too. --- .../api/exporters/markdown/htmlToMarkdown.ts | 12 ++- .../markdown/table/advancedExample.md | 11 +-- .../markdown/table/hardBreakInCell.md | 7 +- .../markdown/tableWithHardBreakInCell.json | 99 +++++++++++++++++++ .../tableWithLineBreakAtCellEdge.json | 61 ++++++++++++ .../exportParseEqualityTestInstances.ts | 54 ++++++++++ 6 files changed, 232 insertions(+), 12 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/tableWithHardBreakInCell.json create mode 100644 tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/tableWithLineBreakAtCellEdge.json diff --git a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts index 09fa10bf94..8c9f2bc3f1 100644 --- a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts +++ b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts @@ -25,6 +25,9 @@ export function htmlToMarkdown(html: string): string { return result.trim() + "\n"; } +/** A serialized hard break: a backslash at the end of the line. */ +const HARD_BREAK = "\\\n"; + interface SerializeContext { indent: string; // current indentation prefix for list nesting // True when the current node is being serialized as continuation content @@ -455,8 +458,13 @@ function serializeTable(el: HTMLElement, ctx: SerializeContext): string { hasHeader = true; } + // A table row is one line, so the line end of a hard break would cut + // the row. Write each hard break as the HTML `
` tag, which the + // markdown parser reads as a hard break. const content = escapeTableCell( - serializeInlineContent(cell as HTMLElement).trim(), + serializeInlineContent(cell as HTMLElement) + .replaceAll(HARD_BREAK, "
") + .trim(), ); const colspan = parseInt(cell.getAttribute("colspan") || "1", 10); const rowspan = parseInt(cell.getAttribute("rowspan") || "1", 10); @@ -829,7 +837,7 @@ function serializeInlineContent(el: Element): string { break; } case "br": - result += "\\\n"; + result += HARD_BREAK; break; case "math": { // Inline math — emit its LaTeX source as a math span. Collapsed to diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/table/advancedExample.md b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/table/advancedExample.md index 53599a4feb..edb30e801d 100644 --- a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/table/advancedExample.md +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/table/advancedExample.md @@ -1,6 +1,5 @@ -| This row has headers | This is **RED** | Text is Blue | -| -------------------------------- | --------------- | ------------------ | -| This spans 2 columns\ -and 2 rows | | Sooo many features | -| | | | -| A cell | Another Cell | Aligned center | +| This row has headers | This is **RED** | Text is Blue | +| ---------------------------------- | --------------- | ------------------ | +| This spans 2 columns
and 2 rows | | Sooo many features | +| | | | +| A cell | Another Cell | Aligned center | diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/table/hardBreakInCell.md b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/table/hardBreakInCell.md index d9ffaf65a3..0d350dc824 100644 --- a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/table/hardBreakInCell.md +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/table/hardBreakInCell.md @@ -1,4 +1,3 @@ -| | | -| -------------- | ----------- | -| Line 1\ -Line 2 | Normal cell | +| | | +| ---------------- | ----------- | +| Line 1
Line 2 | Normal cell | diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/tableWithHardBreakInCell.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/tableWithHardBreakInCell.json new file mode 100644 index 0000000000..1ed7caeec7 --- /dev/null +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/tableWithHardBreakInCell.json @@ -0,0 +1,99 @@ +[ + { + "children": [], + "content": { + "columnWidths": [ + undefined, + undefined, + ], + "headerCols": undefined, + "headerRows": 1, + "rows": [ + { + "cells": [ + { + "content": [ + { + "styles": {}, + "text": "Header 1 +Line 2", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "styles": {}, + "text": "Header 2", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + ], + }, + { + "cells": [ + { + "content": [ + { + "styles": {}, + "text": "Cell 1 +Line 2", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "styles": {}, + "text": "Cell 2", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + ], + }, + ], + "type": "tableContent", + }, + "id": "1", + "props": { + "textColor": "default", + }, + "type": "table", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/tableWithLineBreakAtCellEdge.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/tableWithLineBreakAtCellEdge.json new file mode 100644 index 0000000000..e65b93ae49 --- /dev/null +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/tableWithLineBreakAtCellEdge.json @@ -0,0 +1,61 @@ +[ + { + "children": [], + "content": { + "columnWidths": [ + undefined, + undefined, + ], + "headerCols": undefined, + "headerRows": undefined, + "rows": [ + { + "cells": [ + { + "content": [ + { + "styles": {}, + "text": " +Cell 1", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "styles": {}, + "text": "Cell 2 +", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + ], + }, + ], + "type": "tableContent", + }, + "id": "1", + "props": { + "textColor": "default", + }, + "type": "table", + }, +] \ 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 f9dbbcfccc..0a8b47887e 100644 --- a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts +++ b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts @@ -906,6 +906,60 @@ export const exportParseEqualityTestInstancesMarkdown: TestInstance< }, executeTest: testExportParseEqualityMarkdown, }, + { + // A table row is one line of markdown. A hard break in a cell must not + // end that line, or the parse reads the rest of the row as a new row. + testCase: { + name: "markdown/tableWithHardBreakInCell", + content: [ + { + type: "table", + content: { + type: "tableContent", + headerRows: 1, + rows: [ + { + cells: [ + [{ type: "text", text: "Header 1\nLine 2", styles: {} }], + "Header 2", + ], + }, + { + cells: [ + [{ type: "text", text: "Cell 1\nLine 2", styles: {} }], + "Cell 2", + ], + }, + ], + }, + }, + ], + }, + executeTest: testExportParseEqualityMarkdown, + }, + { + // A hard break at the edge of a cell stays in the cell too. + testCase: { + name: "markdown/tableWithLineBreakAtCellEdge", + content: [ + { + type: "table", + content: { + type: "tableContent", + rows: [ + { + cells: [ + [{ type: "text", text: "\nCell 1", styles: {} }], + [{ type: "text", text: "Cell 2\n", styles: {} }], + ], + }, + ], + }, + }, + ], + }, + executeTest: testExportParseEqualityMarkdown, + }, { testCase: { name: "markdown/quote", From 2515954242c254a67cdaa1972b2a46a2f52165da Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 12:09:14 +0100 Subject: [PATCH 4/4] fix(core): a URL with a space survives the markdown round trip The markdown export wrote the URL of a link, an image or a video as it is: `[a](https://example.com/my file.pdf)`. A space ends a link destination in markdown, so the parse cut the URL at the space. A video came back as an image, because the cut URL had no video extension. The export now writes a destination that has a space, a tab or a newline in angle brackets, as CommonMark allows: `[a]()`. The markdown parser reads the full URL from this form. These are the characters at which the parser ends a destination. A destination without them does not change, also if it has other white space such as a no-break space. The function, `formatLinkDestination`, is in its own module with a unit test, as `wrapWithDelimiter` is. --- .../markdown/formatLinkDestination.test.ts | 28 ++++++ .../markdown/formatLinkDestination.ts | 12 +++ .../api/exporters/markdown/htmlToMarkdown.ts | 26 ++++-- .../markdown/urlWithNoBreakSpace.json | 25 ++++++ .../markdown/markdown/urlWithSpace.json | 53 ++++++++++++ .../markdown/urlWithSpaceInTableCell.json | 65 ++++++++++++++ .../exportParseEqualityTestInstances.ts | 85 +++++++++++++++++++ 7 files changed, 287 insertions(+), 7 deletions(-) create mode 100644 packages/core/src/api/exporters/markdown/formatLinkDestination.test.ts create mode 100644 packages/core/src/api/exporters/markdown/formatLinkDestination.ts create mode 100644 tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/urlWithNoBreakSpace.json create mode 100644 tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/urlWithSpace.json create mode 100644 tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/urlWithSpaceInTableCell.json diff --git a/packages/core/src/api/exporters/markdown/formatLinkDestination.test.ts b/packages/core/src/api/exporters/markdown/formatLinkDestination.test.ts new file mode 100644 index 0000000000..894615e345 --- /dev/null +++ b/packages/core/src/api/exporters/markdown/formatLinkDestination.test.ts @@ -0,0 +1,28 @@ +// @vitest-environment node +import { describe, expect, it } from "vite-plus/test"; +import { formatLinkDestination } from "./formatLinkDestination.js"; + +describe("formatLinkDestination", () => { + it.each([ + "https://example.com/my file.pdf", + "https://example.com/my\tfile.pdf", + "https://example.com/my\nfile.pdf", + " https://example.com/file.pdf", + "https://example.com/file.pdf ", + ])("puts %j in angle brackets", (destination) => { + expect(formatLinkDestination(destination)).toBe(`<${destination}>`); + }); + + it.each(["", "https://example.com/file.pdf", "my%20file.pdf"])( + "leaves %j, which has no white space, unchanged", + (destination) => { + expect(formatLinkDestination(destination)).toBe(destination); + }, + ); + + it("leaves a destination with a no-break space unchanged", () => { + // The markdown parser reads a no-break space as a part of the URL. + const destination = "https://example.com/my file.pdf"; + expect(formatLinkDestination(destination)).toBe(destination); + }); +}); diff --git a/packages/core/src/api/exporters/markdown/formatLinkDestination.ts b/packages/core/src/api/exporters/markdown/formatLinkDestination.ts new file mode 100644 index 0000000000..794610e61f --- /dev/null +++ b/packages/core/src/api/exporters/markdown/formatLinkDestination.ts @@ -0,0 +1,12 @@ +/** + * A space, a tab or a newline ends the destination of a link or an image. + * Thus, a destination with one of these characters goes in angle brackets + * (``), as in CommonMark. Without the brackets, the markdown + * parser cuts the URL at that character. + */ +export function formatLinkDestination(destination: string): string { + // The characters at which `parseDestinationAndTitle` of the markdown parser + // ends a destination. Other white space, such as a no-break space, is a + // part of the URL there. + return /[ \t\n]/.test(destination) ? `<${destination}>` : destination; +} diff --git a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts index 8c9f2bc3f1..e5602697dd 100644 --- a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts +++ b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts @@ -1,4 +1,5 @@ import { parseAutolinkLiteral } from "../../parsers/markdown/autolink.js"; +import { formatLinkDestination } from "./formatLinkDestination.js"; import { wrapWithDelimiter } from "./wrapWithDelimiter.js"; /** @@ -572,7 +573,10 @@ function serializeImage(el: HTMLElement, ctx: SerializeContext): string { if (!src) { return "\n\n"; } - return ctx.indent + `![${alt}](${escapeLinkDestination(src)})\n\n`; + return ( + ctx.indent + + `![${alt}](${formatLinkDestination(escapeLinkDestination(src))})\n\n` + ); } function serializeVideo(el: HTMLElement, ctx: SerializeContext): string { @@ -581,7 +585,10 @@ function serializeVideo(el: HTMLElement, ctx: SerializeContext): string { if (!src) { return "\n\n"; } - return ctx.indent + `![${name}](${escapeLinkDestination(src)})\n\n`; + return ( + ctx.indent + + `![${name}](${formatLinkDestination(escapeLinkDestination(src))})\n\n` + ); } function serializeAudio(el: HTMLElement, ctx: SerializeContext): string { @@ -602,7 +609,9 @@ function serializeEmbed(el: HTMLElement, ctx: SerializeContext): string { if (!src) { return "\n\n"; } - return ctx.indent + `[](${escapeLinkDestination(src)})\n\n`; + return ( + ctx.indent + `[](${formatLinkDestination(escapeLinkDestination(src))})\n\n` + ); } function serializeFigure(el: HTMLElement, ctx: SerializeContext): string { @@ -658,7 +667,10 @@ function serializeMediaFigure( // No caption + has a markdown shorthand → use it. if (!captionText && kind !== "audio") { - return ctx.indent + `![${descriptor}](${escapeLinkDestination(src)})\n\n`; + return ( + ctx.indent + + `![${descriptor}](${formatLinkDestination(escapeLinkDestination(src))})\n\n` + ); } // The descriptor (alt / data-name) is dropped when it duplicates the @@ -730,7 +742,7 @@ function formatLink(text: string, href: string): string { return href; } } - return `[${text}](${escapeLinkDestination(href)})`; + return `[${text}](${formatLinkDestination(escapeLinkDestination(href))})`; } /** @@ -856,7 +868,7 @@ function serializeInlineContent(el: Element): string { case "img": { const src = childEl.getAttribute("src") || ""; const alt = childEl.getAttribute("alt") || ""; - result += `![${alt}](${escapeLinkDestination(src)})`; + result += `![${alt}](${formatLinkDestination(escapeLinkDestination(src))})`; break; } case "video": { @@ -868,7 +880,7 @@ function serializeInlineContent(el: Element): string { childEl.getAttribute("data-name") || childEl.getAttribute("title") || ""; - result += `![${name}](${escapeLinkDestination(src)})`; + result += `![${name}](${formatLinkDestination(escapeLinkDestination(src))})`; break; } case "p": diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/urlWithNoBreakSpace.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/urlWithNoBreakSpace.json new file mode 100644 index 0000000000..72955e98d4 --- /dev/null +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/urlWithNoBreakSpace.json @@ -0,0 +1,25 @@ +[ + { + "children": [], + "content": [ + { + "content": [ + { + "styles": {}, + "text": "Link", + "type": "text", + }, + ], + "href": "https://example.com/my file.pdf", + "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/__snapshots__/markdown/markdown/urlWithSpace.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/urlWithSpace.json new file mode 100644 index 0000000000..c73946d7c0 --- /dev/null +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/urlWithSpace.json @@ -0,0 +1,53 @@ +[ + { + "children": [], + "content": [ + { + "content": [ + { + "styles": {}, + "text": "Link", + "type": "text", + }, + ], + "href": "https://example.com/my file.pdf", + "type": "link", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "Example", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/my%20image.png", + }, + "type": "image", + }, + { + "children": [], + "content": undefined, + "id": "3", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "Example", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/my video.mp4", + }, + "type": "video", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/urlWithSpaceInTableCell.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/urlWithSpaceInTableCell.json new file mode 100644 index 0000000000..8cbd23f312 --- /dev/null +++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/urlWithSpaceInTableCell.json @@ -0,0 +1,65 @@ +[ + { + "children": [], + "content": { + "columnWidths": [ + undefined, + undefined, + ], + "headerCols": undefined, + "headerRows": undefined, + "rows": [ + { + "cells": [ + { + "content": [ + { + "content": [ + { + "styles": {}, + "text": "Link", + "type": "text", + }, + ], + "href": "https://example.com/my file.pdf", + "type": "link", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "styles": {}, + "text": "Cell 2", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + ], + }, + ], + "type": "tableContent", + }, + "id": "1", + "props": { + "textColor": "default", + }, + "type": "table", + }, +] \ 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 0a8b47887e..db5133aefc 100644 --- a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts +++ b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts @@ -858,6 +858,91 @@ export const exportParseEqualityTestInstancesMarkdown: TestInstance< }, executeTest: testExportParseEqualityMarkdown, }, + { + // A space ends the URL of a markdown link or image, so the export must + // not write such a URL as it is. The image URL comes back with `%20` for + // the space, because the HTML parse of an image reads the `src` property. + testCase: { + name: "markdown/urlWithSpace", + content: [ + { + type: "paragraph", + content: [ + { + type: "link", + content: "Link", + href: "https://example.com/my file.pdf", + }, + ], + }, + { + type: "image", + props: { + url: "https://example.com/my image.png", + name: "Example", + }, + }, + { + type: "video", + props: { + url: "https://example.com/my video.mp4", + name: "Example", + }, + }, + ], + }, + executeTest: testExportParseEqualityMarkdown, + }, + { + // The angle brackets of a URL with a space must not change the cells of + // a table row. + testCase: { + name: "markdown/urlWithSpaceInTableCell", + content: [ + { + type: "table", + content: { + type: "tableContent", + rows: [ + { + cells: [ + [ + { + type: "link", + content: "Link", + href: "https://example.com/my file.pdf", + }, + ], + "Cell 2", + ], + }, + ], + }, + }, + ], + }, + executeTest: testExportParseEqualityMarkdown, + }, + { + // The markdown parser reads a no-break space as a part of the URL, so + // the export writes this URL as it is, without angle brackets. + testCase: { + name: "markdown/urlWithNoBreakSpace", + content: [ + { + type: "paragraph", + content: [ + { + type: "link", + content: "Link", + href: "https://example.com/my\u00a0file.pdf", + }, + ], + }, + ], + }, + executeTest: testExportParseEqualityMarkdown, + }, { testCase: { name: "markdown/table",