Skip to content

Commit 36f8b82

Browse files
rubystargos
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 5b0c451 commit 36f8b82

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

tools/doc/html.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ function linkJsTypeDocs(text) {
184184
return parts.join('`');
185185
}
186186

187-
// Preprocess stability blockquotes and YAML blocks
187+
// Preprocess headers, stability blockquotes, and YAML blocks.
188188
function preprocessElements({ filename }) {
189-
return (tree) => {
189+
return (tree, file) => {
190190
const STABILITY_RE = /(.*:)\s*(\d)([\s\S]*)/;
191191
let headingIndex = -1;
192192
let heading = null;
@@ -196,6 +196,22 @@ function preprocessElements({ filename }) {
196196
headingIndex = index;
197197
heading = node;
198198

199+
// Ensure optional API parameters are not treated as links by
200+
// collapsing all of heading into a single text node.
201+
if (heading.children.length > 1) {
202+
const position = {
203+
start: heading.children[0].position.start,
204+
end: heading.position.end
205+
};
206+
207+
heading.children = [{
208+
type: 'text',
209+
value: file.contents.slice(
210+
position.start.offset, position.end.offset),
211+
position
212+
}];
213+
}
214+
199215
} else if (node.type === 'html' && common.isYAMLBlock(node.value)) {
200216
node.value = parseYAML(node.value);
201217

@@ -340,10 +356,9 @@ function buildToc({ filename }) {
340356

341357
depth = node.depth;
342358
const realFilename = path.basename(realFilenames[0], '.md');
343-
const headingText = node.children.map((child) =>
344-
file.contents.slice(child.position.start.offset,
345-
child.position.end.offset)
346-
).join('').trim();
359+
const headingText = file.contents.slice(
360+
node.children[0].position.start.offset,
361+
node.position.end.offset).trim();
347362
const id = getId(`${realFilename}_${headingText}`, idCounters);
348363

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

0 commit comments

Comments
 (0)