summaryrefslogtreecommitdiff
path: root/scripts/highlight.mjs
blob: 28708669da8658b09c4ff1fe1bc221a44e12fe97 (plain)
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
import { createHighlighter, bundledLanguages } from 'shiki';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFile, writeFile } from 'node:fs/promises';
import { glob } from 'glob';
import { parseDocument } from 'htmlparser2';
import { findAll, findOne, textContent, replaceElement } from 'domutils';
import render from 'dom-serializer';

const __dirname = dirname(fileURLToPath(import.meta.url));

main();

async function main() {
	const LIGHT_THEME = 'github-dark';
	const DARK_THEME = LIGHT_THEME;

	const start = Date.now();

	const highlighter = await createHighlighter({
		themes: [LIGHT_THEME, DARK_THEME],
		langs: Object.keys(bundledLanguages),
	});

	const files = await glob(join(__dirname, '../public/**/**/index.html'));
	await Promise.all(
		files.map((filepath) => highlightFile(highlighter, filepath, { lightTheme: LIGHT_THEME, darkTheme: DARK_THEME }))
	);

	console.log(`Highlighted ${files.length} files in ${Math.round(Date.now() - start)} ms`);
}

async function highlightFile(highlighter, filepath, { lightTheme, darkTheme }) {
	const contents = await readFile(filepath, { encoding: 'utf-8' });
	await writeFile(filepath, highlightHtmlContent(highlighter, contents, { lightTheme, darkTheme }));
}

function highlightHtmlContent(highlighter, htmlContent, { lightTheme, darkTheme }) {
	const doc = parseDocument(htmlContent);
	for (const preNode of findAll((e) => e.name === 'pre', doc.children)) {
		const codeNode = findOne((e) => e.name === 'code', preNode.children);
		if (!codeNode) continue;

		const lang = codeNode.attribs['class']?.replace(/^language-/, '') ?? 'text';
		const code = textContent(codeNode);
		const highlighted = highlighter.codeToHtml(code, {
			lang,
			themes: { light: lightTheme, dark: darkTheme },
		});

		const highlightedPreNode = parseDocument(highlighted).children[0];
		replaceElement(preNode, highlightedPreNode);
	}

	return render(doc);
}