-
Notifications
You must be signed in to change notification settings - Fork 22
/
plugin-frontmatter.js
44 lines (43 loc) · 1.2 KB
/
plugin-frontmatter.js
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
const matter = require("gray-matter");
const attrs = require("markdown-it-attrs");
const md = require("markdown-it")().use(attrs, {
allowedAttributes: ["prereq", "hide", "synopsis"],
});
const cheerio = require("cheerio");
module.exports = (options = {}, context) => ({
extendPageData($page) {
let description = "";
let frontmatter = {};
try {
const $ = cheerio.load(md.render($page._content));
description = $("[synopsis]").text();
} catch {
console.log(
`Error in processing description: $page.content is ${$page._content}`
);
}
try {
frontmatter = matter($page._content, { delims: ["<!--", "-->"] }).data;
} catch {
console.log(
`Error in processing frontmatter: $page.content is ${$page._content}`
);
}
$page.frontmatter = {
description,
...$page.frontmatter,
...frontmatter,
};
try {
const tokens = md.parse($page._content, {});
tokens.forEach((t, i) => {
if (t.type === "heading_open" && ["h1"].includes(t.tag)) {
$page.title = tokens[i + 1].content;
return;
}
});
} catch {
console.log(`Error in processing headings.`);
}
},
});