diff --git a/docs/content/docs/features/custom-schemas/custom-blocks.mdx b/docs/content/docs/features/custom-schemas/custom-blocks.mdx index 6d2ab2917e..d3668943a4 100644 --- a/docs/content/docs/features/custom-schemas/custom-blocks.mdx +++ b/docs/content/docs/features/custom-schemas/custom-blocks.mdx @@ -188,7 +188,7 @@ type ReactCustomBlockImplementation = { `renderFrame?:` A component that draws a frame around the block's content and its child blocks. See [Block Frames](#block-frames). -`toExternalHTML?:` This component is used whenever the block is being exported to HTML for use outside BlockNote, for example when copying it to the clipboard. If it's not defined, BlockNote will just use `render` (and `renderFrame`, if the block has one) for the HTML conversion. Takes the same props as `render`, and two more: +`toExternalHTML?:` This component is used whenever the block is being exported to HTML for use outside BlockNote, for example when copying it to the clipboard. If it's not defined, BlockNote will just use `render` (and `renderFrame`, if the block has one) for the HTML conversion. If the component renders nothing (`null`), the block is left out of the exported HTML. A [layout block](/docs/features/custom-schemas/layout-blocks) is an exception: it is not left out, and exports as an empty element. Takes the same props as `render`, and two more: - `childrenRef:` A React `ref` that marks where the block's child blocks go, for example inside a `
` element. Without it, the child blocks follow the block. diff --git a/packages/core/src/api/exporters/html/util/serializeBlocksExternalHTML.test.ts b/packages/core/src/api/exporters/html/util/serializeBlocksExternalHTML.test.ts new file mode 100644 index 0000000000..498ed2e36c --- /dev/null +++ b/packages/core/src/api/exporters/html/util/serializeBlocksExternalHTML.test.ts @@ -0,0 +1,83 @@ +import { describe, expect } from "vite-plus/test"; + +import { BlockNoteSchema } from "../../../../blocks/BlockNoteSchema.js"; +import { defaultBlockSpecs } from "../../../../blocks/defaultBlocks.js"; +import { createBlockSpec } from "../../../../schema/index.js"; + +// A block that leaves itself out of the external HTML. +function createOmittedBlock(type: Type) { + return createBlockSpec( + { type, propSchema: {}, content: "none" }, + { + render: () => ({ dom: document.createElement("div") }), + toExternalHTML: () => null, + }, + )(); +} + +function parseHTML(html: string) { + const dom = document.createElement("div"); + dom.innerHTML = html; + return dom; +} + +describe("a block that exports as nothing", () => { + // The serializer puts a child list of a list item into the item before it. + // The item before a block that is left out is not the item of that block. + it("keeps its child list out of the list item before it", async ({ + setupEditor, + }) => { + const { editor } = await setupEditor({ + schema: BlockNoteSchema.create({ + blockSpecs: { + ...defaultBlockSpecs, + omitted: createOmittedBlock("omitted"), + }, + }), + }); + + const dom = parseHTML( + editor.blocksToHTMLLossy([ + { type: "bulletListItem", content: "A" }, + { + type: "omitted", + children: [{ type: "bulletListItem", content: "B" }], + }, + ]), + ); + + expect( + Array.from(dom.querySelectorAll("li"), (item) => item.textContent), + ).toEqual(["A", "B"]); + expect(dom.querySelector("li li")).toBeNull(); + }); + + // A list item type that is left out made an empty list, and the child list + // had no item to go into. + it("is not an item of a list, and its child list follows", async ({ + setupEditor, + }) => { + const { editor } = await setupEditor({ + schema: BlockNoteSchema.create({ + blockSpecs: { + ...defaultBlockSpecs, + bulletListItem: createOmittedBlock("bulletListItem"), + }, + }), + }); + + const dom = parseHTML( + editor.blocksToHTMLLossy([ + { + type: "bulletListItem", + children: [{ type: "numberedListItem", content: "B" }], + }, + ]), + ); + + expect(dom.querySelector("ul")).toBeNull(); + expect( + Array.from(dom.querySelectorAll("ol > li"), (item) => item.textContent), + ).toEqual(["B"]); + }); +}); diff --git a/packages/core/src/api/exporters/html/util/serializeBlocksExternalHTML.ts b/packages/core/src/api/exporters/html/util/serializeBlocksExternalHTML.ts index 0a267d1b43..e37ff2b2a7 100644 --- a/packages/core/src/api/exporters/html/util/serializeBlocksExternalHTML.ts +++ b/packages/core/src/api/exporters/html/util/serializeBlocksExternalHTML.ts @@ -194,7 +194,8 @@ type BlockOutput = { * block's own `toExternalHTML` replaces the default export, frame included. * By default, a layout block is its frame, with the children in the slot, and * any other block is `render`'s output, which the caller puts into the - * block's frame (if it draws one). + * block's frame (if it draws one). The output is `null` if the block's + * `toExternalHTML` leaves the block out of the export. */ function renderBlockOutput( blockImplementation: BlockImplementation, @@ -202,11 +203,14 @@ function renderBlockOutput( editor: BlockNoteEditor, isLayoutBlock: boolean, nestingLevel: number, -): { output: BlockOutput; frame?: { dom: HTMLElement; slot: HTMLElement } } { +): { + output: BlockOutput | null; + frame?: { dom: HTMLElement; slot: HTMLElement }; +} { const external = blockImplementation.toExternalHTML?.call({}, block, editor, { nestingLevel, }); - if (external) { + if (external !== undefined) { return { output: external }; } @@ -286,6 +290,25 @@ function serializeBlock< nestingLevel, ); + if (ret === null) { + // The block leaves itself out of the export. It adds no element and it is + // not an item of a list, so its children go to this place in the output, + // and not into a list item before it. + const childFragment = doc.createDocumentFragment(); + serializeBlocksToFragment( + childFragment, + editor, + blockWithDefaults.children, + serializer, + orderedListItemBlockTypes, + unorderedListItemBlockTypes, + nestingLevel + 1, + options, + ); + fragment.append(childFragment); + return; + } + const elementFragment = doc.createDocumentFragment(); let rootElement: HTMLElement | undefined; diff --git a/packages/core/src/api/parsers/markdown/markdownToHtml.ts b/packages/core/src/api/parsers/markdown/markdownToHtml.ts index 961192df49..63efb690e2 100644 --- a/packages/core/src/api/parsers/markdown/markdownToHtml.ts +++ b/packages/core/src/api/parsers/markdown/markdownToHtml.ts @@ -718,7 +718,8 @@ type Token = /** * HTML block-level tag names (from the CommonMark type-6 list, plus `audio` * which BlockNote serializes as raw HTML since markdown has no shorthand - * for it). When a line starts with `<` followed by one of these tag names, + * for it, and `video`, so that its `` lines stay in the element). + * When a line starts with `<` followed by one of these tag names, * the run of non-blank lines is emitted verbatim as raw HTML rather than * wrapped in a paragraph. */ @@ -786,6 +787,7 @@ const HTML_BLOCK_TAGS = new Set([ "tr", "track", "ul", + "video", ]); function isHtmlBlockStart(line: string): boolean { diff --git a/packages/core/src/blocks/Audio/block.ts b/packages/core/src/blocks/Audio/block.ts index d4bc264027..27ad37fac8 100644 --- a/packages/core/src/blocks/Audio/block.ts +++ b/packages/core/src/blocks/Audio/block.ts @@ -56,10 +56,16 @@ export const audioParse = return undefined; } + // An audio without a URL gives no block. + const audio = parseAudioElement(element as HTMLAudioElement); + if (!audio.url) { + return undefined; + } + const { backgroundColor } = parseDefaultProps(element); return { - ...parseAudioElement(element as HTMLAudioElement), + ...audio, backgroundColor, }; } @@ -72,10 +78,15 @@ export const audioParse = const { targetElement, caption } = parsedFigure; + const audio = parseAudioElement(targetElement as HTMLAudioElement); + if (!audio.url) { + return undefined; + } + const { backgroundColor } = parseDefaultProps(element); return { - ...parseAudioElement(targetElement as HTMLAudioElement), + ...audio, backgroundColor, caption, }; @@ -128,10 +139,9 @@ export const audioToExternalHTML = any >, ) => { + // The upload placeholder (no URL) is not in the export. if (!block.props.url) { - return { - dom: document.createElement("audio"), - }; + return null; } let audio; diff --git a/packages/core/src/blocks/Audio/parseAudioElement.ts b/packages/core/src/blocks/Audio/parseAudioElement.ts index 2a5bec4403..f4a1d91c35 100644 --- a/packages/core/src/blocks/Audio/parseAudioElement.ts +++ b/packages/core/src/blocks/Audio/parseAudioElement.ts @@ -1,5 +1,7 @@ +import { parseMediaUrl } from "../File/helpers/parse/parseMediaUrl.js"; + export const parseAudioElement = (audioElement: HTMLAudioElement) => { - const url = audioElement.src || undefined; + const url = parseMediaUrl(audioElement); return { url }; }; diff --git a/packages/core/src/blocks/File/block.ts b/packages/core/src/blocks/File/block.ts index 8e1ddf622f..1ada6a9b3a 100644 --- a/packages/core/src/blocks/File/block.ts +++ b/packages/core/src/blocks/File/block.ts @@ -37,10 +37,16 @@ export const fileParse = () => (element: HTMLElement) => { return undefined; } + // A file without a URL gives no block. + const embed = parseEmbedElement(element as HTMLEmbedElement); + if (!embed.url) { + return undefined; + } + const { backgroundColor } = parseDefaultProps(element); return { - ...parseEmbedElement(element as HTMLEmbedElement), + ...embed, backgroundColor, }; } @@ -53,10 +59,15 @@ export const fileParse = () => (element: HTMLElement) => { const { targetElement, caption } = parsedFigure; + const embed = parseEmbedElement(targetElement as HTMLEmbedElement); + if (!embed.url) { + return undefined; + } + const { backgroundColor } = parseDefaultProps(element); return { - ...parseEmbedElement(targetElement as HTMLEmbedElement), + ...embed, backgroundColor, caption, }; @@ -74,10 +85,9 @@ export const createFileBlockSpec = createBlockSpec(createFileBlockConfig, { return createFileBlockWrapper(block, editor); }, toExternalHTML(block) { + // The upload placeholder (no URL) is not in the export. if (!block.props.url) { - return { - dom: document.createElement("embed"), - }; + return null; } const fileSrcLink = document.createElement("a"); diff --git a/packages/core/src/blocks/File/helpers/parse/parseEmbedElement.ts b/packages/core/src/blocks/File/helpers/parse/parseEmbedElement.ts index b7a62f5789..5f790754a8 100644 --- a/packages/core/src/blocks/File/helpers/parse/parseEmbedElement.ts +++ b/packages/core/src/blocks/File/helpers/parse/parseEmbedElement.ts @@ -1,5 +1,7 @@ +import { parseMediaUrl } from "./parseMediaUrl.js"; + export const parseEmbedElement = (embedElement: HTMLEmbedElement) => { - const url = embedElement.src || undefined; + const url = parseMediaUrl(embedElement); return { url }; }; diff --git a/packages/core/src/blocks/File/helpers/parse/parseMediaUrl.ts b/packages/core/src/blocks/File/helpers/parse/parseMediaUrl.ts new file mode 100644 index 0000000000..a0ebc786dd --- /dev/null +++ b/packages/core/src/blocks/File/helpers/parse/parseMediaUrl.ts @@ -0,0 +1,33 @@ +/** + * Gives the URL of a media element (``, `