// @vitest-environment jsdom import { describe, expect, test } from "vitest"; import { embedPlayer, wordpressExcerpt, wordpressHtmlToBlocks, } from "./wordpressToBlocks"; import type { WordpressAttachment } from "./parseWordpressExport"; import type { BuiltBlock } from "src/utils/paste/htmlToBlocks"; const UPLOADS = "https://blog.example.com/wp-content/uploads"; const convert = ( html: string, attachments: Record = {}, ) => wordpressHtmlToBlocks(html, { parent: "page", attachments }); const fact = (b: BuiltBlock, attribute: string) => b.facts.find((f) => f.entity === b.entityID && f.attribute === attribute) ?.data as { value?: unknown } | undefined; const text = (b: BuiltBlock) => b.parsedContent?.textContent ?? ""; const outline = (blocks: BuiltBlock[]) => blocks .filter((b) => b.parent === "page") .map((b) => `${b.type}${text(b) ? ": " + text(b) : ""}`); describe("wordpressHtmlToBlocks: block editor", () => { test("prose, headings, nested lists, quotes, and code", () => { const r = convert(`

Intro bold link

Head

quoted

centred

let x = 1;

`); expect(outline(r.blocks)).toEqual([ "text: Intro bold link", "heading: Head", "text: Step 1", "blockquote: quoted", "text: centred", "code", "horizontal-rule", ]); const top = r.blocks.filter((b) => b.parent === "page"); const nested = r.blocks.find((b) => text(b) === "flour")!; expect(nested.parent).toBe(top[2].entityID); expect(fact(top[4], "block/text-alignment")?.value).toBe("center"); expect(fact(top[5], "block/code")?.value).toBe("let x = 1;"); }); test("images use the attachment's original file, with alt, width and caption", () => { const r = convert( `
An apple
A caption
`, { "11": { url: `${UPLOADS}/a.png`, width: 1500, height: 1000 } }, ); expect(outline(r.blocks)).toEqual(["image", "text: A caption", "image"]); expect(fact(r.blocks[0], "image/alt")?.value).toBe("An apple"); expect(fact(r.blocks[0], "image/max-width")?.value).toBe(300); expect(fact(r.blocks[0], "block/text-alignment")?.value).toBe("center"); expect(fact(r.blocks[1], "block/text-size")?.value).toBe("small"); expect(fact(r.blocks[2], "image/alt")).toBeUndefined(); expect(r.images).toEqual([ { entityID: r.blocks[0].entityID, url: `${UPLOADS}/a.png`, width: 1500, height: 1000, }, { entityID: r.blocks[2].entityID, url: "https://cdn.example.com/b.jpg", width: null, height: null, }, ]); }); test("an image inside a column or list is lifted out of it", () => { const r = convert( `

left

`, ); expect(outline(r.blocks)).toEqual(["text: left", "image"]); }); test("galleries become a gallery block", () => { const r = convert( ``, { "1": { url: `${UPLOADS}/a.png`, width: 600, height: 400 } }, ); expect(outline(r.blocks)).toEqual(["image-gallery", "text: Two"]); expect(r.images.map((i) => i.url)).toEqual([ `${UPLOADS}/a.png`, `${UPLOADS}/b.png`, ]); }); test("embeds become players where the provider has one, else link cards", () => { const embed = (url: string) => `
\n${url}\n
`; const r = convert( embed("https://www.youtube.com/watch?v=abc123&t=1179s") + embed("https://vimeo.com/1168468796") + embed("https://tidal.com/track/568316544"), ); expect(outline(r.blocks)).toEqual(["embed", "embed", "link"]); expect(fact(r.blocks[0], "embed/url")?.value).toBe( "https://www.youtube.com/embed/abc123?start=1179", ); expect(fact(r.blocks[0], "embed/aspect-ratio")?.value).toBe("16/9"); expect(fact(r.blocks[1], "embed/url")?.value).toBe( "https://player.vimeo.com/video/1168468796", ); expect(fact(r.blocks[2], "link/url")?.value).toBe( "https://tidal.com/track/568316544", ); }); test("audio and video play from WordPress in an html block", () => { const r = convert( `
Clip
`, ); expect(outline(r.blocks)).toEqual(["html", "html", "text: Clip"]); expect(fact(r.blocks[0], "embed/html")?.value).toContain( `