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 (!/