Skip to content

Commit e895d54

Browse files
vsemozhetbyttargos
authored andcommitted
tools: simplify tools/doc/preprocess.js
PR-URL: #19539 Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]>
1 parent 4c3465f commit e895d54

File tree

1 file changed

+18
-38
lines changed

1 file changed

+18
-38
lines changed

tools/doc/preprocess.js

+18-38
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,38 @@
11
'use strict';
22

3-
module.exports = preprocess;
3+
module.exports = processIncludes;
44

55
const path = require('path');
66
const fs = require('fs');
77

8-
const includeExpr = /^@include\s+[\w-]+\.?[a-zA-Z]*$/gmi;
9-
const includeData = {};
10-
11-
function preprocess(inputFile, input, cb) {
12-
input = stripComments(input);
13-
processIncludes(inputFile, input, cb);
14-
}
15-
16-
function stripComments(input) {
17-
return input.replace(/^@\/\/.*$/gmi, '');
18-
}
8+
const includeExpr = /^@include\s+([\w-]+)(?:\.md)?$/gmi;
9+
const commentExpr = /^@\/\/.*$/gmi;
1910

2011
function processIncludes(inputFile, input, cb) {
2112
const includes = input.match(includeExpr);
22-
if (includes === null) return cb(null, input);
13+
if (includes === null)
14+
return cb(null, input.replace(commentExpr, ''));
15+
2316
let errState = null;
2417
let incCount = includes.length;
2518

2619
includes.forEach((include) => {
27-
let fname = include.replace(/^@include\s+/, '');
28-
if (!/\.md$/.test(fname)) fname = `${fname}.md`;
29-
30-
if (includeData.hasOwnProperty(fname)) {
31-
input = input.split(include).join(includeData[fname]);
32-
incCount--;
33-
if (incCount === 0) {
34-
return cb(null, input);
35-
}
36-
}
37-
20+
const fname = include.replace(includeExpr, '$1.md');
3821
const fullFname = path.resolve(path.dirname(inputFile), fname);
22+
3923
fs.readFile(fullFname, 'utf8', function(er, inc) {
4024
if (errState) return;
4125
if (er) return cb(errState = er);
42-
preprocess(inputFile, inc, function(er, inc) {
43-
if (errState) return;
44-
if (er) return cb(errState = er);
45-
incCount--;
46-
47-
// Add comments to let the HTML generator know how the anchors for
48-
// headings should look like.
49-
includeData[fname] = `<!-- [start-include:${fname}] -->\n` +
50-
`${inc}\n<!-- [end-include:${fname}] -->\n`;
51-
input = input.split(`${include}\n`).join(`${includeData[fname]}\n`);
52-
if (incCount === 0) {
53-
return cb(null, input);
54-
}
55-
});
26+
incCount--;
27+
28+
// Add comments to let the HTML generator know
29+
// how the anchors for headings should look like.
30+
inc = `<!-- [start-include:${fname}] -->\n` +
31+
`${inc}\n<!-- [end-include:${fname}] -->\n`;
32+
input = input.split(`${include}\n`).join(`${inc}\n`);
33+
34+
if (incCount === 0)
35+
return cb(null, input.replace(commentExpr, ''));
5636
});
5737
});
5838
}

0 commit comments

Comments
 (0)