Files
node/tools/doc/preprocess.js
Vse Mozhet Byt e2fa5a7e04 tools: remove redundant RegExp flag
PR-URL: https://github.com/nodejs/node/pull/20309
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-04-28 05:08:44 +03:00

39 lines
1.1 KiB
JavaScript

'use strict';
module.exports = processIncludes;
const path = require('path');
const fs = require('fs');
const includeExpr = /^@include\s+([\w-]+)(?:\.md)?$/gmi;
const commentExpr = /^@\/\/.*$/gm;
function processIncludes(inputFile, input, cb) {
const includes = input.match(includeExpr);
if (includes === null)
return cb(null, input.replace(commentExpr, ''));
let errState = null;
let incCount = includes.length;
includes.forEach((include) => {
const fname = include.replace(includeExpr, '$1.md');
const fullFname = path.resolve(path.dirname(inputFile), fname);
fs.readFile(fullFname, 'utf8', function(er, inc) {
if (errState) return;
if (er) return cb(errState = er);
incCount--;
// Add comments to let the HTML generator know
// how the anchors for headings should look like.
inc = `<!-- [start-include:${fname}] -->\n` +
`${inc}\n<!-- [end-include:${fname}] -->\n`;
input = input.split(`${include}\n`).join(`${inc}\n`);
if (incCount === 0)
return cb(null, input.replace(commentExpr, ''));
});
});
}