Skip to content

Commit 4561432

Browse files
committed
fix headers
1 parent b3cf8e0 commit 4561432

File tree

3 files changed

+60
-86
lines changed

3 files changed

+60
-86
lines changed

scripts/extract-indices.mjs

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,15 @@ import glob from "glob";
99
import path from "path";
1010
import fs from "fs";
1111
import { URL } from 'url';
12-
import remarkRehype from "remark-rehype";
13-
import rehypeSlug from "rehype-slug";
14-
import rehypeStringify from "rehype-stringify";
1512

1613
import { defaultProcessor } from "./markdown.js";
1714

18-
const remarkCodeblocks = options => (tree, file) => {
19-
const { children } = tree;
20-
const codeblocks = {};
21-
22-
const formatter = value => {
23-
// Strip newlines and weird spacing
24-
return value
25-
.replace(/\n/g, " ")
26-
.replace(/\s+/g, " ")
27-
.replace(/\(\s+/g, "(")
28-
.replace(/\s+\)/g, ")");
29-
};
30-
31-
children.forEach(child => {
32-
if (child.type === "code" && child.value) {
33-
const { meta, lang } = child;
34-
if (meta === "sig" && lang === "re") {
35-
if (codeblocks[lang] == null) {
36-
codeblocks[lang] = [];
37-
}
38-
codeblocks[lang].push(formatter(child.value));
39-
}
40-
}
41-
});
42-
43-
file.data = Object.assign({}, file.data, { codeblocks });
44-
};
45-
46-
const rehypeHeaders = options => (tree, file) => {
47-
const headers = [];
48-
let mainHeader;
49-
tree.children.forEach(child => {
50-
if (child.type === "heading" && child.depth === 1) {
51-
if (child.children.length > 0) {
52-
mainHeader = child.children.map(element => element.value).join("");
53-
}
54-
}
55-
if (child.type === "heading" && child.depth === 2) {
56-
if (child.children.length > 0) {
57-
const id = child.data.id || "";
58-
const name = child.children.map(element => element.value).join("");
59-
headers.push({ name, href: id });
60-
}
61-
}
62-
});
63-
64-
file.data = Object.assign({}, file.data, { headers, mainHeader });
65-
};
66-
67-
const processor = defaultProcessor
68-
.use(remarkCodeblocks)
69-
.use(remarkRehype)
70-
.use(rehypeSlug)
71-
.use(rehypeHeaders)
72-
.use(rehypeStringify)
73-
7415
const pathname = new URL('.', import.meta.url).pathname;
7516
const __dirname = process.platform !== 'win32' ? pathname : pathname.substring(1)
7617

7718
const processFile = filepath => {
7819
const content = fs.readFileSync(filepath, "utf8");
79-
const result = processor.processSync(content);
20+
const result = defaultProcessor.processSync(content);
8021

8122
const pagesPath = path.resolve("./pages");
8223
const relFilepath = path.relative(pagesPath, filepath);

scripts/extract-tocs.mjs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,6 @@ const collapseHeaderChildren = (children) => {
5454
}, "");
5555
};
5656

57-
const headers = (options) => (tree, file) => {
58-
const headers = [];
59-
let mainHeader;
60-
tree.children.forEach((child) => {
61-
if (child.type === "heading" && child.depth === 1) {
62-
if (child.children.length > 0) {
63-
mainHeader = collapseHeaderChildren(child.children);
64-
}
65-
}
66-
if (child.type === "heading" && child.depth === 2) {
67-
if (child.children.length > 0) {
68-
// Take the id generated from remark-slug
69-
const headerValue = collapseHeaderChildren(child.children);
70-
const id = child.data.id || "";
71-
headers.push({ name: headerValue, href: id });
72-
}
73-
}
74-
});
75-
76-
file.data = Object.assign({}, file.data, { headers, mainHeader });
77-
};
78-
7957
// sidebarJson: { [category: string]: array<plain_filename_without_ext> }
8058
const processFile = (filepath, sidebarJson = {}) => {
8159
const content = fs.readFileSync(filepath, "utf8");

scripts/markdown.js

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,72 @@
11
import { unified } from "unified";
22
import remarkParse from "remark-parse";
3-
import remarkStringify from "remark-stringify";
43
import remarkGfm from "remark-gfm";
54
import remarkFrontmatter from "remark-frontmatter";
5+
import remarkRehype from "remark-rehype";
6+
import rehypeSlug from "rehype-slug";
7+
import rehypeStringify from "rehype-stringify";
68
import { matter } from "vfile-matter";
79

8-
export const vfileMatter = options => (tree, file) => {
10+
const remarkVfileMatter = options => (tree, file) => {
911
matter(file);
1012
};
1113

14+
const remarkCodeblocks = options => (tree, file) => {
15+
const { children } = tree;
16+
const codeblocks = {};
17+
18+
const formatter = value => {
19+
// Strip newlines and weird spacing
20+
return value
21+
.replace(/\n/g, " ")
22+
.replace(/\s+/g, " ")
23+
.replace(/\(\s+/g, "(")
24+
.replace(/\s+\)/g, ")");
25+
};
26+
27+
children.forEach(child => {
28+
if (child.type === "code" && child.value) {
29+
const { meta, lang } = child;
30+
if (meta === "sig" && lang === "re") {
31+
if (codeblocks[lang] == null) {
32+
codeblocks[lang] = [];
33+
}
34+
codeblocks[lang].push(formatter(child.value));
35+
}
36+
}
37+
});
38+
39+
Object.assign(file.data, { codeblocks });
40+
};
41+
42+
const rehypeHeaders = options => (tree, file) => {
43+
const headers = [];
44+
let mainHeader;
45+
tree.children.forEach(child => {
46+
if (child.tagName === "h1") {
47+
if (child.children.length > 0) {
48+
mainHeader = child.children.map(element => element.value).join("");
49+
}
50+
}
51+
if (child.tagName === "h2") {
52+
if (child.children.length > 0) {
53+
const id = child.properties.id || "";
54+
const name = child.children.map(element => element.value).join("");
55+
headers.push({ name, href: id });
56+
}
57+
}
58+
});
59+
60+
Object.assign(file.data, { headers, mainHeader });
61+
};
62+
1263
export const defaultProcessor = unified()
1364
.use(remarkParse)
14-
.use(remarkStringify)
1565
.use(remarkGfm)
1666
.use(remarkFrontmatter, [{ type: 'yaml', marker: '-' }])
17-
.use(vfileMatter);
67+
.use(remarkVfileMatter)
68+
.use(remarkCodeblocks)
69+
.use(remarkRehype)
70+
.use(rehypeSlug)
71+
.use(rehypeHeaders)
72+
.use(rehypeStringify);

0 commit comments

Comments
 (0)