diff options
author | Sam Ruby <rubys@intertwingly.net> | 2018-07-22 14:13:56 -0700 |
---|---|---|
committer | Vse Mozhet Byt <vsemozhetbyt@gmail.com> | 2018-07-26 00:27:59 +0300 |
commit | 83474ae1e31d8c614f5f58f9285075283e418f4b (patch) | |
tree | b6119f8d79e83ee55d2b0f9370fbdad601e88348 /tools/doc/html.js | |
parent | e83126a8833ace98d13c2be507e40f8b1340952c (diff) | |
download | node-new-83474ae1e31d8c614f5f58f9285075283e418f4b.tar.gz |
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
https://github.com/nodejs/node/pull/21490#issuecomment-406859983
PR-URL: https://github.com/nodejs/node/pull/21936
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'tools/doc/html.js')
-rw-r--r-- | tools/doc/html.js | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/tools/doc/html.js b/tools/doc/html.js index d6e83a8673..ce137f5d11 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -175,9 +175,9 @@ function linkJsTypeDocs(text) { return parts.join('`'); } -// Preprocess stability blockquotes and YAML blocks +// Preprocess headers, stability blockquotes, and YAML blocks. function preprocessElements({ filename }) { - return (tree) => { + return (tree, file) => { const STABILITY_RE = /(.*:)\s*(\d)([\s\S]*)/; let headingIndex = -1; let heading = null; @@ -187,6 +187,22 @@ function preprocessElements({ filename }) { headingIndex = index; heading = node; + // Ensure optional API parameters are not treated as links by + // collapsing all of heading into a single text node. + if (heading.children.length > 1) { + const position = { + start: heading.children[0].position.start, + end: heading.position.end + }; + + heading.children = [{ + type: 'text', + value: file.contents.slice( + position.start.offset, position.end.offset), + position + }]; + } + } else if (node.type === 'html' && common.isYAMLBlock(node.value)) { node.value = parseYAML(node.value); @@ -331,10 +347,9 @@ function buildToc({ filename }) { depth = node.depth; const realFilename = path.basename(realFilenames[0], '.md'); - const headingText = node.children.map((child) => - file.contents.slice(child.position.start.offset, - child.position.end.offset) - ).join('').trim(); + const headingText = file.contents.slice( + node.children[0].position.start.offset, + node.position.end.offset).trim(); const id = getId(`${realFilename}_${headingText}`, idCounters); const hasStability = node.stability !== undefined; |