Skip to content

Commit 1241b90

Browse files
vsemozhetbytMylesBorins
authored andcommitted
tools: simplify HTML generation
PR-URL: #20307 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent ac05c2b commit 1241b90

File tree

4 files changed

+24
-70
lines changed

4 files changed

+24
-70
lines changed

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ tools/doc/node_modules/js-yaml/package.json:
603603

604604
gen-json = tools/doc/generate.js --format=json $< > $@
605605
gen-html = tools/doc/generate.js --node-version=$(FULLVERSION) --format=html \
606-
--template=doc/template.html --analytics=$(DOCS_ANALYTICS)$< > $@
606+
--analytics=$(DOCS_ANALYTICS)$< > $@
607607

608608
out/doc/api/%.json: doc/api/%.md
609609
$(call available-node, $(gen-json))

‎test/doctool/test-doctool-html.js‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ try{
1010

1111
constassert=require('assert');
1212
constfs=require('fs');
13-
constpath=require('path');
1413
constfixtures=require('../common/fixtures');
1514
constprocessIncludes=require('../../tools/doc/preprocess.js');
1615
consthtml=require('../../tools/doc/html.js');
@@ -107,7 +106,6 @@ testData.forEach((item) =>{
107106
{
108107
input: preprocessed,
109108
filename: 'foo',
110-
template: path.resolve(__dirname,'../../doc/template.html'),
111109
nodeVersion: process.version,
112110
analytics: item.analyticsId,
113111
},

‎tools/doc/generate.js‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const fs = require('fs');
2929

3030
constargs=process.argv.slice(2);
3131
letformat='json';
32-
lettemplate=null;
3332
letfilename=null;
3433
letnodeVersion=null;
3534
letanalytics=null;
@@ -39,8 +38,6 @@ args.forEach(function(arg){
3938
filename=arg;
4039
}elseif(arg.startsWith('--format=')){
4140
format=arg.replace(/^--format=/,'');
42-
}elseif(arg.startsWith('--template=')){
43-
template=arg.replace(/^--template=/,'');
4441
}elseif(arg.startsWith('--node-version=')){
4542
nodeVersion=arg.replace(/^--node-version=/,'');
4643
}elseif(arg.startsWith('--analytics=')){
@@ -71,7 +68,7 @@ function next(er, input){
7168
break;
7269

7370
case'html':
74-
require('./html')({ input, filename,template,nodeVersion, analytics },
71+
require('./html')({ input, filename, nodeVersion, analytics },
7572
(err,html)=>{
7673
if(err)throwerr;
7774
console.log(html);

‎tools/doc/html.js‎

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const common = require('./common.js');
2525
constfs=require('fs');
2626
constmarked=require('marked');
2727
constpath=require('path');
28-
constpreprocess=require('./preprocess.js');
2928
consttypeParser=require('./type-parser.js');
3029

3130
module.exports=toHTML;
@@ -42,76 +41,36 @@ marked.setOptions({
4241
renderer: renderer
4342
});
4443

45-
// TODO(chrisdickinson): never stop vomiting / fix this.
46-
constgtocPath=path.resolve(path.join(
47-
__dirname,
48-
'..',
49-
'..',
50-
'doc',
51-
'api',
52-
'_toc.md'
53-
));
54-
vargtocLoading=null;
55-
vargtocData=null;
44+
constdocPath=path.resolve(__dirname,'..','..','doc');
45+
46+
constgtocPath=path.join(docPath,'api','_toc.md');
47+
constgtocMD=fs.readFileSync(gtocPath,'utf8').replace(/^@\/\/.*$/gm,'');
48+
constgtocHTML=marked(gtocMD).replace(
49+
/<ahref="(.*?)"/g,
50+
(all,href)=>`<a class="nav-${toID(href)}" href="${href}"`
51+
);
52+
53+
consttemplatePath=path.join(docPath,'template.html');
54+
consttemplate=fs.readFileSync(templatePath,'utf8');
55+
5656
vardocCreated=null;
5757
varnodeVersion=null;
5858

5959
/**
60-
* opts: input, filename, template, nodeVersion.
60+
* opts: input, filename, nodeVersion.
6161
*/
6262
functiontoHTML(opts,cb){
63-
consttemplate=opts.template;
64-
6563
nodeVersion=opts.nodeVersion||process.version;
6664
docCreated=opts.input.match(DOC_CREATED_REG_EXP);
6765

68-
if(gtocData){
69-
returnonGtocLoaded();
70-
}
71-
72-
if(gtocLoading===null){
73-
gtocLoading=[onGtocLoaded];
74-
returnloadGtoc(function(err,data){
75-
if(err)throwerr;
76-
gtocData=data;
77-
gtocLoading.forEach(function(xs){
78-
xs();
79-
});
80-
});
81-
}
82-
83-
if(gtocLoading){
84-
returngtocLoading.push(onGtocLoaded);
85-
}
86-
87-
functiononGtocLoaded(){
88-
constlexed=marked.lexer(opts.input);
89-
fs.readFile(template,'utf8',function(er,template){
90-
if(er)returncb(er);
91-
render({
92-
lexed: lexed,
93-
filename: opts.filename,
94-
template: template,
95-
nodeVersion: nodeVersion,
96-
analytics: opts.analytics,
97-
},cb);
98-
});
99-
}
100-
}
101-
102-
functionloadGtoc(cb){
103-
fs.readFile(gtocPath,'utf8',function(err,data){
104-
if(err)returncb(err);
105-
106-
preprocess(gtocPath,data,function(err,data){
107-
if(err)returncb(err);
108-
109-
data=marked(data).replace(/<ahref="(.*?)"/gm,function(a,m){
110-
return`<a class="nav-${toID(m)}" href="${m}"`;
111-
});
112-
returncb(null,data);
113-
});
114-
});
66+
constlexed=marked.lexer(opts.input);
67+
render({
68+
lexed: lexed,
69+
filename: opts.filename,
70+
template: template,
71+
nodeVersion: nodeVersion,
72+
analytics: opts.analytics,
73+
},cb);
11574
}
11675

11776
functiontoID(filename){
@@ -150,7 +109,7 @@ function render(opts, cb){
150109
template=template.replace(/__TOC__/g,toc);
151110
template=template.replace(
152111
/__GTOC__/g,
153-
gtocData.replace(`class="nav-${id}`,`class="nav-${id} active`)
112+
gtocHTML.replace(`class="nav-${id}`,`class="nav-${id} active`)
154113
);
155114

156115
if(opts.analytics){

0 commit comments

Comments
(0)