-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile-db.js
More file actions
87 lines (67 loc) · 2.04 KB
/
Copy pathcompile-db.js
File metadata and controls
87 lines (67 loc) · 2.04 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
/*── compile-db.js ── Compilation of database ──*
│
│ 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 * as marked from "marked";
// Utility for reading files to strings.
function readFile(path) {
return fs.readFileSync(path, { encoding: "utf-8" });
}
// 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 inputs = args.slice(3);
// Variables and constants.
let search = [];
let nav = [];
const searchOutput = `dist/${repo}/search-db.json`;
const navOutput = `obj/${repo}/nav-db.json`;
// Parse inputs.
for (const input of inputs) {
// Initialize entry and push it to search database.
const entry = {
path: input
};
search.push(entry);
// Generate link.
const hrefStart = submodule.length + prefix.length + 1;
const hrefEnd = input.length - 3;
const href = input.substring(hrefStart, hrefEnd);
entry.href = `/${repo}/${href}`;
// Read file contents.
entry.content = readFile(input);
// Determine title.
const lines = entry.content.trim().split("\n");
if (lines.length != 0 && lines[0].startsWith("# ")) {
entry.title = await marked.parseInline(lines[0].substring(2));
} else {
entry.title = entry.href;
}
// Push to navigation database without the `content` field.
const navEntry = structuredClone(entry);
delete navEntry.content;
nav.push(navEntry);
}
// Stringify.
search = JSON.stringify(search);
nav = JSON.stringify(nav);
// Write database to disk.
fs.writeFileSync(searchOutput, search);
// Write navigation data to disk only if there were changes.
if (fs.existsSync(navOutput)) {
const oldContent = readFile(navOutput);
if (oldContent !== nav) {
fs.writeFileSync(navOutput, nav);
}
} else {
fs.writeFileSync(navOutput, nav);
}