"use client";
import juice from "juice";
import { copyHtml } from "./clipboard";
* Take a full HTML document, extract <body> content, inline all CSS via juice,
* and tag top-level children with data-tool="html-anything" so WeChat trusts the styles.
* Returns the HTML to be pasted into WeChat editor.
*/
export function toWechatHtml(fullHtml: string): string {
if (typeof window === "undefined") return fullHtml;
const doc = new DOMParser().parseFromString(fullHtml, "text/html");
const styles: string[] = [];
doc.querySelectorAll("style").forEach((s) => {
styles.push(s.textContent ?? "");
});
const css = styles.join("\n");
const bodyHtml = doc.body?.innerHTML ?? fullHtml;
const wrap = document.createElement("div");
wrap.innerHTML = bodyHtml;
Array.from(wrap.children).forEach((child) => {
child.setAttribute("data-tool", "html-anything");
});
const tagged = wrap.innerHTML;
let inlined: string;
try {
inlined = juice.inlineContent(tagged, css, {
inlinePseudoElements: true,
preserveImportant: true,
});
} catch {
inlined = tagged;
}
return `<section data-tool="html-anything">${inlined}</section>`;
}
export async function copyToWechat(fullHtml: string): Promise<void> {
const html = toWechatHtml(fullHtml);
await copyHtml(html);
}