tools: simplify HTML generation
PR-URL: https://github.com/nodejs/node/pull/20307 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
parent
a4b4854e2f
commit
ad6a65ba11
2
Makefile
2
Makefile
@ -652,7 +652,7 @@ tools/doc/node_modules/js-yaml/package.json:
|
|||||||
|
|
||||||
gen-json = tools/doc/generate.js --format=json $< > $@
|
gen-json = tools/doc/generate.js --format=json $< > $@
|
||||||
gen-html = tools/doc/generate.js --node-version=$(FULLVERSION) --format=html \
|
gen-html = tools/doc/generate.js --node-version=$(FULLVERSION) --format=html \
|
||||||
--template=doc/template.html --analytics=$(DOCS_ANALYTICS) $< > $@
|
--analytics=$(DOCS_ANALYTICS) $< > $@
|
||||||
|
|
||||||
out/doc/api/%.json: doc/api/%.md
|
out/doc/api/%.json: doc/api/%.md
|
||||||
$(call available-node, $(gen-json))
|
$(call available-node, $(gen-json))
|
||||||
|
@ -10,7 +10,6 @@ try {
|
|||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
|
||||||
const fixtures = require('../common/fixtures');
|
const fixtures = require('../common/fixtures');
|
||||||
const processIncludes = require('../../tools/doc/preprocess.js');
|
const processIncludes = require('../../tools/doc/preprocess.js');
|
||||||
const html = require('../../tools/doc/html.js');
|
const html = require('../../tools/doc/html.js');
|
||||||
@ -107,7 +106,6 @@ testData.forEach((item) => {
|
|||||||
{
|
{
|
||||||
input: preprocessed,
|
input: preprocessed,
|
||||||
filename: 'foo',
|
filename: 'foo',
|
||||||
template: path.resolve(__dirname, '../../doc/template.html'),
|
|
||||||
nodeVersion: process.version,
|
nodeVersion: process.version,
|
||||||
analytics: item.analyticsId,
|
analytics: item.analyticsId,
|
||||||
},
|
},
|
||||||
|
@ -29,7 +29,6 @@ const fs = require('fs');
|
|||||||
|
|
||||||
const args = process.argv.slice(2);
|
const args = process.argv.slice(2);
|
||||||
let format = 'json';
|
let format = 'json';
|
||||||
let template = null;
|
|
||||||
let filename = null;
|
let filename = null;
|
||||||
let nodeVersion = null;
|
let nodeVersion = null;
|
||||||
let analytics = null;
|
let analytics = null;
|
||||||
@ -39,8 +38,6 @@ args.forEach(function(arg) {
|
|||||||
filename = arg;
|
filename = arg;
|
||||||
} else if (arg.startsWith('--format=')) {
|
} else if (arg.startsWith('--format=')) {
|
||||||
format = arg.replace(/^--format=/, '');
|
format = arg.replace(/^--format=/, '');
|
||||||
} else if (arg.startsWith('--template=')) {
|
|
||||||
template = arg.replace(/^--template=/, '');
|
|
||||||
} else if (arg.startsWith('--node-version=')) {
|
} else if (arg.startsWith('--node-version=')) {
|
||||||
nodeVersion = arg.replace(/^--node-version=/, '');
|
nodeVersion = arg.replace(/^--node-version=/, '');
|
||||||
} else if (arg.startsWith('--analytics=')) {
|
} else if (arg.startsWith('--analytics=')) {
|
||||||
@ -71,7 +68,7 @@ function next(er, input) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'html':
|
case 'html':
|
||||||
require('./html')({ input, filename, template, nodeVersion, analytics },
|
require('./html')({ input, filename, nodeVersion, analytics },
|
||||||
(err, html) => {
|
(err, html) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
console.log(html);
|
console.log(html);
|
||||||
|
@ -25,7 +25,6 @@ const common = require('./common.js');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const marked = require('marked');
|
const marked = require('marked');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const preprocess = require('./preprocess.js');
|
|
||||||
const typeParser = require('./type-parser.js');
|
const typeParser = require('./type-parser.js');
|
||||||
|
|
||||||
module.exports = toHTML;
|
module.exports = toHTML;
|
||||||
@ -42,52 +41,29 @@ marked.setOptions({
|
|||||||
renderer: renderer
|
renderer: renderer
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO(chrisdickinson): never stop vomiting / fix this.
|
const docPath = path.resolve(__dirname, '..', '..', 'doc');
|
||||||
const gtocPath = path.resolve(path.join(
|
|
||||||
__dirname,
|
const gtocPath = path.join(docPath, 'api', '_toc.md');
|
||||||
'..',
|
const gtocMD = fs.readFileSync(gtocPath, 'utf8').replace(/^@\/\/.*$/gm, '');
|
||||||
'..',
|
const gtocHTML = marked(gtocMD).replace(
|
||||||
'doc',
|
/<a href="(.*?)"/g,
|
||||||
'api',
|
(all, href) => `<a class="nav-${toID(href)}" href="${href}"`
|
||||||
'_toc.md'
|
);
|
||||||
));
|
|
||||||
var gtocLoading = null;
|
const templatePath = path.join(docPath, 'template.html');
|
||||||
var gtocData = null;
|
const template = fs.readFileSync(templatePath, 'utf8');
|
||||||
|
|
||||||
var docCreated = null;
|
var docCreated = null;
|
||||||
var nodeVersion = null;
|
var nodeVersion = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* opts: input, filename, template, nodeVersion.
|
* opts: input, filename, nodeVersion.
|
||||||
*/
|
*/
|
||||||
function toHTML(opts, cb) {
|
function toHTML(opts, cb) {
|
||||||
const template = opts.template;
|
|
||||||
|
|
||||||
nodeVersion = opts.nodeVersion || process.version;
|
nodeVersion = opts.nodeVersion || process.version;
|
||||||
docCreated = opts.input.match(DOC_CREATED_REG_EXP);
|
docCreated = opts.input.match(DOC_CREATED_REG_EXP);
|
||||||
|
|
||||||
if (gtocData) {
|
|
||||||
return onGtocLoaded();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gtocLoading === null) {
|
|
||||||
gtocLoading = [onGtocLoaded];
|
|
||||||
return loadGtoc(function(err, data) {
|
|
||||||
if (err) throw err;
|
|
||||||
gtocData = data;
|
|
||||||
gtocLoading.forEach(function(xs) {
|
|
||||||
xs();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gtocLoading) {
|
|
||||||
return gtocLoading.push(onGtocLoaded);
|
|
||||||
}
|
|
||||||
|
|
||||||
function onGtocLoaded() {
|
|
||||||
const lexed = marked.lexer(opts.input);
|
const lexed = marked.lexer(opts.input);
|
||||||
fs.readFile(template, 'utf8', function(er, template) {
|
|
||||||
if (er) return cb(er);
|
|
||||||
render({
|
render({
|
||||||
lexed: lexed,
|
lexed: lexed,
|
||||||
filename: opts.filename,
|
filename: opts.filename,
|
||||||
@ -95,23 +71,6 @@ function toHTML(opts, cb) {
|
|||||||
nodeVersion: nodeVersion,
|
nodeVersion: nodeVersion,
|
||||||
analytics: opts.analytics,
|
analytics: opts.analytics,
|
||||||
}, cb);
|
}, cb);
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadGtoc(cb) {
|
|
||||||
fs.readFile(gtocPath, 'utf8', function(err, data) {
|
|
||||||
if (err) return cb(err);
|
|
||||||
|
|
||||||
preprocess(gtocPath, data, function(err, data) {
|
|
||||||
if (err) return cb(err);
|
|
||||||
|
|
||||||
data = marked(data).replace(/<a href="(.*?)"/gm, function(a, m) {
|
|
||||||
return `<a class="nav-${toID(m)}" href="${m}"`;
|
|
||||||
});
|
|
||||||
return cb(null, data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function toID(filename) {
|
function toID(filename) {
|
||||||
@ -150,7 +109,7 @@ function render(opts, cb) {
|
|||||||
template = template.replace(/__TOC__/g, toc);
|
template = template.replace(/__TOC__/g, toc);
|
||||||
template = template.replace(
|
template = template.replace(
|
||||||
/__GTOC__/g,
|
/__GTOC__/g,
|
||||||
gtocData.replace(`class="nav-${id}`, `class="nav-${id} active`)
|
gtocHTML.replace(`class="nav-${id}`, `class="nav-${id} active`)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (opts.analytics) {
|
if (opts.analytics) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user