Skip to content

Commit 5d6d6fb

Browse files
rubystargos
authored andcommitted
tools: build all.html by combining generated HTML
Combine the toc and api contents from the generated doc/api/*.html files. This ensures that the single page version of the documentation exactly matches the individual pages. PR-URL: #21568Fixes: #20100 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
1 parent c870372 commit 5d6d6fb

2 files changed

Lines changed: 79 additions & 2 deletions

File tree

‎Makefile‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,16 @@ gen-json = tools/doc/generate.js --format=json $< > $@
657657
gen-html = tools/doc/generate.js --node-version=$(FULLVERSION) --format=html \
658658
--analytics=$(DOCS_ANALYTICS)$< > $@
659659

660-
out/doc/api/%.json: doc/api/%.md
660+
out/doc/api/%.json: doc/api/%.md tools/doc/generate.js tools/doc/json.js
661661
$(call available-node, $(gen-json))
662662

663-
out/doc/api/%.html: doc/api/%.md
663+
out/doc/api/%.html: doc/api/%.md tools/doc/generate.js tools/doc/html.js
664664
$(call available-node, $(gen-html))
665665

666+
out/doc/api/all.html: $(filter-out out/doc/api/all.html, $(apidocs_html))\
667+
tools/doc/allhtml.js
668+
$(call available-node, tools/doc/allhtml.js)
669+
666670
.PHONY: docopen
667671
docopen: $(apidocs_html)
668672
@$(PYTHON) -mwebbrowser file://$(PWD)/out/doc/api/all.html

‎tools/doc/allhtml.js‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
// Build all.html by combining the generated toc and apicontent from each
4+
// of the generated html files.
5+
6+
constfs=require('fs');
7+
8+
constsource=`${__dirname}/../../out/doc/api`;
9+
10+
// Get a list of generated API documents.
11+
consthtmlFiles=fs.readdirSync(source,'utf8')
12+
.filter((name)=>name.includes('.html')&&name!=='all.html');
13+
14+
// Read the table of contents.
15+
consttoc=fs.readFileSync(source+'/_toc.html','utf8');
16+
17+
// Extract (and concatenate) the toc and apicontent from each document.
18+
letcontents='';
19+
letapicontent='';
20+
21+
// Identify files that should be skipped. As files are processed, they
22+
// are added to this list to prevent dupes.
23+
constseen={
24+
'all.html': true,
25+
'index.html': true
26+
};
27+
28+
for(constlinkoftoc.match(/<a.*?>/g)){
29+
consthref=/href="(.*?)"/.exec(link)[1];
30+
if(!htmlFiles.includes(href)||seen[href])continue;
31+
constdata=fs.readFileSync(source+'/'+href,'utf8');
32+
33+
// Split the doc.
34+
constmatch=/(<\/ul>\s*)?<\/div>\s*<divid="apicontent">/.exec(data);
35+
36+
contents+=data.slice(0,match.index)
37+
.replace(/[\s\S]*?<divid="toc">\s*<h2>.*?<\/h2>\s*(<ul>\s*)?/,'');
38+
39+
apicontent+=data.slice(match.index+match[0].length)
40+
.replace(/(<\/div>\s*)*\s*<script[\s\S]*/,'')
41+
.replace(/<ahref="(\w[^#"]*)#/g,(match,href)=>{
42+
returnhtmlFiles.includes(href) ? '<a href="#' : match;
43+
})
44+
.trim()+'\n';
45+
46+
// Mark source as seen.
47+
seen[href]=true;
48+
}
49+
50+
// Replace various mentions of _toc with all.
51+
letall=toc.replace(/_toc\.html/g,'all.html')
52+
.replace('_toc.json','all.json')
53+
.replace('api-section-_toc','api-section-all')
54+
.replace('data-id="_toc"','data-id="all"');
55+
56+
// Clean up the title.
57+
all=all.replace(/<title>.*?\|/,'<title>');
58+
59+
// Insert the combined table of contents.
60+
consttocStart=/<divid="toc">\s*<h2>.*?<\/h2>\s*/.exec(all);
61+
all=all.slice(0,tocStart.index+tocStart[0].length)+
62+
'<ul>\n'+contents+'</ul>\n'+
63+
all.slice(tocStart.index+tocStart[0].length);
64+
65+
// Replace apicontent with the concatenated set of apicontents from each source.
66+
constapiStart=/<divid="apicontent">\s*/.exec(all);
67+
constapiEnd=/(\s*<\/div>)*\s*<script/.exec(all);
68+
all=all.slice(0,apiStart.index+apiStart[0].length)+
69+
apicontent+
70+
all.slice(apiEnd.index);
71+
72+
// Write results.
73+
fs.writeFileSync(source+'/all.html',all,'utf8');

0 commit comments

Comments
 (0)