Text`,
+ types: ["paragraph", "paragraph", "paragraph"],
+ markdown: "[OneTwo](a.pdf)",
+ },
+ {
+ figure: "a link in the caption",
+ html: `aSee 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`);
+ },
+ );
+});
+
+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" } },
+ ]);
+ },
+ );
+});
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/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
\ 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 @@
-
\ 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 @@
-
\ 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 @@
-
\ 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 @@
-
\ 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 @@
-
\ 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 @@
-