Skip to content

Commit 72d0d77

Browse files
committed
handle frontmatter
1 parent 8b8ad63 commit 72d0d77

File tree

8 files changed

+239
-158
lines changed

8 files changed

+239
-158
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@rollup/plugin-node-resolve": "^11.2.0",
2929
"@rollup/plugin-sucrase": "^3.1.0",
3030
"@sindresorhus/slugify": "^1.1.0",
31+
"@types/js-yaml": "^4.0.0",
3132
"@types/marked": "^2.0.0",
3233
"@types/prismjs": "^1.16.4",
3334
"js-yaml": "^4.0.0",
@@ -45,6 +46,7 @@
4546
"typescript": "^4.2.3",
4647
"unified": "^9.2.1",
4748
"unist-util-visit": "^2.0.3",
48-
"uvu": "^0.5.1"
49+
"uvu": "^0.5.1",
50+
"vfile-message": "^2.0.4"
4951
}
5052
}

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/format/frontmatter.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { suite } from "uvu";
2+
import * as assert from "uvu/assert";
3+
4+
import { parse_frontmatter } from "./frontmatter";
5+
6+
import unified from "unified";
7+
import markdown from "remark-parse";
8+
import extract_frontmatter from "remark-frontmatter";
9+
import rehype from "remark-rehype";
10+
import stringify from "rehype-stringify";
11+
12+
const processor = unified()
13+
.use(markdown)
14+
.use(extract_frontmatter)
15+
.use(parse_frontmatter)
16+
.use(rehype)
17+
.use(stringify);
18+
19+
const frontmatter = suite("extract-frontmatter");
20+
21+
frontmatter("extracts and parses yaml metadata from frontmatter", async () => {
22+
const src = `---
23+
name: one
24+
secondname: two
25+
---
26+
27+
# hello
28+
29+
---
30+
31+
`;
32+
33+
const output = await processor.process(src);
34+
35+
assert.equal((output.data as any).frontmatter, {
36+
name: "one",
37+
secondname: "two",
38+
});
39+
assert.equal(output.contents, "<h1>hello</h1>\n<hr>");
40+
});
41+
42+
frontmatter("extracts and parses yaml metadata from frontmatter", async () => {
43+
const src = `---
44+
names:
45+
- 1
46+
- 2
47+
- 3
48+
secondname:
49+
hello: friend
50+
goodbye: friend
51+
---
52+
53+
# hello
54+
55+
---
56+
57+
`;
58+
59+
const output = await processor.process(src);
60+
61+
assert.equal((output.data as any).frontmatter, {
62+
names: [1, 2, 3],
63+
secondname: { hello: "friend", goodbye: "friend" },
64+
});
65+
assert.equal(output.contents, "<h1>hello</h1>\n<hr>");
66+
});
67+
68+
frontmatter.run();

src/format/frontmatter.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { Transformer } from "unified";
2+
import { YamlNode } from "remark-frontmatter";
3+
4+
import yaml from "js-yaml";
5+
import Message from "vfile-message";
6+
import visit from "unist-util-visit";
7+
8+
export function parse_frontmatter(): Transformer {
9+
return function transformer(tree, vFile) {
10+
visit(tree, "yaml", (node: YamlNode) => {
11+
try {
12+
const data = yaml.load(node.value) as Record<string, unknown>;
13+
if (data) {
14+
// @ts-ignore
15+
vFile.data.frontmatter = data;
16+
}
17+
} catch (e) {
18+
vFile.messages.push(new Message("YAML failed to parse", e));
19+
}
20+
});
21+
};
22+
}

src/format/highlight.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Transformer } from "unified";
22
import type { Code, HTML } from "mdast";
3-
import visit from "unist-util-visit";
43

4+
import visit from "unist-util-visit";
55
import PrismJS from "prismjs";
66
import "prismjs/components/prism-bash.js";
77
import "prismjs/components/prism-diff.js";

src/format/increment_headings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Transformer } from "unified";
22
import type { Heading, Root } from "mdast";
3+
34
import visit from "unist-util-visit";
45

56
const types = [

0 commit comments

Comments
 (0)