Markdown to html (with github styling)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
export async function html(markdown: string) {
const [
{ micromark },
{ frontmatter, frontmatterHtml },
{ gfm: gfmMarkdown, gfmHtml },
] = await Promise.all([
import("npm:micromark"),
import("npm:micromark-extension-frontmatter"),
import("npm:micromark-extension-gfm"),
]);
return micromark(markdown, {
extensions: [gfmMarkdown(), frontmatter()],
htmlExtensions: [gfmHtml(), frontmatterHtml()],
});
}
export async function gfm(
markdown: string,
options?: {
title?: string;
favicon?: string | URL;
render?: (markdown: string) => Promise<string>;
},
) {
const render = options?.render ?? html;
const body = await render(markdown);
const faviconURL = new URL(options?.favicon ?? "📝", "https://fav.farm");
return `
<!DOCTYPE html>
<html>
<head>
<title>${options?.title || "Article"}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="${faviconURL.href}" rel="icon">
<link href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.4.0/github-markdown.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/prismjs@v1.29.0/themes/prism.css" rel="stylesheet">
<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;
}
}
</style>
</head>
<body class="markdown-body">
${body}
<script src="https://cdn.jsdelivr.net/npm/prismjs@v1.29.0/components/prism-core.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@v1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
</body>
</html>`;
}

Convert markdown to Html with Github styling

Forked to add remark-mermaid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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";
// This is kind of a pain, but the existing remark/rehype mermaid plugins
// use playwright internally for some reason, which is a non-starter here
// in good ol' valtown!
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);
}

Convert markdown to Html with Github styling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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";
import rehypeCodeTitles from "npm:rehype-code-titles";
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",
],
js: "https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js",
script: [
`document.body.classList.add('markdown-body')`,
// "https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js",
],
})
.use(rehypeStringify)
.use(rehypeCodeTitles)
.process(markdown);
return String(html);
}

Extract yaml front matter from a md string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import rehypeDocument from "https://esm.sh/rehype-document";
import remarkFrontmatter from "https://esm.sh/remark-frontmatter";
import remarkParse from "https://esm.sh/remark-parse";
import remarkStringify from "https://esm.sh/remark-stringify";
import { unified } from "https://esm.sh/unified";
import { matter } from "npm:vfile-matter";
function yamlMetadataHandler() {
return function(tree, file) {
matter(file);
};
}
export async function extractMetadata(key: string, markdown: string) {
const file = await unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkFrontmatter)
.process(markdown);
matter(file, { strip: true });
return file.data.matter[key];
}
1
Next