Skip to content

Commit 9cc3584

Browse files
committed
doc: render About this documentation as landing page
1 parent 33f982e commit 9cc3584

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,16 @@ out/doc/api/all.json: $(apidocs_json) tools/doc/alljson.mjs | out/doc/api
871871
$(call available-node, tools/doc/alljson.mjs) \
872872
fi
873873

874+
# Ensure the documentation landing page mirrors About this documentation.
875+
out/doc/api/documentation.html out/doc/api/documentation.json: \
876+
doc/api/index.md
877+
878+
out/doc/api/index.html: out/doc/api/documentation.html doc/api/index.md
879+
cp $< $@
880+
881+
out/doc/api/index.json: out/doc/api/documentation.json doc/api/index.md
882+
cp $< $@
883+
874884
.PHONY: out/doc/api/stability
875885
out/doc/api/stability: out/doc/api/all.json tools/doc/stability.mjs | out/doc/api
876886
@if [ "$(shell $(node_use_icu))" != "true" ]; then \

tools/doc/allhtml.mjs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ let apicontent = '';
2222
const seen = new Set(['all.html', 'index.html']);
2323

2424
for (const link of toc.match(/<a.*?>/g)) {
25-
const href = /href="(.*?)"/.exec(link)[1];
25+
const hrefMatch = /href="(.*?)"/.exec(link);
26+
if (!hrefMatch) continue;
27+
const href = hrefMatch[1];
2628
if (!htmlFiles.includes(href) || seen.has(href)) continue;
2729
const data = fs.readFileSync(new URL(`./${href}`, source), 'utf8');
2830

@@ -69,6 +71,11 @@ for (const link of toc.match(/<a.*?>/g)) {
6971
seen.add(href);
7072
}
7173

74+
const aggregatedToc =
75+
'<details role="navigation" id="toc" open><summary>Table of contents</summary>\n' +
76+
'<ul>\n' + contents + '</ul>\n' +
77+
'</details>\n';
78+
7279
// Replace various mentions of index with all.
7380
let all = toc.replace(/index\.html/g, 'all.html')
7481
.replace('<a href="all.html">', '<a href="index.html">')
@@ -81,12 +88,28 @@ let all = toc.replace(/index\.html/g, 'all.html')
8188
all = all.replace(/<title>.*?\| /, '<title>');
8289

8390
// Insert the combined table of contents.
84-
const tocStart = /<!-- TOC -->/.exec(all);
85-
all = all.slice(0, tocStart.index + tocStart[0].length) +
86-
'<details id="toc" open><summary>Table of contents</summary>\n' +
87-
'<ul>\n' + contents + '</ul>\n' +
88-
'</details>\n' +
89-
all.slice(tocStart.index + tocStart[0].length);
91+
const tocPlaceholder = '<!-- TOC -->';
92+
if (all.includes(tocPlaceholder)) {
93+
all = all.replace(
94+
tocPlaceholder,
95+
`${tocPlaceholder}\n${aggregatedToc}`,
96+
);
97+
} else {
98+
const tocElement = /<details[^>]+id="toc"[^>]*>[\s\S]*?<\/details>/;
99+
if (!tocElement.test(all)) {
100+
throw new Error('Failed to locate a TOC container in index.html');
101+
}
102+
all = all.replace(tocElement, aggregatedToc);
103+
}
104+
105+
const tocPickerRegex =
106+
/<div class="toc"><ul id="toc-picker">[\s\S]*?<\/ul>\s*<\/div>/;
107+
if (tocPickerRegex.test(all)) {
108+
all = all.replace(
109+
tocPickerRegex,
110+
`<div class="toc"><ul id="toc-picker">\n${contents}</ul></div>`,
111+
);
112+
}
90113

91114
// Replace apicontent with the concatenated set of apicontents from each source.
92115
const apiStart = /<\w+ role="main" id="apicontent">\s*/.exec(all);

tools/doc/alljson.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ const seen = new Set(['all.json', 'index.json']);
2828
// Extract (and concatenate) the selected data from each document.
2929
// Expand hrefs found in json to include source HTML file.
3030
for (const link of toc.match(/<a.*?>/g)) {
31-
const href = /href="(.*?)"/.exec(link)[1];
31+
const hrefMatch = /href="(.*?)"/.exec(link);
32+
if (!hrefMatch) continue;
33+
const href = hrefMatch[1];
3234
const json = href.replace('.html', '.json');
3335
if (!jsonFiles.includes(json) || seen.has(json)) continue;
3436
const data = JSON.parse(

0 commit comments

Comments
 (0)