|
5 | 5 | * - Module names (h1)
|
6 | 6 | * - function names (```res sig)
|
7 | 7 | */
|
8 |
| -import unified from "unified"; |
9 |
| -import markdown from "remark-parse"; |
10 |
| -import stringify from "remark-stringify"; |
11 |
| -import slug from "remark-slug"; |
12 | 8 | import glob from "glob";
|
13 | 9 | import path from "path";
|
14 | 10 | import fs from "fs";
|
15 | 11 | import { URL } from 'url';
|
16 | 12 |
|
| 13 | +import { defaultProcessor } from "./markdown.js"; |
| 14 | + |
17 | 15 | const pathname = new URL('.', import.meta.url).pathname;
|
18 | 16 | const __dirname = process.platform !== 'win32' ? pathname : pathname.substring(1)
|
19 | 17 |
|
20 |
| -const headers = options => (tree, file) => { |
21 |
| - const headers = []; |
22 |
| - let mainHeader; |
23 |
| - tree.children.forEach(child => { |
24 |
| - if (child.type === "heading" && child.depth === 1) { |
25 |
| - if (child.children.length > 0) { |
26 |
| - mainHeader = child.children.map(element => element.value).join(""); |
27 |
| - } |
28 |
| - } |
29 |
| - if (child.type === "heading" && child.depth === 2) { |
30 |
| - if (child.children.length > 0) { |
31 |
| - const id = child.data.id || ""; |
32 |
| - const name = child.children.map(element => element.value).join(""); |
33 |
| - headers.push({ name, href: id }); |
34 |
| - } |
35 |
| - } |
36 |
| - }); |
37 |
| - |
38 |
| - file.data = Object.assign({}, file.data, { headers, mainHeader }); |
39 |
| -}; |
40 |
| - |
41 |
| -const codeblocks = options => (tree, file) => { |
42 |
| - const { children } = tree; |
43 |
| - const codeblocks = {}; |
44 |
| - |
45 |
| - const formatter = value => { |
46 |
| - // Strip newlines and weird spacing |
47 |
| - return value |
48 |
| - .replace(/\n/g, " ") |
49 |
| - .replace(/\s+/g, " ") |
50 |
| - .replace(/\(\s+/g, "(") |
51 |
| - .replace(/\s+\)/g, ")"); |
52 |
| - }; |
53 |
| - |
54 |
| - children.forEach(child => { |
55 |
| - if (child.type === "code" && child.value) { |
56 |
| - const { meta, lang } = child; |
57 |
| - if (meta === "sig" && lang === "re") { |
58 |
| - if (codeblocks[lang] == null) { |
59 |
| - codeblocks[lang] = []; |
60 |
| - } |
61 |
| - codeblocks[lang].push(formatter(child.value)); |
62 |
| - } |
63 |
| - } |
64 |
| - }); |
65 |
| - |
66 |
| - file.data = Object.assign({}, file.data, { codeblocks }); |
67 |
| -}; |
68 |
| - |
69 |
| -const processor = unified() |
70 |
| - .use(markdown, { gfm: true }) |
71 |
| - .use(slug) |
72 |
| - .use(stringify) |
73 |
| - .use(headers) |
74 |
| - .use(codeblocks); |
75 |
| - |
76 | 18 | const processFile = filepath => {
|
77 | 19 | const content = fs.readFileSync(filepath, "utf8");
|
78 |
| - const result = processor.processSync(content); |
| 20 | + const result = defaultProcessor.processSync(content); |
79 | 21 |
|
80 | 22 | const pagesPath = path.resolve("./pages");
|
81 | 23 | const relFilepath = path.relative(pagesPath, filepath);
|
|
0 commit comments