import { matches, select, selectAll } from "https://esm.sh/hast-util-select@6";
import { h } from "https://esm.sh/hastscript";
import rehypeDocument from "https://esm.sh/rehype-document";
import rehypeStringify from "https://esm.sh/rehype-stringify";
import remarkGfm from "https://esm.sh/remark-gfm";
import remarkParse from "https://esm.sh/remark-parse";
import remarkRehype from "https://esm.sh/remark-rehype@6";
import { unified } from "https://esm.sh/unified";
const addMermaidScript = () => (tree) => {
const body = select("body", tree);
console.log(body);
const script = `
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
mermaid.initialize({ startOnLoad: false });
await mermaid.run({
querySelector: '.language-mermaid',
});
`;
body.children.push(h("script", { type: "module" }, script), { type: "text", value: "\n" });
};
export async function gfm(markdown: string, options?: { title?: string; favicon?: string }) {
const html = await unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkRehype)
.use(rehypeDocument, {
title: options?.title,
link: [
{ href: `https://fav.farm/${options?.favicon || "📝"}`, rel: "icon" },
],
style: `
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}`,
css: "https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.4.0/github-markdown.min.css",
script: `document.body.classList.add('markdown-body')`,
})
.use(addMermaidScript)
.use(rehypeStringify)
.process(markdown);
return String(html);
}