Skip to content

Commit a12d13a

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 8ddbac2 commit a12d13a

File tree

4 files changed

+24
-70
lines changed

4 files changed

+24
-70
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ tools/doc/node_modules/js-yaml/package.json:
652652

653653
gen-json = tools/doc/generate.js --format=json $< > $@
654654
gen-html = tools/doc/generate.js --node-version=$(FULLVERSION) --format=html \
655-
--template=doc/template.html --analytics=$(DOCS_ANALYTICS) $< > $@
655+
--analytics=$(DOCS_ANALYTICS) $< > $@
656656

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

test/doctool/test-doctool-html.js

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ try {
1010

1111
const assert = require('assert');
1212
const fs = require('fs');
13-
const path = require('path');
1413
const fixtures = require('../common/fixtures');
1514
const processIncludes = require('../../tools/doc/preprocess.js');
1615
const html = 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

+1-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const fs = require('fs');
2929

3030
const args = process.argv.slice(2);
3131
let format = 'json';
32-
let template = null;
3332
let filename = null;
3433
let nodeVersion = null;
3534
let analytics = null;
@@ -39,8 +38,6 @@ args.forEach(function(arg) {
3938
filename = arg;
4039
} else if (arg.startsWith('--format=')) {
4140
format = arg.replace(/^--format=/, '');
42-
} else if (arg.startsWith('--template=')) {
43-
template = arg.replace(/^--template=/, '');
4441
} else if (arg.startsWith('--node-version=')) {
4542
nodeVersion = arg.replace(/^--node-version=/, '');
4643
} else if (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) throw err;
7774
console.log(html);

tools/doc/html.js

+22-63
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const common = require('./common.js');
2525
const fs = require('fs');
2626
const marked = require('marked');
2727
const path = require('path');
28-
const preprocess = require('./preprocess.js');
2928
const typeParser = 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-
const gtocPath = path.resolve(path.join(
47-
__dirname,
48-
'..',
49-
'..',
50-
'doc',
51-
'api',
52-
'_toc.md'
53-
));
54-
var gtocLoading = null;
55-
var gtocData = null;
44+
const docPath = path.resolve(__dirname, '..', '..', 'doc');
45+
46+
const gtocPath = path.join(docPath, 'api', '_toc.md');
47+
const gtocMD = fs.readFileSync(gtocPath, 'utf8').replace(/^@\/\/.*$/gm, '');
48+
const gtocHTML = marked(gtocMD).replace(
49+
/<a href="(.*?)"/g,
50+
(all, href) => `<a class="nav-${toID(href)}" href="${href}"`
51+
);
52+
53+
const templatePath = path.join(docPath, 'template.html');
54+
const template = fs.readFileSync(templatePath, 'utf8');
55+
5656
var docCreated = null;
5757
var nodeVersion = null;
5858

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

68-
if (gtocData) {
69-
return onGtocLoaded();
70-
}
71-
72-
if (gtocLoading === null) {
73-
gtocLoading = [onGtocLoaded];
74-
return loadGtoc(function(err, data) {
75-
if (err) throw err;
76-
gtocData = data;
77-
gtocLoading.forEach(function(xs) {
78-
xs();
79-
});
80-
});
81-
}
82-
83-
if (gtocLoading) {
84-
return gtocLoading.push(onGtocLoaded);
85-
}
86-
87-
function onGtocLoaded() {
88-
const lexed = marked.lexer(opts.input);
89-
fs.readFile(template, 'utf8', function(er, template) {
90-
if (er) return cb(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-
function loadGtoc(cb) {
103-
fs.readFile(gtocPath, 'utf8', function(err, data) {
104-
if (err) return cb(err);
105-
106-
preprocess(gtocPath, data, function(err, data) {
107-
if (err) return cb(err);
108-
109-
data = marked(data).replace(/<a href="(.*?)"/gm, function(a, m) {
110-
return `<a class="nav-${toID(m)}" href="${m}"`;
111-
});
112-
return cb(null, data);
113-
});
114-
});
66+
const lexed = 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
function toID(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)