import { JSDOM } from "jsdom"; import { contentBuilder, escapeHtml, hoist, numberFact, stringFact, type ConvertedContent, } from "src/import/content"; import type { WordpressAttachment } from "./parseWordpressExport"; const LEAFLET_WIDTH = 592; // Everything in a WordPress body other than prose: block-editor figures, the // classic editor's [caption]/[gallery] output, and raw media. A `wp-html` // element stands in for a custom HTML block (see wrapHtmlBlocks). const IMAGE = "figure.wp-block-image, .wp-caption, img"; const GALLERY = "figure.wp-block-gallery, .gallery, .tiled-gallery"; const COMPONENT = [ IMAGE, GALLERY, "figure.wp-block-embed", "figure.wp-block-video", "figure.wp-block-audio", "video", "audio", "iframe", ".wp-block-buttons", "figure.wp-block-table", "table", "wp-html", ].join(","); const ELEMENT_NODE = 1; const TEXT_NODE = 3; const COMMENT_NODE = 8; const positive = (v: unknown): number | null => { const n = Number(v); return n > 0 ? n : null; }; // The player URL for a link WordPress would have turned into an embed, for // the providers whose players can be addressed without an oEmbed lookup. export function embedPlayer( url: string, ): { src: string; aspectRatio?: string; height: number } | null { let u: URL; try { u = new URL(url); } catch { return null; } const host = u.hostname.replace(/^(www|m)\./, ""); if (/^(youtube\.com|youtu\.be|youtube-nocookie\.com)$/.test(host)) { const id = host === "youtu.be" ? u.pathname.split("/")[1] : u.searchParams.get("v") ?? /^\/(?:embed|shorts|live)\/([^/]+)/.exec(u.pathname)?.[1]; if (!id) return null; const start = seconds( u.searchParams.get("t") ?? u.searchParams.get("start"), ); return { src: `https://www.youtube.com/embed/${id}${start ? `?start=${start}` : ""}`, aspectRatio: "16/9", height: 315, }; } if (host === "vimeo.com" || host === "player.vimeo.com") { const id = /\/(\d+)/.exec(u.pathname)?.[1]; if (!id) return null; return { src: `https://player.vimeo.com/video/${id}`, aspectRatio: "16/9", height: 315, }; } if (host === "open.spotify.com") { const m = /^\/(?:embed\/)?(track|album|playlist|episode|show|artist)\/(\w+)/.exec( u.pathname, ); if (!m) return null; return { src: `https://open.spotify.com/embed/${m[1]}/${m[2]}`, height: m[1] === "track" || m[1] === "episode" ? 152 : 352, }; } return null; } // YouTube's t= is either plain seconds ("1179", "1179s") or "1h2m3s". function seconds(t: string | null): number | null { if (!t) return null; if (/^\d+s?$/.test(t)) return positive(parseInt(t)); const m = /^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$/.exec(t); if (!m) return null; return positive( Number(m[1] ?? 0) * 3600 + Number(m[2] ?? 0) * 60 + Number(m[3] ?? 0), ); } const embedFigure = (url: string) => `
${escapeHtml(url)}
`; const shortcodeAttr = (attrs: string, name: string) => new RegExp(`\\b${name}=(["'])(.*?)\\1`).exec(attrs)?.[2] ?? null; // The classic editor's shortcodes for media, as the HTML WordPress would // render them. Other shortcodes (usually a plugin's) are left as written. function expandShortcodes( html: string, attachments: Record, ): string { return html .replace( /\[caption([^\]]*)\]([\s\S]*?)\[\/caption\]/g, (_, attrs: string, inner: string) => { // The caption is whatever follows the image (and its link), unless // it's given as an attribute. const m = /^\s*((?:]*>\s*)?]*>(?:\s*<\/a>)?)([\s\S]*)$/.exec( inner, ); if (!m) return inner; const caption = (shortcodeAttr(attrs, "caption") ?? m[2]).trim(); const align = shortcodeAttr(attrs, "align") ?? ""; return `
${m[1]}${caption ? `
${caption}
` : ""}
`; }, ) .replace(/\[embed[^\]]*\]([\s\S]*?)\[\/embed\]/g, (_, url: string) => embedFigure(url.trim()), ) .replace(/\[gallery([^\]]*)\]/g, (_, attrs: string) => { const ids = (shortcodeAttr(attrs, "ids") ?? "") .split(",") .map((s) => s.trim()) .filter(Boolean); // Without ids a gallery shows every image attached to the post, which // the export doesn't reliably record. if (ids.length === 0) throw new Error("[gallery] shortcode without ids"); const images = ids.map((id) => { const a = attachments[id]; if (!a) throw new Error(`Gallery image ${id} isn't in the export`); return ``; }); return ``; }) .replace( /\[(audio|video)([^\]]*)\](?:\s*\[\/\1\])?/g, (_, kind: string, attrs: string) => { const src = ["src", "mp3", "m4a", "ogg", "wav", "mp4", "m4v", "webm"] .map((a) => shortcodeAttr(attrs, a)) .find(Boolean); if (!src) throw new Error(`[${kind}] shortcode without a source`); return `
<${kind} controls src="${escapeHtml(src)}">
`; }, ); } const BLOCK_START = /^<(p|div|h[1-6]|ul|ol|dl|blockquote|pre|table|figure|hr|iframe|img|video|audio|address|section|!--)\b/i; // The classic editor stores prose with blank lines between paragraphs and // adds the

s when rendering (wpautop). A URL on a line of its own is an // embed. function autop(html: string): string { const pres: string[] = []; const protectedHtml = html .replace(/\r\n?/g, "\n") .replace(//gi, (pre) => { pres.push(pre); return `

`;
    });
  return protectedHtml
    .split(/\n\s*\n/)
    .map((chunk) => chunk.trim())
    .filter(Boolean)
    .map((chunk) => {
      if (/^https?:\/\/\S+$/.test(chunk)) return embedFigure(chunk);
      if (BLOCK_START.test(chunk)) return chunk;
      return `

${chunk.replace(/\n/g, "
")}

`; }) .join("\n") .replace(/
<\/pre>/g, (_, i) => pres[Number(i)]);
}

// A custom HTML block is raw markup between  comments; keep
// it together so it can become one html block rather than being parsed as
// prose.
function wrapHtmlBlocks(doc: Document) {
  const walker = doc.createTreeWalker(doc.body, 128 /* SHOW_COMMENT */);
  const opens: Comment[] = [];
  while (walker.nextNode()) {
    const c = walker.currentNode as Comment;
    if (/^\s*wp:html\b/.test(c.data)) opens.push(c);
  }
  for (const open of opens) {
    const wrapper = doc.createElement("wp-html");
    open.parentNode!.insertBefore(wrapper, open);
    let node = open.nextSibling;
    while (
      node &&
      !(
        node.nodeType === COMMENT_NODE &&
        /^\s*\/wp:html\b/.test(node.textContent ?? "")
      )
    ) {
      const next = node.nextSibling;
      wrapper.appendChild(node);
      node = next;
    }
  }
}

function removeComments(doc: Document) {
  const walker = doc.createTreeWalker(doc.body, 128 /* SHOW_COMMENT */);
  const comments: Node[] = [];
  while (walker.nextNode()) comments.push(walker.currentNode);
  for (const c of comments) c.parentNode?.removeChild(c);
}

function prepare(doc: Document, rewriteLink?: (href: string) => string) {
  wrapHtmlBlocks(doc);
  removeComments(doc);
  for (const el of Array.from(doc.querySelectorAll("script, style, noscript")))
    if (!el.closest("wp-html")) el.remove();

  // A footnote is a  reference plus an item in the footnotes
  // block at the end of the post. Hand the paste pipeline a span carrying
  // the content, the shape it mints footnote entities from.
  const definitions = new Map();
  for (const list of Array.from(doc.querySelectorAll(".wp-block-footnotes"))) {
    for (const li of Array.from(list.querySelectorAll("li[id]"))) {
      for (const back of Array.from(li.querySelectorAll('a[href$="-link"]')))
        back.remove();
      definitions.set(li.id, li);
    }
    list.remove();
  }
  for (const ref of Array.from(doc.querySelectorAll("sup[data-fn]"))) {
    const id = ref.getAttribute("data-fn")!;
    const definition = definitions.get(id);
    if (!definition) throw new Error(`Footnote ${id} has no content`);
    const span = doc.createElement("span");
    span.setAttribute("data-footnote-def", definition.innerHTML.trim());
    span.textContent = ref.textContent;
    ref.replaceWith(span);
  }

  for (const el of Array.from(
    doc.querySelectorAll(
      ".has-text-align-center, .has-text-align-right, [style*='text-align']",
    ),
  )) {
    const align =
      /has-text-align-(center|right)/.exec(el.className)?.[1] ??
      /text-align:\s*(center|right)/.exec(el.getAttribute("style") ?? "")?.[1];
    if (align) el.setAttribute("data-alignment", align);
  }

  if (rewriteLink)
    for (const a of Array.from(doc.querySelectorAll("a[href]")))
      a.setAttribute("href", rewriteLink(a.getAttribute("href")!));

  for (const el of Array.from(doc.querySelectorAll(COMPONENT))) {
    // Only the outermost element of a component is moved; e.g. the 
    // inside an image figure stays where it is.
    if (el.parentElement?.closest(COMPONENT)) continue;
    hoist(el, doc.body);
  }

  // The block editor leaves empty paragraphs behind as spacing.
  for (const p of Array.from(doc.querySelectorAll("p")))
    if (!p.textContent?.trim() && !p.querySelector(COMPONENT)) p.remove();
}

const caption = (html: string | null | undefined) =>
  html?.trim() ? `

${html.trim()}

` : ""; export function wordpressHtmlToBlocks( html: string, opts: { parent: string; attachments: Record; rewriteLink?: (href: string) => string; }, ): ConvertedContent { let source = expandShortcodes(html, opts.attachments); if (!/