diff --git a/packages/core/src/api/parsers/html/parseHTML.ts b/packages/core/src/api/parsers/html/parseHTML.ts index cfe1a49ba3..f21302780e 100644 --- a/packages/core/src/api/parsers/html/parseHTML.ts +++ b/packages/core/src/api/parsers/html/parseHTML.ts @@ -7,10 +7,12 @@ 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"; import { preprocessHTMLWhitespace } from "./util/normalizeWhitespace.js"; +import { moveImagesOutOfText } from "./util/imagesInText.js"; /** * Removes the placeholder character that the external HTML exporter inserts @@ -74,7 +76,16 @@ export function HTMLToBlocks< I extends InlineContentSchema, S extends StyleSchema, >(html: string, pmSchema: Schema): Block[] { - const htmlNode = nestedListsToBlockNoteStructure(html); + 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. 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/api/parsers/html/util/imagesInText.ts b/packages/core/src/api/parsers/html/util/imagesInText.ts new file mode 100644 index 0000000000..0bc8af4144 --- /dev/null +++ b/packages/core/src/api/parsers/html/util/imagesInText.ts @@ -0,0 +1,99 @@ +import { isAllowedUri } from "../../../../extensions/tiptap-extensions/Link/link.js"; +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 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. + * + * 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, 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. The + // 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, figcaption", + ) + ) { + continue; + } + if (image.closest("td, th")) { + // 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 + // 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); + 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()) { + part.replaceWith(...part.childNodes); + } + } + } + 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/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( ``, ); 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/packages/core/src/util/detachedDoc.test.ts b/packages/core/src/util/detachedDoc.test.ts index 6a852b1e40..2bf76e03d6 100644 --- a/packages/core/src/util/detachedDoc.test.ts +++ b/packages/core/src/util/detachedDoc.test.ts @@ -1,6 +1,9 @@ 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", () => { @@ -49,3 +52,51 @@ 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

` + + ``, + editor.pmSchema, + ); + + expect(createElement).not.toHaveBeenCalled(); + expect(blocks.map((block) => block.type)).toEqual([ + "paragraph", + "image", + "paragraph", + "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: {} }], + }); + }); +}); 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/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/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/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/__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/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/__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/__snapshots__/html/inlineImageInHeading.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInHeading.json new file mode 100644 index 0000000000..00c45b78de --- /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__/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__/html/inlineImageInListItemParagraph.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInListItemParagraph.json new file mode 100644 index 0000000000..00edbd9b4e --- /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/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__/html/inlineImageInParagraph.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/html/inlineImageInParagraph.json new file mode 100644 index 0000000000..a780e42b63 --- /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..dac0c910a4 --- /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..7d5a42500b --- /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__/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/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/__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/__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__/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/inlineImage.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImage.json index 35a0bc23d1..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 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/inlineImageInHeading.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInHeading.json new file mode 100644 index 0000000000..718b2cd0be --- /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/__snapshots__/markdown/inlineImageInListItem.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItem.json new file mode 100644 index 0000000000..cc16af1850 --- /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/inlineImageInListItemWithNestedList.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInListItemWithNestedList.json new file mode 100644 index 0000000000..396ce7e72d --- /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", + }, + { + "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", + }, + ], + "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": "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/__snapshots__/markdown/inlineImageInQuote.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/inlineImageInQuote.json new file mode 100644 index 0000000000..aad7647727 --- /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/__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/__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 f907e0a38b..56ba06102f 100644 --- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts +++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts @@ -713,6 +713,264 @@ 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 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, + }, + // 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, + }, + // 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. + { + testCase: { + name: "inlineImageInListItemParagraph", + content: `
    +
  • +

    Open the menu The menu and select a block

    +
  • +
  • +

    Close the menu

    +
  • +
`, + }, + 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. + { + testCase: { + name: "inlineImageInQuote", + content: `

As the picture shows A diagram the result is good.

`, + }, + 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, + }, + // 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, + }, + // 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, + }, + // 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. + { + testCase: { + name: "inlineImageInTableCell", + content: ` + + + + + + + + + + + + +
LightDark
Light themeDark theme
`, + }, + 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, + }, + // 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", @@ -2213,7 +2471,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 +2480,66 @@ 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. + { + testCase: { + name: "inlineImageInListItem", + content: `- Item ![inline](https://example.com/img.png) text after`, + }, + executeTest: testParseMarkdown, + }, + // 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", + 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: { + name: "inlineImageInQuote", + content: `> Quote ![inline](https://example.com/img.png) text after`, + }, + 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, + }, + // 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: {