Skip to content

Commit 206a84a

Browse files
doc, tools: rebase from nodejs#6943
1 parent c58bfb1 commit 206a84a

3 files changed

Lines changed: 51 additions & 11 deletions

File tree

test/doctool/test-doctool-html.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const path = require('path');
77

88
common.globalCheck = false;
99

10+
const processIncludes = require('../../tools/doc/preprocess.js');
1011
const html = require('../../tools/doc/html.js').toHTML;
1112

1213
// Test data is a list of objects with two properties.
@@ -55,22 +56,43 @@ const testData = [
5556
'<p>Describe <code>Something</code> in more detail here. ' +
5657
'</p>'
5758
},
59+
{
60+
file: path.join(common.fixturesDir, 'doc_with_includes.md'),
61+
html: '<!-- [start-include:doc_inc_1.md] -->' +
62+
'<p>Look <a href="doc_inc_2.html#doc_inc_2_foobar">here</a>!</p>' +
63+
'<!-- [end-include:doc_inc_1.md] -->' +
64+
'<!-- [start-include:doc_inc_2.md] -->' +
65+
'<h1>foobar<span><a class="mark" href="#doc_inc_2_foobar" ' +
66+
'id="doc_inc_2_foobar">#</a></span></h1>' +
67+
'<p>I exist and am being linked to.</p>' +
68+
'<!-- [end-include:doc_inc_2.md] -->'
69+
},
5870
];
5971

6072
testData.forEach(function(item) {
6173
// Normalize expected data by stripping whitespace
6274
const expected = item.html.replace(/\s/g, '');
6375

64-
fs.readFile(item.file, 'utf8', common.mustCall(function(err, input) {
76+
fs.readFile(item.file, 'utf8', common.mustCall((err, input) => {
6577
assert.ifError(err);
66-
html(input, 'foo', 'doc/template.html',
67-
common.mustCall(function(err, output) {
68-
assert.ifError(err);
78+
processIncludes(item.file, input, common.mustCall((err, preprocessed) => {
79+
assert.ifError(err);
80+
81+
html(
82+
{
83+
input: preprocessed,
84+
filename: 'foo',
85+
template: 'doc/template.html',
86+
nodeVersion: process.version,
87+
},
88+
common.mustCall((err, output) => {
89+
assert.ifError(err);
6990

70-
const actual = output.replace(/\s/g, '');
71-
// Assert that the input stripped of all whitespace contains the
72-
// expected list
73-
assert.notEqual(actual.indexOf(expected), -1);
74-
}));
91+
const actual = output.replace(/\s/g, '');
92+
// Assert that the input stripped of all whitespace contains the
93+
// expected list
94+
assert.notEqual(actual.indexOf(expected), -1);
95+
}));
96+
}));
7597
}));
7698
});

tools/doc/lib/buildToc.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,30 @@ const getId = require('./getId.js');
55
module.exports = function buildToc(lexed, filename, cb) {
66
var toc = [];
77
var depth = 0;
8+
9+
const startIncludeRefRE = /^\s*<!-- \[start-include:(.+)\] -->\s*$/;
10+
const endIncludeRefRE = /^\s*<!-- \[end-include:(.+)\] -->\s*$/;
11+
const realFilenames = [filename];
12+
813
lexed.forEach(function(tok) {
14+
// Keep track of the current filename along @include directives.
15+
if (tok.type === 'html') {
16+
let match;
17+
if ((match = tok.text.match(startIncludeRefRE)) !== null)
18+
realFilenames.unshift(match[1]);
19+
else if (tok.text.match(endIncludeRefRE))
20+
realFilenames.shift();
21+
}
22+
923
if (tok.type !== 'heading') return;
1024
if (tok.depth - depth > 1) {
1125
return cb(new Error('Inappropriate heading level\n' +
1226
JSON.stringify(tok)));
1327
}
1428

1529
depth = tok.depth;
16-
var id = getId(filename + '_' + tok.text.trim());
30+
const realFilename = path.basename(realFilenames[0], '.md');
31+
const id = getId(realFilename + '_' + tok.text.trim());
1732
toc.push(new Array((depth - 1) * 2 + 1).join(' ') +
1833
'* <a href="#' + id + '">' +
1934
tok.text + '</a>');

tools/doc/preprocess.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ function processIncludes(inputFile, input, cb) {
4848
if (errState) return;
4949
if (er) return cb(errState = er);
5050
incCount--;
51-
includeData[fname] = inc;
51+
// Add comments to let the HTML generator know how the anchors for
52+
// headings should look like.
53+
includeData[fname] = `<!-- [start-include:${fname}] -->\n` +
54+
inc + `\n<!-- [end-include:${fname}] -->\n`;
5255
input = input.split(include + '\n').join(includeData[fname] + '\n');
5356
if (incCount === 0) {
5457
return cb(null, input);

0 commit comments

Comments
 (0)