Skip to content

Commit fed08a8

Browse files
aduh95addaleax
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 8cc7a73 commit fed08a8

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

‎test/doctool/test-doctool-html.js‎

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ try{
99
}
1010

1111
constassert=require('assert');
12-
const{readFile}=require('fs');
12+
const{readFileSync}=require('fs');
1313
constfixtures=require('../common/fixtures');
1414
const{ replaceLinks }=require('../../tools/doc/markdown.js');
1515
consthtml=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
consttestData=[
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>ClassMethod: 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
constspaces=/\s/g;
@@ -144,17 +143,16 @@ testData.forEach(({file, html }) =>{
144143
// Normalize expected data by stripping whitespace.
145144
constexpected=html.replace(spaces,'');
146145

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

154-
constactual=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+
constactual=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
});
Lines changed: 4 additions & 0 deletions
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‎

Lines changed: 7 additions & 5 deletions
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
functionfirstHeader(){
9595
return(tree,file)=>{
96-
file.section='Index';
97-
9896
constheading=find(tree,{type: 'heading'});
99-
if(heading){
100-
consttext=find(heading,{type: 'text'});
101-
if(text)file.section=text.value;
97+
98+
if(heading&&heading.children.length){
99+
constrecursiveTextContent=(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)