From 3466e1d6df712e1702b88e3a5245adf9273c6096 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 16:02:22 +0100 Subject: [PATCH 1/3] fix(core): a file block with a caption stays one block in external HTML The external HTML of a file block with a caption was a `div` with the link and the caption in a paragraph. An image, a video or an audio block without a preview, with a caption, had the same form. The HTML parse and the markdown parse read that as two paragraphs: the file block was lost. Now `createLinkWithCaption` calls `createFigureWithCaption`. The export is a figure with the link and a `figcaption`, the form of an image with a caption. The new function `parseLinkFigure` reads that figure with `parseFigureElement`. The file parse rule calls it and gives one file block. The markdown export calls it too, and keeps the figure as HTML with the function that writes an image with a caption. `parseLinkFigure` reads only the form that the export writes: the link, then the `figcaption`, each with only text in it, and no other content. Each other figure gives the same blocks and the same markdown as before, thus no text is lost. A figure with a link and without a caption is not a file block, because the export of a file block without a caption is a link in a paragraph. A figure with only a link does not tell the block type. Thus, an image, a video or an audio block without a preview reads back as a file block. Test changes: 4 HTML and 4 markdown export snapshots change from the `div` form to the figure form. The snapshot of `@blocknote/server-util` changes too: its image without a preview reads back as one file block. --- .../api/exporters/markdown/htmlToMarkdown.ts | 19 ++- packages/core/src/blocks/File/block.test.ts | 160 ++++++++++++++++++ packages/core/src/blocks/File/block.ts | 8 + .../File/helpers/parse/parseLinkFigure.ts | 37 ++++ .../toExternalHTML/createLinkWithCaption.ts | 19 +-- .../ServerBlockNoteEditor.test.ts.snap | 82 ++------- .../export/__snapshots__/html/file/basic.html | 6 +- .../__snapshots__/html/file/nested.html | 12 +- .../__snapshots__/html/file/noName.html | 6 +- .../__snapshots__/html/image/noPreview.html | 6 +- .../__snapshots__/markdown/file/basic.md | 4 +- .../__snapshots__/markdown/file/nested.md | 8 +- .../__snapshots__/markdown/file/noName.md | 4 +- .../__snapshots__/markdown/image/noPreview.md | 4 +- 14 files changed, 259 insertions(+), 116 deletions(-) create mode 100644 packages/core/src/blocks/File/helpers/parse/parseLinkFigure.ts diff --git a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts index f99b3d36dc..4edbafabf1 100644 --- a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts +++ b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts @@ -1,3 +1,4 @@ +import { parseLinkFigure } from "../../../blocks/File/helpers/parse/parseLinkFigure.js"; import { parseAutolinkLiteral } from "../../parsers/markdown/autolink.js"; import { wrapWithDelimiter } from "./wrapWithDelimiter.js"; @@ -626,6 +627,16 @@ function serializeFigure(el: HTMLElement, ctx: SerializeContext): string { ctx, ); } + const linkFigure = parseLinkFigure(el); + if (linkFigure) { + return serializeMediaFigure( + "a", + linkFigure.url, + linkFigure.name || linkFigure.url, + linkFigure.caption, + ctx, + ); + } if (link) { return serializeBlockLink(link as HTMLElement, ctx); } @@ -633,7 +644,7 @@ function serializeFigure(el: HTMLElement, ctx: SerializeContext): string { } function serializeMediaFigure( - kind: "img" | "video" | "audio", + kind: "img" | "video" | "audio" | "a", src: string, descriptor: string, captionText: string, @@ -644,7 +655,7 @@ function serializeMediaFigure( } // No caption + has a markdown shorthand → use it. - if (!captionText && kind !== "audio") { + if (!captionText && (kind === "img" || kind === "video")) { return ctx.indent + `![${descriptor}](${src})\n\n`; } @@ -664,7 +675,9 @@ function serializeMediaFigure( const tag = kind === "img" ? `` - : `<${kind} src="${escapeHtmlAttr(src)}"${descAttr} controls>`; + : kind === "a" + ? `${escapeHtmlText(descriptor)}` + : `<${kind} src="${escapeHtmlAttr(src)}"${descAttr} controls>`; const captionPart = captionText ? `
${escapeHtmlText(captionText)}
` diff --git a/packages/core/src/blocks/File/block.test.ts b/packages/core/src/blocks/File/block.test.ts index 9a506252b2..6b3c16fc45 100644 --- a/packages/core/src/blocks/File/block.test.ts +++ b/packages/core/src/blocks/File/block.test.ts @@ -1,5 +1,7 @@ import { describe, expect } from "vite-plus/test"; +import { cleanHTMLToMarkdown } from "../../api/exporters/markdown/markdownExporter.js"; + describe("external HTML of a file block without a caption", () => { // The export was a link alone. A link is inline content: the HTML parse // joined two adjacent links into one paragraph. @@ -25,3 +27,161 @@ describe("external HTML of a file block without a caption", () => { ]); }); }); + +describe("external HTML of a file block with a caption", () => { + const url = "https://example.com/a.pdf"; + const htmlCharacters = { + url: `${url}?a="1"&b=<2>`, + name: `a & "b".pdf`, + caption: `Text & "more"`, + }; + + // The export was a `div` with the link and the caption in a paragraph. The + // HTML parse and the markdown parse read that as two paragraphs. + it.for([ + { format: "HTML", title: "a name", props: { name: "a.pdf" } }, + { format: "HTML", title: "no name", props: { name: "" } }, + { format: "HTML", title: "HTML characters", props: htmlCharacters }, + { format: "markdown", title: "a name", props: { name: "a.pdf" } }, + { format: "markdown", title: "no name", props: { name: "" } }, + { format: "markdown", title: "HTML characters", props: htmlCharacters }, + ] as const)( + "reads back as the same block from $format, with $title", + async ({ format, props: caseProps }, { setupEditor }) => { + const { editor } = await setupEditor(); + const props = { url, caption: "Text", ...caseProps }; + + const blocks = + format === "HTML" + ? editor.tryParseHTMLToBlocks( + editor.blocksToHTMLLossy([{ type: "file", props }]), + ) + : editor.tryParseMarkdownToBlocks( + editor.blocksToMarkdownLossy([{ type: "file", props }]), + ); + + expect(blocks).toMatchObject([{ type: "file", props }]); + }, + ); + + it.for([ + { + figure: "white space and a comment", + html: `
+ + Name + + +
Text
+
`, + props: { url: "a.pdf", name: "Name", caption: "Text" }, + }, + { + figure: "a background color", + html: `
Name
Text
`, + props: { + url: "a.pdf", + name: "Name", + caption: "Text", + backgroundColor: "red", + }, + }, + { + figure: "the URL as link text on its own line", + html: `
+ ${url} +
Text
`, + props: { url, name: "", caption: "Text" }, + }, + ])( + "reads a figure with $figure as a file block", + async ({ html, props }, { setupEditor }) => { + const { editor } = await setupEditor(); + + expect(editor.tryParseHTMLToBlocks(html)).toMatchObject([ + { type: "file", props }, + ]); + }, + ); + + // Only the form that the export writes is a file block. Each other figure + // gives the same blocks and the same markdown as before. + it.for([ + { + figure: "a link that wraps an image", + html: `
Text
`, + types: ["image"], + markdown: `
Text
`, + }, + { + figure: "a link without a caption", + html: `
a
`, + types: ["paragraph"], + markdown: "[a](a.pdf)", + }, + { + figure: "a link and an empty caption", + html: `
a
`, + types: ["paragraph"], + markdown: "[a](a.pdf)", + }, + { + figure: "a link without a URL", + html: `
a
Text
`, + types: ["paragraph", "paragraph"], + markdown: "a", + }, + { + figure: "a link and more text", + html: `
a and more text
Text
`, + types: ["paragraph", "paragraph"], + markdown: "[a](a.pdf)", + }, + { + figure: "two links", + html: `
ab
Text
`, + types: ["paragraph", "paragraph"], + markdown: "[a](a.pdf)", + }, + { + figure: "the caption before the link", + html: `
Text
a
`, + types: ["paragraph", "paragraph"], + markdown: "[a](a.pdf)", + }, + { + figure: "a link in a paragraph", + html: `

See a here

Text
`, + types: ["paragraph", "paragraph"], + markdown: "[a](a.pdf)", + }, + { + figure: "paragraphs in the link", + html: `

One

Two

Text
`, + types: ["paragraph", "paragraph", "paragraph"], + markdown: "[OneTwo](a.pdf)", + }, + { + figure: "a link in the caption", + html: `
a
See b
`, + types: ["paragraph", "paragraph"], + markdown: "[a](a.pdf)", + }, + { + figure: "a quote, and a link in the caption", + html: `
Quote
By b
`, + types: ["quote", "paragraph"], + markdown: "[b](b.pdf)", + }, + ])( + "does not read a figure with $figure as a file block", + async ({ html, types, markdown }, { setupEditor }) => { + const { editor } = await setupEditor(); + + expect( + editor.tryParseHTMLToBlocks(html).map((block) => block.type), + ).toEqual(types); + expect(cleanHTMLToMarkdown(html)).toBe(`${markdown}\n`); + }, + ); +}); diff --git a/packages/core/src/blocks/File/block.ts b/packages/core/src/blocks/File/block.ts index 1be06e7a02..a3d609add9 100644 --- a/packages/core/src/blocks/File/block.ts +++ b/packages/core/src/blocks/File/block.ts @@ -2,6 +2,7 @@ import { createBlockConfig, createBlockSpec } from "../../schema/index.js"; import { defaultProps, parseDefaultProps } from "../defaultProps.js"; import { parseEmbedElement } from "./helpers/parse/parseEmbedElement.js"; import { parseFigureElement } from "./helpers/parse/parseFigureElement.js"; +import { parseLinkFigure } from "./helpers/parse/parseLinkFigure.js"; import { createFileBlockWrapper } from "./helpers/render/createFileBlockWrapper.js"; import { createLinkWithCaption } from "./helpers/toExternalHTML/createLinkWithCaption.js"; @@ -46,6 +47,13 @@ export const fileParse = () => (element: HTMLElement) => { } if (element.tagName === "FIGURE") { + const linkFigure = parseLinkFigure(element); + if (linkFigure) { + const { backgroundColor } = parseDefaultProps(element); + + return { ...linkFigure, backgroundColor }; + } + const parsedFigure = parseFigureElement(element, "embed"); if (!parsedFigure) { return undefined; diff --git a/packages/core/src/blocks/File/helpers/parse/parseLinkFigure.ts b/packages/core/src/blocks/File/helpers/parse/parseLinkFigure.ts new file mode 100644 index 0000000000..ce862b08e1 --- /dev/null +++ b/packages/core/src/blocks/File/helpers/parse/parseLinkFigure.ts @@ -0,0 +1,37 @@ +import { parseFigureElement } from "./parseFigureElement.js"; + +/** + * Reads the figure that `createLinkWithCaption` writes: a link to the file, + * then a caption, each with only text in it, and no other content. Each other + * figure gives `undefined`. A figure without a caption is one of these: the + * export without a caption is a link in a paragraph, which is not a file + * block. + */ +export function parseLinkFigure(figure: HTMLElement) { + const parsedFigure = parseFigureElement(figure, "a"); + const url = parsedFigure?.targetElement.getAttribute("href"); + if ( + !parsedFigure?.caption || + !url || + figure.children.length !== 2 || + figure.lastElementChild?.tagName !== "FIGCAPTION" || + // An element in the link or in the caption. + figure.querySelector(":scope > * > *") || + // Text next to them. White space and comments do not count. + Array.from(figure.childNodes).some( + (node) => + node.nodeType === 3 /* Node.TEXT_NODE */ && node.textContent?.trim(), + ) + ) { + return undefined; + } + + // The export writes the URL as the link text of a block without a name. + const name = parsedFigure.targetElement.textContent.trim(); + + return { + url, + name: name === url ? "" : name, + caption: parsedFigure.caption, + }; +} diff --git a/packages/core/src/blocks/File/helpers/toExternalHTML/createLinkWithCaption.ts b/packages/core/src/blocks/File/helpers/toExternalHTML/createLinkWithCaption.ts index fcf0abbe9b..1bd319ad46 100644 --- a/packages/core/src/blocks/File/helpers/toExternalHTML/createLinkWithCaption.ts +++ b/packages/core/src/blocks/File/helpers/toExternalHTML/createLinkWithCaption.ts @@ -1,8 +1,10 @@ +import { createFigureWithCaption } from "./createFigureWithCaption.js"; + /** * Puts the link to the file of a block into a block-level element. With a - * caption, this is a `div` with the link and the caption in a paragraph. - * Without a caption, this is a paragraph. A link alone is inline content: the - * HTML parse joins two adjacent links into one paragraph. + * caption, this is a figure, as for an image with a caption. Without a + * caption, this is a paragraph. A link alone is inline content: the HTML + * parse joins two adjacent links into one paragraph. */ export const createLinkWithCaption = ( element: HTMLElement, @@ -17,14 +19,5 @@ export const createLinkWithCaption = ( }; } - const wrapper = document.createElement("div"); - const fileCaption = document.createElement("p"); - fileCaption.textContent = caption; - - wrapper.appendChild(element); - wrapper.appendChild(fileCaption); - - return { - dom: wrapper, - }; + return createFigureWithCaption(element, caption); }; diff --git a/packages/server-util/src/context/__snapshots__/ServerBlockNoteEditor.test.ts.snap b/packages/server-util/src/context/__snapshots__/ServerBlockNoteEditor.test.ts.snap index db9aa56560..b38a966ede 100644 --- a/packages/server-util/src/context/__snapshots__/ServerBlockNoteEditor.test.ts.snap +++ b/packages/server-util/src/context/__snapshots__/ServerBlockNoteEditor.test.ts.snap @@ -2,7 +2,7 @@ exports[`Test ServerBlockNoteEditor > converts to HTML (blocksToFullHTML) 1`] = `"

Heading 2

Paragraph

list item

Example
Caption

Example

Caption
"`; -exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLossy) 1`] = `"

Heading 2

Paragraph

Example
Caption
Example

Caption

"`; +exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLossy) 1`] = `"

Heading 2

Paragraph

Example
Caption
Example
Caption
"`; exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLossy) 2`] = ` [ @@ -87,43 +87,15 @@ exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLos }, { "children": [], - "content": [ - { - "content": [ - { - "styles": {}, - "text": "Example", - "type": "text", - }, - ], - "href": "exampleURL", - "type": "link", - }, - ], + "content": undefined, "id": "4", "props": { "backgroundColor": "default", - "textAlignment": "left", - "textColor": "default", - }, - "type": "paragraph", - }, - { - "children": [], - "content": [ - { - "styles": {}, - "text": "Caption", - "type": "text", - }, - ], - "id": "5", - "props": { - "backgroundColor": "default", - "textAlignment": "left", - "textColor": "default", + "caption": "Caption", + "name": "Example", + "url": "exampleURL", }, - "type": "paragraph", + "type": "file", }, ] `; @@ -137,9 +109,7 @@ Paragraph
Example
Caption
-[Example](exampleURL) - -Caption +
Example
Caption
" `; @@ -229,43 +199,15 @@ exports[`Test ServerBlockNoteEditor > converts to and from markdown (blocksToMar }, { "children": [], - "content": [ - { - "content": [ - { - "styles": {}, - "text": "Example", - "type": "text", - }, - ], - "href": "exampleURL", - "type": "link", - }, - ], + "content": undefined, "id": "4", "props": { "backgroundColor": "default", - "textAlignment": "left", - "textColor": "default", - }, - "type": "paragraph", - }, - { - "children": [], - "content": [ - { - "styles": {}, - "text": "Caption", - "type": "text", - }, - ], - "id": "5", - "props": { - "backgroundColor": "default", - "textAlignment": "left", - "textColor": "default", + "caption": "Caption", + "name": "Example", + "url": "exampleURL", }, - "type": "paragraph", + "type": "file", }, ] `; diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/basic.html b/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/basic.html index 22fe1c373a..3ea45fd647 100644 --- a/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/basic.html +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/basic.html @@ -1,4 +1,4 @@ -
+
example -

Caption

-
\ No newline at end of file +
Caption
+ \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/nested.html b/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/nested.html index 768e2a8359..721859a217 100644 --- a/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/nested.html +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/nested.html @@ -1,13 +1,13 @@ -
+
example -

Caption

-
-
Caption + +
example -

Caption

-
\ No newline at end of file +
Caption
+ \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/noName.html b/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/noName.html index f0ac923a66..52769c3219 100644 --- a/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/noName.html +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/html/file/noName.html @@ -1,4 +1,4 @@ -
+
exampleURL -

Caption

-
\ No newline at end of file +
Caption
+ \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/html/image/noPreview.html b/tests/src/unit/core/formatConversion/export/__snapshots__/html/image/noPreview.html index 21bf2faaf1..315fa602ea 100644 --- a/tests/src/unit/core/formatConversion/export/__snapshots__/html/image/noPreview.html +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/html/image/noPreview.html @@ -1,4 +1,4 @@ -
example -

Caption

-
\ No newline at end of file +
Caption
+ \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/basic.md b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/basic.md index 3acd9e1ad0..6ccde39bf2 100644 --- a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/basic.md +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/basic.md @@ -1,3 +1 @@ -[example](exampleURL) - -Caption +
example
Caption
diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/nested.md b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/nested.md index 93a61da7b4..a72239439c 100644 --- a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/nested.md +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/nested.md @@ -1,7 +1,3 @@ -[example](exampleURL) +
example
Caption
-Caption - -[example](exampleURL) - -Caption +
example
Caption
diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/noName.md b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/noName.md index 4cca42f87d..77e1060f3b 100644 --- a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/noName.md +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/file/noName.md @@ -1,3 +1 @@ -exampleURL - -Caption +
exampleURL
Caption
diff --git a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/image/noPreview.md b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/image/noPreview.md index 3acd9e1ad0..6ccde39bf2 100644 --- a/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/image/noPreview.md +++ b/tests/src/unit/core/formatConversion/export/__snapshots__/markdown/image/noPreview.md @@ -1,3 +1 @@ -[example](exampleURL) - -Caption +
example
Caption
From 68b92b12c6c601738a39ee8cae3ca0dbc76c1204 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 16:02:23 +0100 Subject: [PATCH 2/3] fix(react): a React file block with a caption stays one block in external HTML `LinkWithCaption` wrote a `div` with the link and the caption in a paragraph, as the core helper did. The parse read that as two paragraphs. Now `LinkWithCaption` renders `FigureWithCaption` when the block has a caption. The export is the same figure as the export of the core block, and the file parse rule reads it back as one file block. Test changes: 4 HTML export snapshots change from the `div` form to the figure form. --- packages/react/src/blocks/File/block.test.tsx | 32 +++++++++++++++++++ .../toExternalHTML/LinkWithCaption.tsx | 15 ++++----- .../__snapshots__/html/reactFile/basic.html | 6 ++-- .../__snapshots__/html/reactFile/nested.html | 12 +++---- .../__snapshots__/html/reactFile/noName.html | 6 ++-- .../html/reactImage/noPreview.html | 6 ++-- 6 files changed, 53 insertions(+), 24 deletions(-) diff --git a/packages/react/src/blocks/File/block.test.tsx b/packages/react/src/blocks/File/block.test.tsx index e60cd0e6d8..f0c8c27f12 100644 --- a/packages/react/src/blocks/File/block.test.tsx +++ b/packages/react/src/blocks/File/block.test.tsx @@ -32,3 +32,35 @@ describe("external HTML of a React file block without a caption", () => { ]); }); }); + +describe("external HTML of a React file block with a caption", () => { + // The export was a `div` with the link and the caption in a paragraph. The + // HTML parse and the markdown parse read that as two paragraphs. + it.for([ + { format: "HTML", name: "a.pdf" }, + { format: "HTML", name: "" }, + { format: "markdown", name: "a.pdf" }, + { format: "markdown", name: "" }, + ] as const)( + "reads back as the same block from $format, with the name '$name'", + async ({ format, name }, { setupEditor }) => { + const { editor } = await setupEditor({ + schema: BlockNoteSchema.create().extend({ + blockSpecs: { file: ReactFileBlock() }, + }), + }); + const props = { url: "https://example.com/a.pdf", name, caption: "Text" }; + + const blocks = + format === "HTML" + ? editor.tryParseHTMLToBlocks( + editor.blocksToHTMLLossy([{ type: "file", props }]), + ) + : editor.tryParseMarkdownToBlocks( + editor.blocksToMarkdownLossy([{ type: "file", props }]), + ); + + expect(blocks).toMatchObject([{ type: "file", props }]); + }, + ); +}); diff --git a/packages/react/src/blocks/File/helpers/toExternalHTML/LinkWithCaption.tsx b/packages/react/src/blocks/File/helpers/toExternalHTML/LinkWithCaption.tsx index af4d444dbf..4ecbf9f588 100644 --- a/packages/react/src/blocks/File/helpers/toExternalHTML/LinkWithCaption.tsx +++ b/packages/react/src/blocks/File/helpers/toExternalHTML/LinkWithCaption.tsx @@ -1,10 +1,12 @@ import { ReactNode } from "react"; +import { FigureWithCaption } from "./FigureWithCaption.js"; + /** * Puts the link to the file of a block into a block-level element. With a - * caption, this is a `div` with the link and the caption in a paragraph. - * Without a caption, this is a paragraph. A link alone is inline content: the - * HTML parse joins two adjacent links into one paragraph. + * caption, this is a figure, as for an image with a caption. Without a + * caption, this is a paragraph. A link alone is inline content: the HTML + * parse joins two adjacent links into one paragraph. */ export const LinkWithCaption = (props: { caption: string; @@ -14,10 +16,5 @@ export const LinkWithCaption = (props: { return

{props.children}

; } - return ( -
- {props.children} -

{props.caption}

-
- ); + return ; }; diff --git a/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/basic.html b/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/basic.html index 22fe1c373a..3ea45fd647 100644 --- a/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/basic.html +++ b/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/basic.html @@ -1,4 +1,4 @@ -
+
example -

Caption

-
\ No newline at end of file +
Caption
+ \ No newline at end of file diff --git a/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/nested.html b/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/nested.html index 768e2a8359..721859a217 100644 --- a/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/nested.html +++ b/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/nested.html @@ -1,13 +1,13 @@ -
+
example -

Caption

-
-
Caption + +
example -

Caption

-
\ No newline at end of file +
Caption
+ \ No newline at end of file diff --git a/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/noName.html b/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/noName.html index f0ac923a66..52769c3219 100644 --- a/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/noName.html +++ b/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactFile/noName.html @@ -1,4 +1,4 @@ -
+
exampleURL -

Caption

-
\ No newline at end of file +
Caption
+ \ No newline at end of file diff --git a/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactImage/noPreview.html b/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactImage/noPreview.html index 21bf2faaf1..315fa602ea 100644 --- a/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactImage/noPreview.html +++ b/tests/src/unit/react/formatConversion/export/__snapshots__/html/reactImage/noPreview.html @@ -1,4 +1,4 @@ -
example -

Caption

-
\ No newline at end of file +
Caption
+ \ No newline at end of file From b2ab47fefd62a61d52b2f32ac08b589cfe1a4c89 Mon Sep 17 00:00:00 2001 From: yousefed Date: Mon, 23 Nov 2026 10:39:19 +0100 Subject: [PATCH 3/3] fix(core): a figure caption with a line feed stays one figure in markdown The markdown export writes a figure with a caption as raw HTML, and the line feeds of the caption were in that text as they are. The markdown parse ends a raw HTML block at an empty line. Thus, a caption with an empty line split the figure: the rest of the caption became a paragraph, and a file block with such a caption read back as paragraphs. Now `serializeMediaFigure` writes each line feed of the caption as ` `, so the figure stays on one line. This is the same for a file, an image, a video and an audio figure. The HTML parse reads a line feed in text as a space, thus the caption reads back with one space in place of the line feeds. A caption on one line gives the same markdown as before. --- .../api/exporters/markdown/htmlToMarkdown.ts | 4 +++- packages/core/src/blocks/File/block.test.ts | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts index 4edbafabf1..33328c4d6f 100644 --- a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts +++ b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts @@ -679,8 +679,10 @@ function serializeMediaFigure( ? `${escapeHtmlText(descriptor)}` : `<${kind} src="${escapeHtmlAttr(src)}"${descAttr} controls>`; + // A line feed is a character reference, so that the figure stays on one + // line: an empty line in the caption would end the HTML block in markdown. const captionPart = captionText - ? `
${escapeHtmlText(captionText)}
` + ? `
${escapeHtmlText(captionText).replace(/\n/g, " ")}
` : ""; return ctx.indent + `
${tag}${captionPart}
\n\n`; } diff --git a/packages/core/src/blocks/File/block.test.ts b/packages/core/src/blocks/File/block.test.ts index 6b3c16fc45..d1a4184c8a 100644 --- a/packages/core/src/blocks/File/block.test.ts +++ b/packages/core/src/blocks/File/block.test.ts @@ -185,3 +185,24 @@ describe("external HTML of a file block with a caption", () => { }, ); }); + +describe("markdown of a figure with a caption", () => { + // The caption is raw HTML in markdown. An empty line in it ended the HTML + // block: the rest of the caption became a paragraph. + it.for(["file", "image"] as const)( + "keeps a %s block as one block, with an empty line and HTML characters in the caption", + async (type, { setupEditor }) => { + const { editor } = await setupEditor(); + const url = "https://example.com/a.png"; + + const markdown = editor.blocksToMarkdownLossy([ + { type, props: { url, caption: "One\n\nTwo & three" } }, + ]); + + // The HTML parse reads a line feed in text as a space. + expect(editor.tryParseMarkdownToBlocks(markdown)).toMatchObject([ + { type, props: { url, caption: "One Two & three" } }, + ]); + }, + ); +});