Skip to content

Commit 9218963

Browse files
committed
Speed up recompilation by using separate navigation and search databases
1 parent ae3daa7 commit 9218963

4 files changed

Lines changed: 38 additions & 13 deletions

File tree

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,17 @@ void_guides_sources = \
6969
void_guides_outputs = \
7070
$(patsubst src/submodules/void-guides/%.md,dist/void-guides/%.html,$(void_guides_sources))
7171

72-
dist/void-guides/db.json: compile-db.js $(void_guides_sources)
73-
mkdir -p dist/void-guides
72+
dist/void-guides/search-db.json obj/void-guides/nav-db.json: \
73+
compile-db.js $(void_guides_sources)
74+
mkdir -p dist/void-guides obj/void-guides
7475
node compile-db.js \
7576
void-guides void-guides/ \
7677
$(sort $(void_guides_sources))
7778

7879
dist/void-guides/%.html: \
7980
src/templates/docs.html src/main.html \
8081
src/submodules/void-guides/%.md src/submodules/void-guides/categories.json \
81-
compile-markdown.js obj/oro-theme.json dist/void-guides/db.json
82+
compile-markdown.js obj/oro-theme.json obj/void-guides/nav-db.json
8283

8384
# Create directories.
8485
mkdir -p "$$(dirname $(subst dist/void-guides/,obj/void-guides/,$@))"
@@ -104,7 +105,7 @@ dist/void-guides/%.html: \
104105
envsubst '$$PAGE_TITLE,$$DOCS_REPO,$$DOCS_MAINHTML,$$DOCS_NAVHTML,$$DOCS_TOCHTML' \
105106
< $(patsubst dist/void-guides/%.html,obj/void-guides/%.in.html,$@) > $@
106107

107-
targets += $(void_guides_outputs)
108+
targets += dist/void-guides/search-db.json $(void_guides_outputs)
108109

109110
# *───────*
110111
# │ Phony

compile-db.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,26 @@ function readFile(path) {
1919
return fs.readFileSync(path, { encoding: "utf-8" });
2020
}
2121

22-
// Parse inputs.
23-
const db = [];
22+
// Variables and constants.
23+
let search = [];
24+
let nav = [];
2425

2526
const repo = process.argv[2];
2627
const prefix = process.argv[3];
2728

28-
const output = `dist/${repo}/db.json`;
29+
const searchOutput = `dist/${repo}/search-db.json`;
30+
const navOutput = `obj/${repo}/nav-db.json`;
31+
32+
// Parse inputs.
2933
const inputs = process.argv.slice(4);
3034

3135
for (const input of inputs) {
32-
// Initialize entry and push it to database.
36+
// Initialize entry and push it to search database.
3337
const entry = {
3438
path: input
3539
};
3640

37-
db.push(entry);
41+
search.push(entry);
3842

3943
// Generate link.
4044
const href = input.substring(15 + prefix.length, input.length - 3);
@@ -50,7 +54,27 @@ for (const input of inputs) {
5054
} else {
5155
entry.title = entry.href;
5256
}
57+
58+
// Push to navigation database without the `content` field.
59+
const navEntry = structuredClone(entry);
60+
delete navEntry.content;
61+
62+
nav.push(navEntry);
5363
}
5464

65+
// Stringify.
66+
search = JSON.stringify(search);
67+
nav = JSON.stringify(nav);
68+
5569
// Write database to disk.
56-
fs.writeFileSync(output, JSON.stringify(db));
70+
fs.writeFileSync(searchOutput, search);
71+
72+
// Write navigation data to disk only if there were changes.
73+
if (fs.existsSync(navOutput)) {
74+
const oldContent = readFile(navOutput);
75+
if (oldContent !== nav) {
76+
fs.writeFileSync(navOutput, nav);
77+
}
78+
} else {
79+
fs.writeFileSync(navOutput, nav);
80+
}

compile-markdown.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const mainOutput = `obj/${repo}/${target}.html`;
3434
const navOutput = `obj/${repo}/${target}.nav.html`;
3535
const tocOutput = `obj/${repo}/${target}.toc.html`;
3636
const nameOutput = `obj/${repo}/${target}.name.txt`;
37-
const db = JSON.parse(readFile(`dist/${repo}/db.json`));
37+
const nav = JSON.parse(readFile(`obj/${repo}/nav-db.json`));
3838

3939
// Fetch highlighter theme.
4040
const oroTheme = JSON.parse(readFile("obj/oro-theme.json"));
@@ -188,7 +188,7 @@ if (fs.existsSync(`src/submodules/${repo}/${prefix}categories.json`)) {
188188
);
189189
}
190190

191-
for (const entry of db) {
191+
for (const entry of nav) {
192192
const path = entry.path;
193193

194194
// Remove common prefix.

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function setTheme(theme) {
157157
`;
158158

159159
// Download and parse database.
160-
const response = await fetch(`/${repo}/db.json`);
160+
const response = await fetch(`/${repo}/search-db.json`);
161161
if (!response.ok) {
162162
// Display error on failure.
163163
navSearch.innerHTML = /* HTML */ `

0 commit comments

Comments
 (0)