-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile-markdown.js
More file actions
281 lines (231 loc) · 7.68 KB
/
compile-markdown.js
File metadata and controls
281 lines (231 loc) · 7.68 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*── compile-markdown.js ── Compilation of markdown docs ──*
│
│ Copyright (c) 2025-2026 Deimonn
│
│ This file is licensed under the MIT License.
│
│ See https://deimonn.dev/license.txt for license information.
│
*/
// SPDX-License-Identifier: MIT
import fs from "fs";
import path from "path";
import * as marked from "marked";
import * as markedGfmHeadingId from "marked-gfm-heading-id";
import * as shiki from "shiki";
import sanitizeHtml from "sanitize-html";
// Utility for reading files to strings.
function readFile(path) {
return fs.readFileSync(path, { encoding: "utf-8" });
}
// Utility for escaping HTML tags.
function escapeHtml(text) {
return text
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll("\"", """)
.replaceAll("'", "'");
}
// Utility for removing HTML tags.
function removeHtml(text) {
return sanitizeHtml(text, {
allowedTags: [],
allowedAttributes: {},
disallowedTagsMode: "discard"
});
};
// Fetch arguments.
const args = process.argv.slice(2);
const submodule = args[0];
const repo = args[1];
const prefix = args[2].substring(repo.length + 1);
const target = args[3];
const input = `${submodule}/${prefix}${target}.md`;
const mainOutput = `obj/${repo}/${target}.html`;
const navOutput = `obj/${repo}/${target}.nav.html`;
const tocOutput = `obj/${repo}/${target}.toc.html`;
const nameOutput = `obj/${repo}/${target}.name.txt`;
const nav = JSON.parse(readFile(`obj/${repo}/nav-db.json`));
// Fetch highlighter theme.
const oroTheme = JSON.parse(readFile("obj/oro-theme.json"));
oroTheme.name = "oro";
// Configure highlighting.
const highlighter = await shiki.createHighlighter({
langs: ["c", "cpp", "shell", "xml"],
themes: ["light-plus", oroTheme]
});
const languages = highlighter.getLoadedLanguages();
// Configure markdown compiler.
marked.use(markedGfmHeadingId.gfmHeadingId());
marked.use({
async: true
});
// Compile markdown.
let counter = 0;
let mainHtml = await marked.parse(readFile(input), {
walkTokens: async (token) => {
// Process links.
if (token.type === "link") {
// Same document; nothing to do.
if (token.href.startsWith("#")) {
return;
}
// Escape token text.
const tokenText = escapeHtml(token.text);
// Absolute link; make it open in a new tab.
if (/^[a-z]+:\/\//i.test(token.href)) {
token.type = "html";
token.text = /* HTML */ `
<a href="${token.href}" target="_blank">${tokenText}</a>
`.trim();
return;
}
// Relative link to markdown.
if (/(\.md$)|(\.md(?=#))/.test(token.href)) {
// Remove extension.
token.href = token.href.replace(/(\.md$)|(\.md(?=#))/, "");
// Validate existence.
let file = `${submodule}/${prefix}`;
file += path.dirname(target) + "/";
file += token.href.replace(/#.*$/, "") + ".md";
if (!fs.existsSync(file)) {
console.warn(
`${input}: broken link to file '${file}'`
);
}
return;
}
// Other relative link; point to raw user content.
let dirname = path.dirname(prefix + target);
let href =
`https://raw.githubusercontent.com/deimonn/` +
`${repo}/refs/heads/master/${dirname}/${token.href}`
token.type = "html";
token.text = /* HTML */ `
<a href="${href}" target="_blank">${tokenText}</a>
`.trim();
return;
}
// Process code blocks.
if (token.type === "code") {
// Replace unknown languages with plain text.
let lang = (token.lang ?? "txt").toLowerCase();
if (!languages.includes(lang)) {
lang = "txt";
}
// Update token.
token.type = "html";
token.text = highlighter.codeToHtml(token.text, {
lang,
themes: {
light: "light-plus",
dark: "oro"
}
});
// Add copy button to code block.
token.text = /* HTML */ `
<div class="copy-code">
<sl-copy-button from="codeblock-no${counter}.innerText">
<sl-icon
slot="copy-icon"
name="clipboard-fill"
></sl-icon>
<sl-icon
slot="success-icon"
name="clipboard-check-fill"
></sl-icon>
<sl-icon
slot="error-icon"
name="clipboard-x-fill"
></sl-icon>
</sl-copy-button>
${token.text}
</div>
`;
token.text = token.text.replace(/<code>/, /* HTML */ `
<code id="codeblock-no${counter}">
`.trim());
counter++;
return;
}
// Remove HTML tags.
if (token.type === "html") {
console.warn("removed html tag found in markdown: " + token.text);
token.text = "";
return;
}
}
});
fs.writeFileSync(mainOutput, mainHtml);
// Generate table of contents.
const headings = markedGfmHeadingId.getHeadingList();
let tocHtml = "";
for (const heading of headings) {
tocHtml += /* HTML */ `
<a href="#${heading.id}" style="margin-left: ${heading.level - 1}em">
- ${removeHtml(heading.text)}
</a>
<br>
`;
}
fs.writeFileSync(tocOutput, tocHtml);
// Generate title.
let title;
if (headings.length == 0) {
fs.writeFileSync(nameOutput, title = "untitled");
} else {
fs.writeFileSync(nameOutput, title = headings[0].text);
}
// Classify files into categories as per directory structure.
const categories = {};
let dictionary = {};
if (fs.existsSync(`${submodule}/${prefix}categories.json`)) {
dictionary = JSON.parse(
readFile(`${submodule}/${prefix}categories.json`)
);
}
for (const entry of nav) {
const path = entry.path;
// Remove common prefix.
const file = path.substring(submodule.length + prefix.length + 1);
if (!file.includes("/")) {
continue;
}
// Fetch or create category.
const [category] = file.split("/");
if (categories[category] === undefined) {
categories[category] = {
name: category in dictionary ? dictionary[category] : category,
files: []
};
}
// Push file entry to category.
categories[category].files.push(entry);
}
// Generate navigation.
let navHtml = "";
for (const [_, category] of Object.entries(categories)) {
navHtml += /* HTML */ `<h2>${escapeHtml(category.name)}</h2>`;
for (const file of category.files) {
// Current file, highlighted in bold.
if (input === file.path) {
navHtml += /* HTML */ `
<li id="current-page">
<b>${removeHtml(file.title)}</b>
<br>
</li>
`;
}
// Other files.
else {
navHtml += /* HTML */ `
<li>
<a href="${file.href}">${removeHtml(file.title)}</a>
<br>
</li>
`;
}
}
}
fs.writeFileSync(navOutput, navHtml);