Back to packages list

Vals using rehype-document

Description from the NPM package:
rehype plugin to wrap a document around a fragment

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
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";
export async function extractMetadata(markdown: string) {
const file = await unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkFrontmatter)
.process(markdown);
matter(file, { strip: true });
return file.data.matter;
}
const md = `---
title: Hello, world!
---
<p>Some more text</p>`;

utility to strip yaml frontmatter from a md file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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";
export async function stripMetadata(markdown: string) {
const file = await unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkFrontmatter)
.process(markdown);
matter(file, { strip: true });
return String(file);
}

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