Skip to content

Commit b6f3ae8

Browse files
aduh95richardlau
authored andcommitted
tools,doc: allow page titles to contain inline code
Previously the HTML title would be cut to the first text node only. PR-URL: #35003 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Derek Lewis <[email protected]>
1 parent a420184 commit b6f3ae8

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

test/doctool/test-doctool-html.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ try {
99
}
1010

1111
const assert = require('assert');
12-
const { readFile } = require('fs');
12+
const { readFileSync } = require('fs');
1313
const fixtures = require('../common/fixtures');
1414
const { replaceLinks } = require('../../tools/doc/markdown.js');
1515
const html = require('../../tools/doc/html.js');
@@ -58,11 +58,6 @@ function toHTML({ input, filename, nodeVersion, versions }) {
5858
// This HTML will be stripped of all whitespace because we don't currently
5959
// have an HTML parser.
6060
const testData = [
61-
{
62-
file: fixtures.path('sample_document.md'),
63-
html: '<ol><li>fish</li><li>fish</li></ol>' +
64-
'<ul><li>Redfish</li><li>Bluefish</li></ul>'
65-
},
6661
{
6762
file: fixtures.path('order_of_end_tags_5873.md'),
6863
html: '<h3>Static method: Buffer.from(array) <span> ' +
@@ -126,6 +121,10 @@ const testData = [
126121
'href="#foo_see_also" id="foo_see_also">#</a></span></h2><p>Check' +
127122
'out also<a href="https://nodejs.org/">this guide</a></p>'
128123
},
124+
{
125+
file: fixtures.path('document_with_special_heading.md'),
126+
html: '<title>Sample markdown with special heading |',
127+
}
129128
];
130129

131130
const spaces = /\s/g;
@@ -144,17 +143,16 @@ testData.forEach(({ file, html }) => {
144143
// Normalize expected data by stripping whitespace.
145144
const expected = html.replace(spaces, '');
146145

147-
readFile(file, 'utf8', common.mustCall(async (err, input) => {
148-
assert.ifError(err);
149-
const output = toHTML({ input: input,
150-
filename: 'foo',
151-
nodeVersion: process.version,
152-
versions: versions });
146+
const input = readFileSync(file, 'utf8');
147+
148+
const output = toHTML({ input,
149+
filename: 'foo',
150+
nodeVersion: process.version,
151+
versions });
153152

154-
const actual = output.replace(spaces, '');
155-
// Assert that the input stripped of all whitespace contains the
156-
// expected markup.
157-
assert(actual.includes(expected),
158-
`ACTUAL: ${actual}\nEXPECTED: ${expected}`);
159-
}));
153+
const actual = output.replace(spaces, '');
154+
// Assert that the input stripped of all whitespace contains the
155+
// expected markup.
156+
assert(actual.includes(expected),
157+
`ACTUAL: ${actual}\nEXPECTED: ${expected}`);
160158
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Sample `markdown` with _special_ **heading**
2+
3+
Sometimes heading contains more than just one text child, the current file is
4+
there to test just that.

tools/doc/html.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ function toHTML({ input, content, filename, nodeVersion, versions }) {
9393
// Set the section name based on the first header. Default to 'Index'.
9494
function firstHeader() {
9595
return (tree, file) => {
96-
file.section = 'Index';
97-
9896
const heading = find(tree, { type: 'heading' });
99-
if (heading) {
100-
const text = find(heading, { type: 'text' });
101-
if (text) file.section = text.value;
97+
98+
if (heading && heading.children.length) {
99+
const recursiveTextContent = (node) =>
100+
node.value || node.children.map(recursiveTextContent).join('');
101+
file.section = recursiveTextContent(heading);
102+
} else {
103+
file.section = 'Index';
102104
}
103105
};
104106
}

0 commit comments

Comments
 (0)