Skip to content

Commit 83474ae

Browse files
rubysvsemozhetbyt
authored andcommitted
tools: flatten apidoc headers
ensure optional parameters are not treated as markedown links by replacing the children of headers nodes with a single text node containing the raw markup; Fixes issue identified in #21490 (comment) PR-URL: #21936 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent e83126a commit 83474ae

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

tools/doc/html.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ function linkJsTypeDocs(text) {
175175
return parts.join('`');
176176
}
177177

178-
// Preprocess stability blockquotes and YAML blocks
178+
// Preprocess headers, stability blockquotes, and YAML blocks.
179179
function preprocessElements({ filename }) {
180-
return (tree) => {
180+
return (tree, file) => {
181181
const STABILITY_RE = /(.*:)\s*(\d)([\s\S]*)/;
182182
let headingIndex = -1;
183183
let heading = null;
@@ -187,6 +187,22 @@ function preprocessElements({ filename }) {
187187
headingIndex = index;
188188
heading = node;
189189

190+
// Ensure optional API parameters are not treated as links by
191+
// collapsing all of heading into a single text node.
192+
if (heading.children.length > 1) {
193+
const position = {
194+
start: heading.children[0].position.start,
195+
end: heading.position.end
196+
};
197+
198+
heading.children = [{
199+
type: 'text',
200+
value: file.contents.slice(
201+
position.start.offset, position.end.offset),
202+
position
203+
}];
204+
}
205+
190206
} else if (node.type === 'html' && common.isYAMLBlock(node.value)) {
191207
node.value = parseYAML(node.value);
192208

@@ -331,10 +347,9 @@ function buildToc({ filename }) {
331347

332348
depth = node.depth;
333349
const realFilename = path.basename(realFilenames[0], '.md');
334-
const headingText = node.children.map((child) =>
335-
file.contents.slice(child.position.start.offset,
336-
child.position.end.offset)
337-
).join('').trim();
350+
const headingText = file.contents.slice(
351+
node.children[0].position.start.offset,
352+
node.position.end.offset).trim();
338353
const id = getId(`${realFilename}_${headingText}`, idCounters);
339354

340355
const hasStability = node.stability !== undefined;

0 commit comments

Comments
 (0)