From 8f465bb21a819155ca2f09ae155e2fc46fb07cc7 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 15:56:47 +0100 Subject: [PATCH 01/13] fix(core): keep an image that is in the text of a paragraph or a quote BlockNote has no inline images: an image is a block. The HTML parser dropped an image that was in a `

` or a `

` together with text. The markdown parser makes HTML first, so `Text ![alt](url) more` also lost its image, in a paragraph, in a quote and in a list item. The cause: the content of these blocks is parsed as inline content only, and ProseMirror has no place for an image block there. Before ProseMirror parses the HTML, each paragraph and each quote that has an image now splits around the image: the text before, the image, the text after. The parts are copies of the element, so they keep its type, its alignment and its colors. A part without text is removed. In a list item, the image and the text after it become children of the list item, as for an image that is directly in an `
  • `. A link or a style that holds the image splits in the same way. The image does not keep the link. --- .../core/src/api/parsers/html/parseHTML.ts | 2 + .../src/api/parsers/html/util/imagesInText.ts | 32 +++++++++ .../html/inlineImageInListItemParagraph.json | 68 +++++++++++++++++++ .../html/inlineImageInParagraph.json | 51 ++++++++++++++ .../html/inlineImageInQuote.json | 48 +++++++++++++ .../html/inlineImageInStyledParagraph.json | 57 ++++++++++++++++ .../__snapshots__/markdown/inlineImage.json | 33 ++++++++- .../markdown/inlineImageInListItem.json | 51 ++++++++++++++ .../markdown/inlineImageInQuote.json | 48 +++++++++++++ .../parse/parseTestInstances.ts | 63 ++++++++++++++++- 10 files changed, 451 insertions(+), 2 deletions(-) create mode 100644 packages/core/src/api/parsers/html/util/imagesInText.ts create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraph.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInParagraph.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInQuote.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInStyledParagraph.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItem.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInQuote.json diff --git a/packages/core/src/api/parsers/html/parseHTML.ts b/packages/core/src/api/parsers/html/parseHTML.ts index cfe1a49ba3..05a4d02c31 100644 --- a/packages/core/src/api/parsers/html/parseHTML.ts +++ b/packages/core/src/api/parsers/html/parseHTML.ts @@ -11,6 +11,7 @@ import { EMPTY_BLOCK_PLACEHOLDER } from "../../exporters/html/util/serializeBloc import { nodeToBlock } from "../../nodeConversions/nodeToBlock.js"; import { nestedListsToBlockNoteStructure } from "./util/nestedLists.js"; import { preprocessHTMLWhitespace } from "./util/normalizeWhitespace.js"; +import { moveImagesOutOfText } from "./util/imagesInText.js"; /** * Removes the placeholder character that the external HTML exporter inserts @@ -76,6 +77,7 @@ export function HTMLToBlocks< >(html: string, pmSchema: Schema): Block[] { const htmlNode = nestedListsToBlockNoteStructure(html); preprocessHTMLWhitespace(htmlNode); + moveImagesOutOfText(htmlNode); const parser = DOMParser.fromSchema(pmSchema); // Other approach might be to use diff --git a/packages/core/src/api/parsers/html/util/imagesInText.ts b/packages/core/src/api/parsers/html/util/imagesInText.ts new file mode 100644 index 0000000000..fb18a6c6aa --- /dev/null +++ b/packages/core/src/api/parsers/html/util/imagesInText.ts @@ -0,0 +1,32 @@ +import { detachedDoc } from "../../../../util/detachedDoc.js"; + +// The elements with text that split around an image. +const TEXT_BLOCKS = "p, blockquote"; + +/** + * Moves each image out of the text that holds it. An image is a block, and + * ProseMirror drops a block that is in the text of a block. + * + * A paragraph and a quote split around the image, and the parts keep the + * attributes of the element. + */ +export function moveImagesOutOfText(root: HTMLElement) { + for (const image of root.querySelectorAll("img")) { + // Splits the parent of the image until no text block holds the image: a + // copy of the parent takes the content before the image. + while (image.parentElement?.closest(TEXT_BLOCKS)) { + const parent = image.parentElement; + const before = detachedDoc().importNode(parent, false); + while (image.previousSibling) { + before.prepend(image.previousSibling); + } + parent.before(before, image); + // A part without text is not a block. Its other content stays. + for (const part of [before, parent]) { + if (!part.textContent?.trim()) { + part.replaceWith(...part.childNodes); + } + } + } + } +} diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraph.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraph.json new file mode 100644 index 0000000000..0fd16ff289 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraph.json @@ -0,0 +1,68 @@ +[ + { + "children": [ + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "The menu", + "showPreview": true, + "textAlignment": "left", + "url": "https://github.com/octo/repo/raw/main/docs/menu.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": " and select a block", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Open the menu ", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Close the menu", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInParagraph.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInParagraph.json new file mode 100644 index 0000000000..5f6eb71756 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInParagraph.json @@ -0,0 +1,51 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "We arrived at the harbor before dusk. ", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "The harbor at dusk", + "previewWidth": 300, + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/wp-content/uploads/harbor-300x200.jpg", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": " The boats were in.", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInQuote.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInQuote.json new file mode 100644 index 0000000000..d439ad5ccb --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInQuote.json @@ -0,0 +1,48 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "As the picture shows ", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textColor": "default", + }, + "type": "quote", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "A diagram", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/diagram.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": " the result is good.", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textColor": "default", + }, + "type": "quote", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInStyledParagraph.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInStyledParagraph.json new file mode 100644 index 0000000000..375045c1cf --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInStyledParagraph.json @@ -0,0 +1,57 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Before ", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "yellow", + "textAlignment": "center", + "textColor": "red", + }, + "type": "paragraph", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "The harbor", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/harbor.jpg", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": " and ", + "type": "text", + }, + { + "styles": { + "bold": true, + }, + "text": "after", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "yellow", + "textAlignment": "center", + "textColor": "red", + }, + "type": "paragraph", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImage.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImage.json index 35a0bc23d1..d294770df9 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImage.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImage.json @@ -4,7 +4,7 @@ "content": [ { "styles": {}, - "text": "Text before text after", + "text": "Text before ", "type": "text", }, ], @@ -16,4 +16,35 @@ }, "type": "paragraph", }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "inline", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/img.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": " text after", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, ] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItem.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItem.json new file mode 100644 index 0000000000..7fd3d43b75 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItem.json @@ -0,0 +1,51 @@ +[ + { + "children": [ + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "inline", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/img.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": " text after", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Item ", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInQuote.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInQuote.json new file mode 100644 index 0000000000..b2ae0b0a69 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInQuote.json @@ -0,0 +1,48 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Quote ", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textColor": "default", + }, + "type": "quote", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "inline", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/img.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": " text after", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textColor": "default", + }, + "type": "quote", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index f907e0a38b..99540ea7c7 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -713,6 +713,49 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // An image in the text of a paragraph, as a blog writes it. An image is a + // block, so the paragraph splits around the image. + { + testCase: { + name: "inlineImageInParagraph", + content: `

    We arrived at the harbor before dusk. The harbor at dusk The boats were in.

    `, + }, + executeTest: testParseHTML, + }, + // The parts of the paragraph keep its alignment and its colors. + { + testCase: { + name: "inlineImageInStyledParagraph", + content: `

    Before The harbor and after

    `, + }, + executeTest: testParseHTML, + }, + // An image in the paragraph of a list item, as GitHub writes it (the image + // is in a link). The image and the text after it become children of the + // list item. + { + testCase: { + name: "inlineImageInListItemParagraph", + content: `
      +
    • +

      Open the menu The menu and select a block

      +
    • +
    • +

      Close the menu

      +
    • +
    `, + }, + executeTest: testParseHTML, + }, + // An image in a quote, as a markdown renderer writes it. The quote splits + // around the image. + { + testCase: { + name: "inlineImageInQuote", + content: `

    As the picture shows A diagram the result is good.

    `, + }, + executeTest: testParseHTML, + }, { testCase: { name: "fakeImageCaption", @@ -2213,7 +2256,8 @@ Final paragraph with [a link](https://example.com).`, }, executeTest: testParseMarkdown, }, - // Inline image within text (should be handled) + // An image in the text of a paragraph. An image is a block, so the paragraph + // splits around the image. { testCase: { name: "inlineImage", @@ -2221,6 +2265,23 @@ Final paragraph with [a link](https://example.com).`, }, executeTest: testParseMarkdown, }, + // An image in the text of a list item: the image and the text after it + // become children of the list item. + { + testCase: { + name: "inlineImageInListItem", + content: `- Item ![inline](https://example.com/img.png) text after`, + }, + executeTest: testParseMarkdown, + }, + // An image in a quote: the quote splits around the image. + { + testCase: { + name: "inlineImageInQuote", + content: `> Quote ![inline](https://example.com/img.png) text after`, + }, + executeTest: testParseMarkdown, + }, // Code block immediately after heading { testCase: { From 8177126caab5bd3dd9a526bd2d590086712bb08c Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 15:56:49 +0100 Subject: [PATCH 02/13] fix(core): keep an image that is in the text of a heading The HTML parser and the markdown parser dropped an image that was in the text of a heading, for example `## Title ![alt](url)` or `

    Project

    `. The cause is the same as for a paragraph: the content of a heading is parsed as inline content only, and an image is a block. A heading now splits around the image, as a paragraph and a quote do. The parts are headings of the same level. An image at the start or at the end of the heading gives the image and one heading. An image between two texts gives two headings: `## A ![x](url) B` becomes the heading `A`, the image and the heading `B`. --- .../src/api/parsers/html/util/imagesInText.ts | 6 +-- .../html/inlineImageInHeading.json | 36 +++++++++++++ .../markdown/inlineImageInHeading.json | 54 +++++++++++++++++++ .../parse/parseTestInstances.ts | 19 +++++++ 4 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeading.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInHeading.json diff --git a/packages/core/src/api/parsers/html/util/imagesInText.ts b/packages/core/src/api/parsers/html/util/imagesInText.ts index fb18a6c6aa..07e96a7fd4 100644 --- a/packages/core/src/api/parsers/html/util/imagesInText.ts +++ b/packages/core/src/api/parsers/html/util/imagesInText.ts @@ -1,14 +1,14 @@ import { detachedDoc } from "../../../../util/detachedDoc.js"; // The elements with text that split around an image. -const TEXT_BLOCKS = "p, blockquote"; +const TEXT_BLOCKS = "p, blockquote, h1, h2, h3, h4, h5, h6"; /** * Moves each image out of the text that holds it. An image is a block, and * ProseMirror drops a block that is in the text of a block. * - * A paragraph and a quote split around the image, and the parts keep the - * attributes of the element. + * A paragraph, a quote and a heading split around the image, and the parts + * keep the attributes of the element. */ export function moveImagesOutOfText(root: HTMLElement) { for (const image of root.querySelectorAll("img")) { diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeading.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeading.json new file mode 100644 index 0000000000..c6aed71073 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeading.json @@ -0,0 +1,36 @@ +[ + { + "children": [], + "content": undefined, + "id": "1", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "Project logo", + "previewWidth": 32, + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/logo.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": " Project", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "isToggleable": false, + "level": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInHeading.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInHeading.json new file mode 100644 index 0000000000..68ef158a71 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInHeading.json @@ -0,0 +1,54 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Heading ", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "isToggleable": false, + "level": 2, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "inline", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/img.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": " text after", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "isToggleable": false, + "level": 2, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index 99540ea7c7..50c5d6bb64 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -756,6 +756,16 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // An image in a heading (a logo next to a title). The heading splits around + // the image. There is no text before the image, so there is no heading + // before it. + { + testCase: { + name: "inlineImageInHeading", + content: `

    Project logo Project

    `, + }, + executeTest: testParseHTML, + }, { testCase: { name: "fakeImageCaption", @@ -2282,6 +2292,15 @@ Final paragraph with [a link](https://example.com).`, }, executeTest: testParseMarkdown, }, + // An image in a heading: the heading splits around the image. Text before + // and after the image gives two headings. + { + testCase: { + name: "inlineImageInHeading", + content: `## Heading ![inline](https://example.com/img.png) text after`, + }, + executeTest: testParseMarkdown, + }, // Code block immediately after heading { testCase: { From d966b48e253cd7652b0ffd7da77e11b10d232338 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 15:56:51 +0100 Subject: [PATCH 03/13] fix(core): keep an image that is in a table cell as a link The HTML parser and the markdown parser dropped an image that was in a table cell, for example `| ![alt](url) |`. The cause: the content of a cell is parsed as blocks, and then only the inline content of these blocks goes into the cell. An image block has no inline content, so it was dropped. A table cell cannot hold a block, so a cell cannot split around the image as a paragraph does. Thus, before ProseMirror parses the HTML, a link to the image replaces each image that is in a cell. The link text is the alt text of the image, or its URL. Three snapshots of the HTML parse change: their table cells have an image without alt text, which is now a link with the URL as its text. --- .../src/api/parsers/html/util/imagesInText.ts | 10 +- .../html/imageBetweenParagraphsTableCell.json | 18 ++- .../html/inlineImageInTableCell.json | 109 +++++++++++++++++ .../html/mixedContentTableCell.json | 18 ++- .../html/paragraphImageTableCell.json | 11 ++ .../markdown/inlineImageInTableCell.json | 114 ++++++++++++++++++ .../parse/parseTestInstances.ts | 34 ++++++ 7 files changed, 311 insertions(+), 3 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInTableCell.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInTableCell.json diff --git a/packages/core/src/api/parsers/html/util/imagesInText.ts b/packages/core/src/api/parsers/html/util/imagesInText.ts index 07e96a7fd4..20d9b5e165 100644 --- a/packages/core/src/api/parsers/html/util/imagesInText.ts +++ b/packages/core/src/api/parsers/html/util/imagesInText.ts @@ -8,10 +8,18 @@ const TEXT_BLOCKS = "p, blockquote, h1, h2, h3, h4, h5, h6"; * ProseMirror drops a block that is in the text of a block. * * A paragraph, a quote and a heading split around the image, and the parts - * keep the attributes of the element. + * keep the attributes of the element. A table cell cannot hold a block, thus + * a link replaces the image there. */ export function moveImagesOutOfText(root: HTMLElement) { for (const image of root.querySelectorAll("img")) { + if (image.closest("td, th")) { + const link = detachedDoc().createElement("a"); + link.href = image.src; + link.textContent = image.alt || image.src; + image.replaceWith(link); + continue; + } // Splits the parent of the image until no text block holds the image: a // copy of the parent takes the content before the image. while (image.parentElement?.closest(TEXT_BLOCKS)) { diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageBetweenParagraphsTableCell.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageBetweenParagraphsTableCell.json index a7e9220784..e0ab1cc170 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageBetweenParagraphsTableCell.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageBetweenParagraphsTableCell.json @@ -14,7 +14,23 @@ "content": [ { "styles": {}, - "text": "Table CellTable Cell", + "text": "Table Cell", + "type": "text", + }, + { + "content": [ + { + "styles": {}, + "text": "exampleURL", + "type": "text", + }, + ], + "href": "exampleURL", + "type": "link", + }, + { + "styles": {}, + "text": "Table Cell", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInTableCell.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInTableCell.json new file mode 100644 index 0000000000..95b2702c35 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInTableCell.json @@ -0,0 +1,109 @@ +[ + { + "children": [], + "content": { + "columnWidths": [ + undefined, + undefined, + ], + "headerCols": undefined, + "headerRows": 1, + "rows": [ + { + "cells": [ + { + "content": [ + { + "styles": {}, + "text": "Light", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "styles": {}, + "text": "Dark", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + ], + }, + { + "cells": [ + { + "content": [ + { + "content": [ + { + "styles": {}, + "text": "Light theme", + "type": "text", + }, + ], + "href": "https://github.com/octo/repo/raw/main/docs/light.png", + "type": "link", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "content": [ + { + "styles": {}, + "text": "Dark theme", + "type": "text", + }, + ], + "href": "https://github.com/octo/repo/raw/main/docs/dark.png", + "type": "link", + }, + ], + "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/parse/__snapshots__/html/mixedContentTableCell.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/mixedContentTableCell.json index ba9afc2c7d..7427817724 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/mixedContentTableCell.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/mixedContentTableCell.json @@ -14,7 +14,23 @@ "content": [ { "styles": {}, - "text": "Table Cell Table Cell + "text": "Table Cell", + "type": "text", + }, + { + "content": [ + { + "styles": {}, + "text": "exampleURL", + "type": "text", + }, + ], + "href": "exampleURL", + "type": "link", + }, + { + "styles": {}, + "text": " Table Cell Table CellTable CellTable Cell", "type": "text", }, diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/paragraphImageTableCell.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/paragraphImageTableCell.json index 4fea15bf5b..9a4f19f6e3 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/paragraphImageTableCell.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/paragraphImageTableCell.json @@ -17,6 +17,17 @@ "text": "Table Cell", "type": "text", }, + { + "content": [ + { + "styles": {}, + "text": "exampleURL", + "type": "text", + }, + ], + "href": "exampleURL", + "type": "link", + }, ], "props": { "backgroundColor": "default", diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInTableCell.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInTableCell.json new file mode 100644 index 0000000000..68d0441e51 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInTableCell.json @@ -0,0 +1,114 @@ +[ + { + "children": [], + "content": { + "columnWidths": [ + undefined, + undefined, + ], + "headerCols": undefined, + "headerRows": 1, + "rows": [ + { + "cells": [ + { + "content": [ + { + "styles": {}, + "text": "Step", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "styles": {}, + "text": "Result", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + ], + }, + { + "cells": [ + { + "content": [ + { + "styles": {}, + "text": "Open the menu ", + "type": "text", + }, + { + "content": [ + { + "styles": {}, + "text": "The menu", + "type": "text", + }, + ], + "href": "https://example.com/menu.png", + "type": "link", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "content": [ + { + "styles": {}, + "text": "The result", + "type": "text", + }, + ], + "href": "https://example.com/result.png", + "type": "link", + }, + ], + "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/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index 50c5d6bb64..9077dfe24b 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -766,6 +766,29 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // Images in table cells, as GitHub writes them (each image is in a link). A + // table cell cannot hold a block, so the image becomes a link with its alt + // text. + { + testCase: { + name: "inlineImageInTableCell", + content: ` + + + + + + + + + + + + +
    LightDark
    Light themeDark theme
    `, + }, + executeTest: testParseHTML, + }, { testCase: { name: "fakeImageCaption", @@ -2301,6 +2324,17 @@ Final paragraph with [a link](https://example.com).`, }, executeTest: testParseMarkdown, }, + // Images in table cells, with text and alone. A table cell cannot hold a + // block, so the image becomes a link with its alt text. + { + testCase: { + name: "inlineImageInTableCell", + content: `| Step | Result | +| --- | --- | +| Open the menu ![The menu](https://example.com/menu.png) | ![The result](https://example.com/result.png) |`, + }, + executeTest: testParseMarkdown, + }, // Code block immediately after heading { testCase: { From 4dbd6c83a143557742d9ce207172fb3f068603cf Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 16:48:23 +0100 Subject: [PATCH 04/13] fix(core): do not move an image that is in the HTML of BlockNote The step that moves an image out of the text also changed HTML that BlockNote wrote, and text was lost. Custom inline content can render an image, for example a mention with an avatar. The HTML export of the paragraph `hi {mention} there` then has an `` in the text. The step split the paragraph at this image. For the full HTML, the parse then gave only `hi`: a block of the full HTML has one content element, and the split made two. An image is now not moved and not replaced by a link if it is in custom inline content (an element with `data-inline-content-type`) or in a block of the full HTML (an element with `data-content-type`). For such an image the parse gives the same result as before this step. --- .../src/api/parsers/html/util/imagesInText.ts | 5 ++ .../html/imageInCustomInlineContent.json | 77 +++++++++++++++++++ .../imageInCustomInlineContentFullHTML.json | 31 ++++++++ .../html/inlineImageInFullHTMLBlock.json | 51 ++++++++++++ .../parse/parseTestInstances.ts | 34 ++++++++ 5 files changed, 198 insertions(+) create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageInCustomInlineContent.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageInCustomInlineContentFullHTML.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInFullHTMLBlock.json diff --git a/packages/core/src/api/parsers/html/util/imagesInText.ts b/packages/core/src/api/parsers/html/util/imagesInText.ts index 20d9b5e165..e65e48bad9 100644 --- a/packages/core/src/api/parsers/html/util/imagesInText.ts +++ b/packages/core/src/api/parsers/html/util/imagesInText.ts @@ -13,6 +13,11 @@ const TEXT_BLOCKS = "p, blockquote, h1, h2, h3, h4, h5, h6"; */ export function moveImagesOutOfText(root: HTMLElement) { for (const image of root.querySelectorAll("img")) { + // BlockNote's own HTML stays as it is: custom inline content can hold an + // image, and a block of the full HTML has only one content element. + if (image.closest("[data-inline-content-type], [data-content-type]")) { + continue; + } if (image.closest("td, th")) { const link = detachedDoc().createElement("a"); link.href = image.src; diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageInCustomInlineContent.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageInCustomInlineContent.json new file mode 100644 index 0000000000..5ec3d96ebf --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageInCustomInlineContent.json @@ -0,0 +1,77 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Ask ", + "type": "text", + }, + { + "content": undefined, + "props": { + "user": "Matthew", + }, + "type": "mention", + }, + { + "styles": {}, + "text": " for help", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": { + "columnWidths": [ + undefined, + ], + "headerCols": undefined, + "headerRows": undefined, + "rows": [ + { + "cells": [ + { + "content": [ + { + "styles": {}, + "text": "Owner: ", + "type": "text", + }, + { + "content": undefined, + "props": { + "user": "Matthew", + }, + "type": "mention", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + ], + }, + ], + "type": "tableContent", + }, + "id": "2", + "props": { + "textColor": "default", + }, + "type": "table", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageInCustomInlineContentFullHTML.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageInCustomInlineContentFullHTML.json new file mode 100644 index 0000000000..1b6b39864e --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageInCustomInlineContentFullHTML.json @@ -0,0 +1,31 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Ask ", + "type": "text", + }, + { + "content": undefined, + "props": { + "user": "Matthew", + }, + "type": "mention", + }, + { + "styles": {}, + "text": " for help", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInFullHTMLBlock.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInFullHTMLBlock.json new file mode 100644 index 0000000000..e0ff6b1d69 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInFullHTMLBlock.json @@ -0,0 +1,51 @@ +[ + { + "children": [ + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "The harbor", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/harbor.jpg", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": " after", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Before", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index 9077dfe24b..6e5c8cfa10 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -789,6 +789,40 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // An image in custom inline content (a mention with an avatar), as the HTML + // export of BlockNote writes it. The image is a part of the inline content: + // the paragraph does not split, and the table cell gets no link. + { + testCase: { + name: "imageInCustomInlineContent", + content: `

    Ask @Matthew for help

    + + + + +
    Owner: @Matthew
    `, + }, + executeTest: testParseHTML, + }, + // The same in the full HTML of BlockNote. A block of the full HTML has one + // content element: a split would lose the text after the image. + { + testCase: { + name: "imageInCustomInlineContentFullHTML", + content: `

    Ask @Matthew for help

    `, + }, + executeTest: testParseHTML, + }, + // An image in the content element of a block of the full HTML. BlockNote + // does not write this, but the text after the image must not be lost: the + // block does not split, and ProseMirror makes the image a child. + { + testCase: { + name: "inlineImageInFullHTMLBlock", + content: `

    Before The harbor after

    `, + }, + executeTest: testParseHTML, + }, { testCase: { name: "fakeImageCaption", From a23d8a72ab3badc216bf889a77b00587fa9c0d33 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 16:49:14 +0100 Subject: [PATCH 05/13] fix(core): no link for an image in a table cell if its URL cannot be a link In a table cell, a link replaces an image. The link mark of the editor does not take each URL: it refuses `file:`, `data:`, `blob:` and `javascript:`. For such an image the cell got the URL as plain text. A table from Word with a picture in a cell showed the local path of the picture, and a `data:` URL gave thousands of characters. The link now replaces the image only if `isAllowedUri`, the check of the link mark, accepts the URL of the image. In each other case the image stays as it is, and the parse of the cell drops it, as before this branch. This also applies to an image without a URL. --- .../src/api/parsers/html/util/imagesInText.ts | 15 +- .../html/imageUrlsInTableCell.json | 223 ++++++++++++++++++ .../parse/parseTestInstances.ts | 32 +++ 3 files changed, 265 insertions(+), 5 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageUrlsInTableCell.json diff --git a/packages/core/src/api/parsers/html/util/imagesInText.ts b/packages/core/src/api/parsers/html/util/imagesInText.ts index e65e48bad9..90bd642450 100644 --- a/packages/core/src/api/parsers/html/util/imagesInText.ts +++ b/packages/core/src/api/parsers/html/util/imagesInText.ts @@ -1,3 +1,4 @@ +import { isAllowedUri } from "../../../../extensions/tiptap-extensions/Link/link.js"; import { detachedDoc } from "../../../../util/detachedDoc.js"; // The elements with text that split around an image. @@ -9,7 +10,7 @@ const TEXT_BLOCKS = "p, blockquote, h1, h2, h3, h4, h5, h6"; * * A paragraph, a quote and a heading split around the image, and the parts * keep the attributes of the element. A table cell cannot hold a block, thus - * a link replaces the image there. + * a link replaces the image there, if the URL of the image can be a link. */ export function moveImagesOutOfText(root: HTMLElement) { for (const image of root.querySelectorAll("img")) { @@ -19,10 +20,14 @@ export function moveImagesOutOfText(root: HTMLElement) { continue; } if (image.closest("td, th")) { - const link = detachedDoc().createElement("a"); - link.href = image.src; - link.textContent = image.alt || image.src; - image.replaceWith(link); + // Not each URL can be a link (for example `file:` and `data:`). Such an + // image stays, and the parse of the cell drops it. + if (image.src && isAllowedUri(image.src)) { + const link = detachedDoc().createElement("a"); + link.href = image.src; + link.textContent = image.alt || image.src; + image.replaceWith(link); + } continue; } // Splits the parent of the image until no text block holds the image: a diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageUrlsInTableCell.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageUrlsInTableCell.json new file mode 100644 index 0000000000..61c8772ecb --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageUrlsInTableCell.json @@ -0,0 +1,223 @@ +[ + { + "children": [], + "content": { + "columnWidths": [ + undefined, + undefined, + ], + "headerCols": undefined, + "headerRows": 1, + "rows": [ + { + "cells": [ + { + "content": [ + { + "content": [ + { + "styles": {}, + "text": "Logo", + "type": "text", + }, + ], + "href": "https://example.com/logo.png", + "type": "link", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "styles": {}, + "text": "The URL of the image", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + ], + }, + { + "cells": [ + { + "content": [ + { + "content": [ + { + "styles": {}, + "text": "Chart", + "type": "text", + }, + ], + "href": "https://example.com/chart.png", + "type": "link", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "styles": {}, + "text": "A web URL, with alt text", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + ], + }, + { + "cells": [ + { + "content": [ + { + "content": [ + { + "styles": {}, + "text": "https://example.com/chart.png", + "type": "text", + }, + ], + "href": "https://example.com/chart.png", + "type": "link", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "styles": {}, + "text": "A web URL, without alt text", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + ], + }, + { + "cells": [ + { + "content": [ + { + "styles": {}, + "text": "Before after", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "styles": {}, + "text": "A file URL", + "type": "text", + }, + ], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + ], + }, + { + "cells": [ + { + "content": [], + "props": { + "backgroundColor": "default", + "colspan": 1, + "rowspan": 1, + "textAlignment": "left", + "textColor": "default", + }, + "type": "tableCell", + }, + { + "content": [ + { + "styles": {}, + "text": "A data URL", + "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/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index 6e5c8cfa10..54898d9197 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -823,6 +823,38 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // Images in table cells with different URLs. A web URL becomes a link, with + // the alt text or the URL as its text, also in a header cell. A `file:` URL + // (a table from Word) and a `data:` URL cannot be a link: the image is + // dropped, and the cell does not get the URL as text. + { + testCase: { + name: "imageUrlsInTableCell", + content: ` + + + + + + + + + + + + + + + + + + + + +
    LogoThe URL of the image
    ChartA web URL, with alt text
    A web URL, without alt text
    Before Picture 1 afterA file URL
    A data URL
    `, + }, + executeTest: testParseHTML, + }, { testCase: { name: "fakeImageCaption", From 5b0d380b121d940fdebfd11adc9dbde657565976 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 16:50:03 +0100 Subject: [PATCH 06/13] fix(core): remove the line break and the space next to an image that splits a block When a block splits around an image, the text before the image kept the space at its end, and the text after the image started with a space: `a b` gave `a `, the image and ` b`. An image on its own line (`Some text`, a line break, the image, a line break, `More text`) gave parts that ended and started with a line break. A line break or white space directly next to the image has no meaning at the edge of a block. At each split, the `
    ` elements and the white space text directly before and after the image are now removed. `a b` gives `a`, the image and `b`. Nine snapshots change: only the spaces next to the image are gone. --- .../src/api/parsers/html/util/imagesInText.ts | 22 ++++++++ .../html/inlineImageInHeading.json | 2 +- .../html/inlineImageInListItemParagraph.json | 4 +- .../html/inlineImageInParagraph.json | 4 +- .../html/inlineImageInQuote.json | 4 +- .../html/inlineImageInStyledParagraph.json | 4 +- .../html/lineBreakNextToInlineImage.json | 50 +++++++++++++++++++ .../__snapshots__/markdown/inlineImage.json | 4 +- .../markdown/inlineImageInHeading.json | 4 +- .../markdown/inlineImageInListItem.json | 4 +- .../markdown/inlineImageInQuote.json | 4 +- .../markdown/lineBreakNextToInlineImage.json | 50 +++++++++++++++++++ .../parse/parseTestInstances.ts | 20 ++++++++ 13 files changed, 159 insertions(+), 17 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/lineBreakNextToInlineImage.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/lineBreakNextToInlineImage.json diff --git a/packages/core/src/api/parsers/html/util/imagesInText.ts b/packages/core/src/api/parsers/html/util/imagesInText.ts index 90bd642450..5b4b78c307 100644 --- a/packages/core/src/api/parsers/html/util/imagesInText.ts +++ b/packages/core/src/api/parsers/html/util/imagesInText.ts @@ -4,6 +4,26 @@ import { detachedDoc } from "../../../../util/detachedDoc.js"; // The elements with text that split around an image. const TEXT_BLOCKS = "p, blockquote, h1, h2, h3, h4, h5, h6"; +// Removes the line breaks and the white space that are next to the image on +// one side: they have no meaning at the edge of a block. +function trimNextTo(image: Element, side: "previousSibling" | "nextSibling") { + for (let node = image[side]; node; node = image[side]) { + if (node.nodeName !== "BR") { + // Only a text node (type 3) can have white space. + if (node.nodeType !== 3) { + return; + } + const text = node.nodeValue ?? ""; + node.nodeValue = + side === "nextSibling" ? text.trimStart() : text.trimEnd(); + if (node.nodeValue) { + return; + } + } + node.remove(); + } +} + /** * Moves each image out of the text that holds it. An image is a block, and * ProseMirror drops a block that is in the text of a block. @@ -34,6 +54,8 @@ export function moveImagesOutOfText(root: HTMLElement) { // copy of the parent takes the content before the image. while (image.parentElement?.closest(TEXT_BLOCKS)) { const parent = image.parentElement; + trimNextTo(image, "previousSibling"); + trimNextTo(image, "nextSibling"); const before = detachedDoc().importNode(parent, false); while (image.previousSibling) { before.prepend(image.previousSibling); diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeading.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeading.json index c6aed71073..00c45b78de 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeading.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeading.json @@ -19,7 +19,7 @@ "content": [ { "styles": {}, - "text": " Project", + "text": "Project", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraph.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraph.json index 0fd16ff289..00edbd9b4e 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraph.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraph.json @@ -20,7 +20,7 @@ "content": [ { "styles": {}, - "text": " and select a block", + "text": "and select a block", "type": "text", }, ], @@ -36,7 +36,7 @@ "content": [ { "styles": {}, - "text": "Open the menu ", + "text": "Open the menu", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInParagraph.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInParagraph.json index 5f6eb71756..a780e42b63 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInParagraph.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInParagraph.json @@ -4,7 +4,7 @@ "content": [ { "styles": {}, - "text": "We arrived at the harbor before dusk. ", + "text": "We arrived at the harbor before dusk.", "type": "text", }, ], @@ -36,7 +36,7 @@ "content": [ { "styles": {}, - "text": " The boats were in.", + "text": "The boats were in.", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInQuote.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInQuote.json index d439ad5ccb..dac0c910a4 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInQuote.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInQuote.json @@ -4,7 +4,7 @@ "content": [ { "styles": {}, - "text": "As the picture shows ", + "text": "As the picture shows", "type": "text", }, ], @@ -34,7 +34,7 @@ "content": [ { "styles": {}, - "text": " the result is good.", + "text": "the result is good.", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInStyledParagraph.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInStyledParagraph.json index 375045c1cf..7d5a42500b 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInStyledParagraph.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInStyledParagraph.json @@ -4,7 +4,7 @@ "content": [ { "styles": {}, - "text": "Before ", + "text": "Before", "type": "text", }, ], @@ -35,7 +35,7 @@ "content": [ { "styles": {}, - "text": " and ", + "text": "and ", "type": "text", }, { diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/lineBreakNextToInlineImage.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/lineBreakNextToInlineImage.json new file mode 100644 index 0000000000..feabfc3c0c --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/lineBreakNextToInlineImage.json @@ -0,0 +1,50 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Some text", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "The harbor", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/harbor.jpg", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "More text", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImage.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImage.json index d294770df9..227eb26738 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImage.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImage.json @@ -4,7 +4,7 @@ "content": [ { "styles": {}, - "text": "Text before ", + "text": "Text before", "type": "text", }, ], @@ -35,7 +35,7 @@ "content": [ { "styles": {}, - "text": " text after", + "text": "text after", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInHeading.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInHeading.json index 68ef158a71..718b2cd0be 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInHeading.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInHeading.json @@ -4,7 +4,7 @@ "content": [ { "styles": {}, - "text": "Heading ", + "text": "Heading", "type": "text", }, ], @@ -37,7 +37,7 @@ "content": [ { "styles": {}, - "text": " text after", + "text": "text after", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItem.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItem.json index 7fd3d43b75..cc16af1850 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItem.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItem.json @@ -20,7 +20,7 @@ "content": [ { "styles": {}, - "text": " text after", + "text": "text after", "type": "text", }, ], @@ -36,7 +36,7 @@ "content": [ { "styles": {}, - "text": "Item ", + "text": "Item", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInQuote.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInQuote.json index b2ae0b0a69..aad7647727 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInQuote.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInQuote.json @@ -4,7 +4,7 @@ "content": [ { "styles": {}, - "text": "Quote ", + "text": "Quote", "type": "text", }, ], @@ -34,7 +34,7 @@ "content": [ { "styles": {}, - "text": " text after", + "text": "text after", "type": "text", }, ], diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/lineBreakNextToInlineImage.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/lineBreakNextToInlineImage.json new file mode 100644 index 0000000000..32c9f017d0 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/lineBreakNextToInlineImage.json @@ -0,0 +1,50 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Some text", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "inline", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/img.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "More text", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index 54898d9197..059366ab16 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -730,6 +730,15 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // An image on its own line in a paragraph. The line breaks and the spaces + // next to the image have no meaning at the edge of a block: they are removed. + { + testCase: { + name: "lineBreakNextToInlineImage", + content: `

    Some text
    The harbor
    More text

    `, + }, + executeTest: testParseHTML, + }, // An image in the paragraph of a list item, as GitHub writes it (the image // is in a link). The image and the text after it become children of the // list item. @@ -2364,6 +2373,17 @@ Final paragraph with [a link](https://example.com).`, }, executeTest: testParseMarkdown, }, + // An image on its own line in a paragraph. The line breaks next to the + // image are removed. + { + testCase: { + name: "lineBreakNextToInlineImage", + content: `Some text +![inline](https://example.com/img.png) +More text`, + }, + executeTest: testParseMarkdown, + }, // An image in the text of a list item: the image and the text after it // become children of the list item. { From b9836b86c8d4b188d88969727fad04bc411bc045 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 16:52:09 +0100 Subject: [PATCH 07/13] fix(core): keep the time of the image step about linear in jsdom The step that moves an image out of the text put the parts of each block before the block. In jsdom, the time to put a node before another node grows with the number of nodes before it. A document with 20,000 paragraphs that each have an image took about 115 s in jsdom, which is the server path. `main` takes about 2 s. The nodes of an element that are complete now go to a document fragment, and they come back at the end. They are removed from the start of the element, which is fast in jsdom. A block that splits is thus always near the start of its parent. The nodes before the image also move from the start of their parent now. The results do not change. In jsdom, 2,000 paragraphs with an image each now take about 0.3 s (before: 0.55 s), and 20,000 take about 3 s. --- .../src/api/parsers/html/util/imagesInText.ts | 27 ++- .../html/inlineImagesBetweenParagraphs.json | 197 ++++++++++++++++++ .../parse/parseTestInstances.ts | 16 ++ 3 files changed, 237 insertions(+), 3 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImagesBetweenParagraphs.json diff --git a/packages/core/src/api/parsers/html/util/imagesInText.ts b/packages/core/src/api/parsers/html/util/imagesInText.ts index 5b4b78c307..f4b46ecdae 100644 --- a/packages/core/src/api/parsers/html/util/imagesInText.ts +++ b/packages/core/src/api/parsers/html/util/imagesInText.ts @@ -24,6 +24,14 @@ function trimNextTo(image: Element, side: "previousSibling" | "nextSibling") { } } +// Moves the nodes that are before `node` in its parent to the end of `to`. +function moveNodesBefore(node: ChildNode, to: ParentNode) { + const from = node.parentNode; + while (from?.firstChild && from.firstChild !== node) { + to.append(from.firstChild); + } +} + /** * Moves each image out of the text that holds it. An image is a block, and * ProseMirror drops a block that is in the text of a block. @@ -33,6 +41,10 @@ function trimNextTo(image: Element, side: "previousSibling" | "nextSibling") { * a link replaces the image there, if the URL of the image can be a link. */ export function moveImagesOutOfText(root: HTMLElement) { + // The nodes of an element that are complete wait here until the end. In + // jsdom, the time to put a node before another node grows with the number + // of nodes before it. Thus a block that splits must be near the start. + const complete = new Map(); for (const image of root.querySelectorAll("img")) { // BlockNote's own HTML stays as it is: custom inline content can hold an // image, and a block of the full HTML has only one content element. @@ -52,15 +64,15 @@ export function moveImagesOutOfText(root: HTMLElement) { } // Splits the parent of the image until no text block holds the image: a // copy of the parent takes the content before the image. + let holder: ParentNode | null = null; while (image.parentElement?.closest(TEXT_BLOCKS)) { const parent = image.parentElement; trimNextTo(image, "previousSibling"); trimNextTo(image, "nextSibling"); const before = detachedDoc().importNode(parent, false); - while (image.previousSibling) { - before.prepend(image.previousSibling); - } + moveNodesBefore(image, before); parent.before(before, image); + holder = parent.parentNode; // A part without text is not a block. Its other content stays. for (const part of [before, parent]) { if (!part.textContent?.trim()) { @@ -68,5 +80,14 @@ export function moveImagesOutOfText(root: HTMLElement) { } } } + if (holder) { + const nodes = + complete.get(holder) ?? detachedDoc().createDocumentFragment(); + complete.set(holder, nodes); + moveNodesBefore(image, nodes); + } + } + for (const [holder, nodes] of complete) { + holder.prepend(nodes); } } diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImagesBetweenParagraphs.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImagesBetweenParagraphs.json new file mode 100644 index 0000000000..d371207a67 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImagesBetweenParagraphs.json @@ -0,0 +1,197 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "First", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "One", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/one.jpg", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "second", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "No image here", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Third", + "type": "text", + }, + ], + "id": "5", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": undefined, + "id": "6", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "Two", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/two.jpg", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "fourth", + "type": "text", + }, + ], + "id": "7", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "No image here also", + "type": "text", + }, + ], + "id": "8", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Fifth", + "type": "text", + }, + ], + "id": "9", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": undefined, + "id": "10", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "Three", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/three.jpg", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "sixth", + "type": "text", + }, + ], + "id": "11", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "The end", + "type": "text", + }, + ], + "id": "12", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index 059366ab16..663976ff71 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -739,6 +739,22 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // Paragraphs with and without an image, at the top and in a `
    `. The + // blocks keep their order. + { + testCase: { + name: "inlineImagesBetweenParagraphs", + content: `

    First One second

    +

    No image here

    +
    +

    Third Two fourth

    +

    No image here also

    +

    Fifth Three sixth

    +
    +

    The end

    `, + }, + executeTest: testParseHTML, + }, // An image in the paragraph of a list item, as GitHub writes it (the image // is in a link). The image and the text after it become children of the // list item. From 85249e03a609181bbcbeca016ff545fcb3bf1cb6 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 16:55:34 +0100 Subject: [PATCH 08/13] test(core): more parse cases for an image in the text A review found changes of the image step that no test saw. These cases make each of them fail a test: - Text with a style before the image: the nodes before the image keep their order. - A paragraph with an image and a video and no text: a part without text is removed, but its other content stays. - A heading with an image and a space in a ``: a part with only white space is not a block. - The heading levels 3 to 6. An image in a header cell of a table is in the case `imageUrlsInTableCell` of an earlier commit. A new case has an image without a URL in a table cell. One more case shows a known fault: a nested list after an image in a list item moves out of its item. The cause is the list parse, which does this for each list item that has a block as a child. The snapshot records the result of this branch alone. It changes when the list parse is corrected. --- .../html/imageAndSpaceInHeading.json | 16 +++ .../html/imageAndVideoInParagraph.json | 30 ++++ .../html/imageWithoutUrlInTableCell.json | 41 ++++++ .../html/inlineImageAfterStyledText.json | 62 ++++++++ .../html/inlineImageInHeadingLevels.json | 134 ++++++++++++++++++ .../inlineImageInListItemWithNestedList.json | 85 +++++++++++ .../parse/parseTestInstances.ts | 65 +++++++++ 7 files changed, 433 insertions(+) create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageAndSpaceInHeading.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageAndVideoInParagraph.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageWithoutUrlInTableCell.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageAfterStyledText.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeadingLevels.json create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItemWithNestedList.json diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageAndSpaceInHeading.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageAndSpaceInHeading.json new file mode 100644 index 0000000000..978f89d69b --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageAndSpaceInHeading.json @@ -0,0 +1,16 @@ +[ + { + "children": [], + "content": undefined, + "id": "1", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "Logo", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/logo.png", + }, + "type": "image", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageAndVideoInParagraph.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageAndVideoInParagraph.json new file mode 100644 index 0000000000..2117c7d40d --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageAndVideoInParagraph.json @@ -0,0 +1,30 @@ +[ + { + "children": [], + "content": undefined, + "id": "1", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "Photo", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/photo.jpg", + }, + "type": "image", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/clip.mp4", + }, + "type": "video", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageWithoutUrlInTableCell.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageWithoutUrlInTableCell.json new file mode 100644 index 0000000000..78c95a87ed --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/imageWithoutUrlInTableCell.json @@ -0,0 +1,41 @@ +[ + { + "children": [], + "content": { + "columnWidths": [ + undefined, + ], + "headerCols": undefined, + "headerRows": undefined, + "rows": [ + { + "cells": [ + { + "content": [ + { + "styles": {}, + "text": "Before after", + "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/parse/__snapshots__/html/inlineImageAfterStyledText.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageAfterStyledText.json new file mode 100644 index 0000000000..6bb34e89bc --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageAfterStyledText.json @@ -0,0 +1,62 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Open ", + "type": "text", + }, + { + "styles": { + "bold": true, + }, + "text": "the menu", + "type": "text", + }, + { + "styles": {}, + "text": " and look:", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "The menu", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/menu.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Then close it.", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeadingLevels.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeadingLevels.json new file mode 100644 index 0000000000..db5f7ea2d3 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeadingLevels.json @@ -0,0 +1,134 @@ +[ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Level three", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "isToggleable": false, + "level": 3, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "3", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/3.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Level four", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "isToggleable": false, + "level": 4, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, + { + "children": [], + "content": undefined, + "id": "4", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "4", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/4.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Level five", + "type": "text", + }, + ], + "id": "5", + "props": { + "backgroundColor": "default", + "isToggleable": false, + "level": 5, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, + { + "children": [], + "content": undefined, + "id": "6", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "5", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/5.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Level six", + "type": "text", + }, + ], + "id": "7", + "props": { + "backgroundColor": "default", + "isToggleable": false, + "level": 6, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, + { + "children": [], + "content": undefined, + "id": "8", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "6", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/6.png", + }, + "type": "image", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItemWithNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItemWithNestedList.json new file mode 100644 index 0000000000..57fffdb9fe --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItemWithNestedList.json @@ -0,0 +1,85 @@ +[ + { + "children": [ + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "shot", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/shot.png", + }, + "type": "image", + }, + ], + "content": [ + { + "styles": {}, + "text": "Step one", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "sub a", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "sub b", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Step two", + "type": "text", + }, + ], + "id": "5", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index 663976ff71..a9a3a6f740 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -755,6 +755,24 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // Text with a style before the image: the text before the image keeps its + // order. + { + testCase: { + name: "inlineImageAfterStyledText", + content: `

    Open the menu and look: The menu Then close it.

    `, + }, + executeTest: testParseHTML, + }, + // A paragraph with an image and a video, and no text. No part has text, so + // there is no paragraph, but the video stays. + { + testCase: { + name: "imageAndVideoInParagraph", + content: `

    Photo

    `, + }, + executeTest: testParseHTML, + }, // An image in the paragraph of a list item, as GitHub writes it (the image // is in a link). The image and the text after it become children of the // list item. @@ -791,6 +809,39 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // An image without a URL in a table cell: there is no link, and the cell + // does not get the alt text. + { + testCase: { + name: "imageWithoutUrlInTableCell", + content: ` + + + +
    Before A picture that is not there after
    `, + }, + executeTest: testParseHTML, + }, + // A heading with an image and only a space after it, as Word writes it. A + // part with only white space is not a block: there is no empty heading. + { + testCase: { + name: "imageAndSpaceInHeading", + content: `

    Logo

    `, + }, + executeTest: testParseHTML, + }, + // Each heading level splits around an image. + { + testCase: { + name: "inlineImageInHeadingLevels", + content: `

    Level three 3

    +

    Level four 4

    +
    Level five 5
    +
    Level six 6
    `, + }, + executeTest: testParseHTML, + }, // Images in table cells, as GitHub writes them (each image is in a link). A // table cell cannot hold a block, so the image becomes a link with its alt // text. @@ -2409,6 +2460,20 @@ More text`, }, executeTest: testParseMarkdown, }, + // A nested list after an image in a list item. The nested list moves out of + // its item, and the numbers start again. This is a fault of the list parse: + // it occurs for each list item that has a block as a child. This snapshot + // changes when the list parse is corrected. + { + testCase: { + name: "inlineImageInListItemWithNestedList", + content: `1. Step one ![shot](https://example.com/shot.png) + - sub a + - sub b +2. Step two`, + }, + executeTest: testParseMarkdown, + }, // An image in a quote: the quote splits around the image. { testCase: { From 7b97be8aa253f26679aa706008501dfe2078655c Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 17:00:01 +0100 Subject: [PATCH 09/13] fix(core): do not move an image that is in the summary of a toggle The summary of a `
    ` element is the text of a toggle. A toggle cannot split, so an image cannot go out of the summary. But the step split a heading or a paragraph that was in the summary. The parse of the toggle then put the parts together again, and the image was dropped. Since the step removes the space next to the image, this joined two words: `

    Step one

    ` gave the toggle heading `Stepone`. A toggle heading with only an image became a toggle list item. The step now does not change a summary. An image in a summary gives the same result as before this branch: the image is dropped, and the text stays as it is. --- .../src/api/parsers/html/util/imagesInText.ts | 7 +++- .../html/inlineImageInToggleSummary.json | 39 +++++++++++++++++++ .../parse/parseTestInstances.ts | 10 +++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInToggleSummary.json diff --git a/packages/core/src/api/parsers/html/util/imagesInText.ts b/packages/core/src/api/parsers/html/util/imagesInText.ts index f4b46ecdae..02c71d7642 100644 --- a/packages/core/src/api/parsers/html/util/imagesInText.ts +++ b/packages/core/src/api/parsers/html/util/imagesInText.ts @@ -47,8 +47,11 @@ export function moveImagesOutOfText(root: HTMLElement) { const complete = new Map(); for (const image of root.querySelectorAll("img")) { // BlockNote's own HTML stays as it is: custom inline content can hold an - // image, and a block of the full HTML has only one content element. - if (image.closest("[data-inline-content-type], [data-content-type]")) { + // image, and a block of the full HTML has only one content element. The + // summary of a toggle is its text: an image cannot go out of it. + if ( + image.closest("[data-inline-content-type], [data-content-type], summary") + ) { continue; } if (image.closest("td, th")) { diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInToggleSummary.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInToggleSummary.json new file mode 100644 index 0000000000..00224f1a19 --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInToggleSummary.json @@ -0,0 +1,39 @@ +[ + { + "children": [ + { + "children": [], + "content": [ + { + "styles": {}, + "text": "The details", + "type": "text", + }, + ], + "id": "2", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + ], + "content": [ + { + "styles": {}, + "text": "Step one", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "isToggleable": true, + "level": 2, + "textAlignment": "left", + "textColor": "default", + }, + "type": "heading", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index a9a3a6f740..824db3e471 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -842,6 +842,16 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // An image in the summary of a toggle. The summary is the text of the + // toggle, and a toggle cannot split: the step does not change the summary. + // The image is dropped, and the words next to it stay apart. + { + testCase: { + name: "inlineImageInToggleSummary", + content: `

    Step A screenshot one

    The details

    `, + }, + executeTest: testParseHTML, + }, // Images in table cells, as GitHub writes them (each image is in a link). A // table cell cannot hold a block, so the image becomes a link with its alt // text. From 1bcb70b5361fa2d50457a59f7db3f3494a3a4505 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 17:10:30 +0100 Subject: [PATCH 10/13] fix(core): move an image out of the text before the list steps The HTML parse first moved the nested lists out of their list items, and then moved the images out of the text. For a list item with an image in its paragraph and a nested list, for example `
  • Step one

      ...
  • ` or the markdown `1. Step one ![shot](url)` with sub items, the list steps saw a list item with one paragraph. The image came out of the paragraph later, and the nested list was then not in the list item: its items went to the top level, and a numbered list started again. The image step now runs first. The list steps then see the image as a block of the list item, and the image, the text after it and the nested list are children of the list item, in this order. For this, `HTMLToBlocks` makes the element for the HTML in `detachedDoc()`, runs the image step, then the list steps on this element, then the white space step. `nestedListsToBlockNoteStructure` now takes this element, not the HTML string. `HTMLToBlocks` is its one caller; its tests make the element in the same way. --- .../core/src/api/parsers/html/parseHTML.ts | 10 +- .../api/parsers/html/util/nestedLists.test.ts | 14 ++- .../src/api/parsers/html/util/nestedLists.ts | 9 +- ...mageInListItemParagraphWithNestedList.json | 119 ++++++++++++++++++ .../inlineImageInListItemWithNestedList.json | 66 +++++----- .../parse/parseTestInstances.ts | 26 +++- 6 files changed, 198 insertions(+), 46 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraphWithNestedList.json diff --git a/packages/core/src/api/parsers/html/parseHTML.ts b/packages/core/src/api/parsers/html/parseHTML.ts index 05a4d02c31..252cbf7119 100644 --- a/packages/core/src/api/parsers/html/parseHTML.ts +++ b/packages/core/src/api/parsers/html/parseHTML.ts @@ -7,6 +7,7 @@ import { } from "../../../schema/index.js"; import { Block } from "../../../blocks/defaultBlocks.js"; +import { detachedDoc } from "../../../util/detachedDoc.js"; import { EMPTY_BLOCK_PLACEHOLDER } from "../../exporters/html/util/serializeBlocksExternalHTML.js"; import { nodeToBlock } from "../../nodeConversions/nodeToBlock.js"; import { nestedListsToBlockNoteStructure } from "./util/nestedLists.js"; @@ -75,9 +76,14 @@ export function HTMLToBlocks< I extends InlineContentSchema, S extends StyleSchema, >(html: string, pmSchema: Schema): Block[] { - const htmlNode = nestedListsToBlockNoteStructure(html); - preprocessHTMLWhitespace(htmlNode); + const htmlNode = detachedDoc().createElement("div"); + htmlNode.innerHTML = html; + // The images move out of the text before the list steps. An image in the + // paragraph of a list item is then a block for these steps, and a nested + // list after it stays in the list item. moveImagesOutOfText(htmlNode); + nestedListsToBlockNoteStructure(htmlNode); + preprocessHTMLWhitespace(htmlNode); const parser = DOMParser.fromSchema(pmSchema); // Other approach might be to use diff --git a/packages/core/src/api/parsers/html/util/nestedLists.test.ts b/packages/core/src/api/parsers/html/util/nestedLists.test.ts index 6ee15c077d..06ec91eeb1 100644 --- a/packages/core/src/api/parsers/html/util/nestedLists.test.ts +++ b/packages/core/src/api/parsers/html/util/nestedLists.test.ts @@ -1,9 +1,17 @@ import { afterEach, describe, expect, it } from "vite-plus/test"; +import { detachedDoc } from "../../../../util/detachedDoc.js"; import { nestedListsToBlockNoteStructure } from "./nestedLists.js"; +// Makes the element for the HTML as `HTMLToBlocks` does, and runs the steps. +function parse(html: string) { + const element = detachedDoc().createElement("div"); + element.innerHTML = html; + nestedListsToBlockNoteStructure(element); + return element; +} + async function testHTML(html: string) { - const htmlNode = nestedListsToBlockNoteStructure(html); - expect(htmlNode.innerHTML).toMatchSnapshot(); + expect(parse(html).innerHTML).toMatchSnapshot(); } describe("Lift nested lists", () => { @@ -192,7 +200,7 @@ describe("content after a nested list", () => { } }; - const htmlNode = nestedListsToBlockNoteStructure( + const htmlNode = parse( `
    • Item
      • Nested item
    `, ); diff --git a/packages/core/src/api/parsers/html/util/nestedLists.ts b/packages/core/src/api/parsers/html/util/nestedLists.ts index 7b3f967c42..7e97c842cf 100644 --- a/packages/core/src/api/parsers/html/util/nestedLists.ts +++ b/packages/core/src/api/parsers/html/util/nestedLists.ts @@ -185,10 +185,11 @@ function createGroups(element: HTMLElement) { }); } -export function nestedListsToBlockNoteStructure(html: string) { - const element = detachedDoc().createElement("div"); - element.innerHTML = html; +/** + * Changes the lists in `element` with the two steps above. The element is the + * one that `HTMLToBlocks` makes in `detachedDoc()` for the HTML. + */ +export function nestedListsToBlockNoteStructure(element: HTMLElement) { liftNestedListsToParent(element); createGroups(element); - return element; } diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraphWithNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraphWithNestedList.json new file mode 100644 index 0000000000..686a0e4ebd --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraphWithNestedList.json @@ -0,0 +1,119 @@ +[ + { + "children": [ + { + "children": [], + "content": undefined, + "id": "2", + "props": { + "backgroundColor": "default", + "caption": "", + "name": "A screenshot", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/shot.png", + }, + "type": "image", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "done", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "paragraph", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "sub a", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "sub b", + "type": "text", + }, + ], + "id": "5", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", + }, + ], + "content": [ + { + "styles": {}, + "text": "Step one", + "type": "text", + }, + ], + "id": "1", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Step two", + "type": "text", + }, + ], + "id": "6", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, + { + "children": [], + "content": [ + { + "styles": {}, + "text": "Step three", + "type": "text", + }, + ], + "id": "7", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "numberedListItem", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItemWithNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItemWithNestedList.json index 57fffdb9fe..396ce7e72d 100644 --- a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItemWithNestedList.json +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItemWithNestedList.json @@ -15,55 +15,55 @@ }, "type": "image", }, - ], - "content": [ { - "styles": {}, - "text": "Step one", - "type": "text", + "children": [], + "content": [ + { + "styles": {}, + "text": "sub a", + "type": "text", + }, + ], + "id": "3", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", }, - ], - "id": "1", - "props": { - "backgroundColor": "default", - "textAlignment": "left", - "textColor": "default", - }, - "type": "numberedListItem", - }, - { - "children": [], - "content": [ { - "styles": {}, - "text": "sub a", - "type": "text", + "children": [], + "content": [ + { + "styles": {}, + "text": "sub b", + "type": "text", + }, + ], + "id": "4", + "props": { + "backgroundColor": "default", + "textAlignment": "left", + "textColor": "default", + }, + "type": "bulletListItem", }, ], - "id": "3", - "props": { - "backgroundColor": "default", - "textAlignment": "left", - "textColor": "default", - }, - "type": "bulletListItem", - }, - { - "children": [], "content": [ { "styles": {}, - "text": "sub b", + "text": "Step one", "type": "text", }, ], - "id": "4", + "id": "1", "props": { "backgroundColor": "default", "textAlignment": "left", "textColor": "default", }, - "type": "bulletListItem", + "type": "numberedListItem", }, { "children": [], diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index 824db3e471..e62e523368 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -790,6 +790,26 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // An image in the paragraph of a list item that has a nested list. The + // image, the text after it and the nested list are children of the list + // item, in this order, and the numbered list continues. + { + testCase: { + name: "inlineImageInListItemParagraphWithNestedList", + content: `
      +
    1. +

      Step one A screenshot done

      +
        +
      • sub a
      • +
      • sub b
      • +
      +
    2. +
    3. Step two

    4. +
    5. Step three

    6. +
    `, + }, + executeTest: testParseHTML, + }, // An image in a quote, as a markdown renderer writes it. The quote splits // around the image. { @@ -2470,10 +2490,8 @@ More text`, }, executeTest: testParseMarkdown, }, - // A nested list after an image in a list item. The nested list moves out of - // its item, and the numbers start again. This is a fault of the list parse: - // it occurs for each list item that has a block as a child. This snapshot - // changes when the list parse is corrected. + // A nested list after an image in a list item. The image and the nested + // list are children of the list item, and the numbered list continues. { testCase: { name: "inlineImageInListItemWithNestedList", From 993b4565f9edc0da79c3ca16dbd34e08a3f1e9e7 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 17:10:58 +0100 Subject: [PATCH 11/13] fix(core): do not move an image that is in a figure caption The caption of a figure is text: the parse of a figure takes the text of its `
    `. An image cannot go out of the caption. But the step split a paragraph that was in the caption and removed the space next to the image. This joined two words: `

    cap more

    ` gave the caption `capmore`. The step now does not change a `
    `, as it does not change the summary of a toggle. An image in a caption gives the same result as before this branch: the image is dropped, and the text stays as it is. --- .../src/api/parsers/html/util/imagesInText.ts | 7 +++++-- .../html/inlineImageInFigureCaption.json | 16 ++++++++++++++++ .../formatConversion/parse/parseTestInstances.ts | 10 ++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInFigureCaption.json diff --git a/packages/core/src/api/parsers/html/util/imagesInText.ts b/packages/core/src/api/parsers/html/util/imagesInText.ts index 02c71d7642..0bc8af4144 100644 --- a/packages/core/src/api/parsers/html/util/imagesInText.ts +++ b/packages/core/src/api/parsers/html/util/imagesInText.ts @@ -48,9 +48,12 @@ export function moveImagesOutOfText(root: HTMLElement) { for (const image of root.querySelectorAll("img")) { // BlockNote's own HTML stays as it is: custom inline content can hold an // image, and a block of the full HTML has only one content element. The - // summary of a toggle is its text: an image cannot go out of it. + // summary of a toggle and the caption of a figure are text: an image + // cannot go out of them. if ( - image.closest("[data-inline-content-type], [data-content-type], summary") + image.closest( + "[data-inline-content-type], [data-content-type], summary, figcaption", + ) ) { continue; } diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInFigureCaption.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInFigureCaption.json new file mode 100644 index 0000000000..7a171874bb --- /dev/null +++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInFigureCaption.json @@ -0,0 +1,16 @@ +[ + { + "children": [], + "content": undefined, + "id": "1", + "props": { + "backgroundColor": "default", + "caption": "The harbor at dusk", + "name": "The harbor", + "showPreview": true, + "textAlignment": "left", + "url": "https://example.com/harbor.jpg", + }, + "type": "image", + }, +] \ No newline at end of file diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts index e62e523368..56ba06102f 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -872,6 +872,16 @@ export const parseTestInstancesHTML: TestInstance< }, executeTest: testParseHTML, }, + // An image in the caption of a figure. The caption is text, and the step + // does not change it: the image is dropped, and the words next to it stay + // apart. + { + testCase: { + name: "inlineImageInFigureCaption", + content: `
    The harbor

    The harbor sun at dusk

    `, + }, + executeTest: testParseHTML, + }, // Images in table cells, as GitHub writes them (each image is in a link). A // table cell cannot hold a block, so the image becomes a link with its alt // text. From f4c2dccb550657a0fb6ef1df2d1a660cb8b93e0c Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 19:43:52 +0100 Subject: [PATCH 12/13] test(core): the HTML parse makes its element in the detached document `HTMLToBlocks` now makes the element for the HTML string. The test fails if that element is made in the active document. --- packages/core/src/util/detachedDoc.test.ts | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/core/src/util/detachedDoc.test.ts b/packages/core/src/util/detachedDoc.test.ts index 6a852b1e40..959e65ebe4 100644 --- a/packages/core/src/util/detachedDoc.test.ts +++ b/packages/core/src/util/detachedDoc.test.ts @@ -1,6 +1,7 @@ import { DOMParser } from "prosemirror-model"; import { afterEach, describe, expect, it, vi } from "vite-plus/test"; +import { HTMLToBlocks } from "../api/parsers/html/parseHTML.js"; import { BlockNoteEditor } from "../editor/BlockNoteEditor.js"; describe("the parse rules with HTML that is in the active document", () => { @@ -49,3 +50,30 @@ describe("the parse rules with HTML that is in the active document", () => { ]); }); }); + +describe("the parse of an HTML string", () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + // The string is from outside. In an element of the active document, its + // images load and its event handlers run before the parse reads them. + it("makes no element in the active document", () => { + const editor = BlockNoteEditor.create(); + const createElement = vi.spyOn(document, "createElement"); + + const blocks = HTMLToBlocks( + `

    Text more

    ` + + `
    • Item
      • Nested
    `, + editor.pmSchema, + ); + + expect(createElement).not.toHaveBeenCalled(); + expect(blocks.map((block) => block.type)).toEqual([ + "paragraph", + "image", + "paragraph", + "bulletListItem", + ]); + }); +}); From ff8c060a7e796eec7eccbbf84404a7c3884a0fff Mon Sep 17 00:00:00 2001 From: yousefed Date: Mon, 23 Nov 2026 10:38:45 +0100 Subject: [PATCH 13/13] fix(core): do not move images if the schema has no image block `HTMLToBlocks` ran the image step for every schema. A schema without the image block drops an image in the parse, but the step split the block around the image first: `

    Before after

    ` became two paragraphs. Before this branch it was one paragraph. The step now runs only if the schema has the `image` node. Without the image block, each result is as before this branch, also in a table cell (no link). --- .../core/src/api/parsers/html/parseHTML.ts | 7 ++++-- packages/core/src/util/detachedDoc.test.ts | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/core/src/api/parsers/html/parseHTML.ts b/packages/core/src/api/parsers/html/parseHTML.ts index 252cbf7119..f21302780e 100644 --- a/packages/core/src/api/parsers/html/parseHTML.ts +++ b/packages/core/src/api/parsers/html/parseHTML.ts @@ -80,8 +80,11 @@ export function HTMLToBlocks< htmlNode.innerHTML = html; // The images move out of the text before the list steps. An image in the // paragraph of a list item is then a block for these steps, and a nested - // list after it stays in the list item. - moveImagesOutOfText(htmlNode); + // list after it stays in the list item. Without the image block, the parse + // drops each image, and the text around it must stay one block. + if (pmSchema.nodes["image"]) { + moveImagesOutOfText(htmlNode); + } nestedListsToBlockNoteStructure(htmlNode); preprocessHTMLWhitespace(htmlNode); const parser = DOMParser.fromSchema(pmSchema); diff --git a/packages/core/src/util/detachedDoc.test.ts b/packages/core/src/util/detachedDoc.test.ts index 959e65ebe4..2bf76e03d6 100644 --- a/packages/core/src/util/detachedDoc.test.ts +++ b/packages/core/src/util/detachedDoc.test.ts @@ -2,6 +2,8 @@ import { DOMParser } from "prosemirror-model"; import { afterEach, describe, expect, it, vi } from "vite-plus/test"; import { HTMLToBlocks } from "../api/parsers/html/parseHTML.js"; +import { BlockNoteSchema } from "../blocks/BlockNoteSchema.js"; +import { defaultBlockSpecs } from "../blocks/defaultBlocks.js"; import { BlockNoteEditor } from "../editor/BlockNoteEditor.js"; describe("the parse rules with HTML that is in the active document", () => { @@ -76,4 +78,25 @@ describe("the parse of an HTML string", () => { "bulletListItem", ]); }); + + // A schema without the image block drops an image in the parse. The text + // around the image must then stay one block. + it("does not split a paragraph at an image if the schema has no image block", () => { + const editor = BlockNoteEditor.create({ + schema: BlockNoteSchema.create({ + blockSpecs: { paragraph: defaultBlockSpecs.paragraph }, + }), + }); + + const blocks = HTMLToBlocks( + `

    Before after

    `, + editor.pmSchema, + ); + + expect(blocks).toHaveLength(1); + expect(blocks[0]).toMatchObject({ + type: "paragraph", + content: [{ type: "text", text: "Before after", styles: {} }], + }); + }); });