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 44be7074dc..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"; /** @@ -25,6 +26,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 @@ -111,9 +115,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 +125,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 +143,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 +152,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 +327,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 +352,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 +382,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 @@ -459,8 +459,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); @@ -568,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 { @@ -577,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 { @@ -598,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 { @@ -654,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 @@ -726,7 +742,7 @@ function formatLink(text: string, href: string): string { return href; } } - return `[${text}](${escapeLinkDestination(href)})`; + return `[${text}](${formatLinkDestination(escapeLinkDestination(href))})`; } /** @@ -783,6 +799,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 || ""); @@ -831,7 +849,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 @@ -850,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": { @@ -862,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": @@ -882,6 +900,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 @@ -901,6 +954,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. @@ -908,7 +971,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__/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/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 +
+

+ +
    +
  1. +

    + Numbered List Item +
    +

    +
  2. +
+ +
+ Quote +
+
+ \ 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/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__/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__/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/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/__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..1465b80857 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: { @@ -2920,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/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/__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/__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/__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 b351380003..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", @@ -906,6 +991,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", @@ -936,6 +1075,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", @@ -1267,6 +1456,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",