Skip to content

Commit eb04ba3

Browse files
aw-davidsonMylesBorins
authored andcommitted
doc: add dynamic source code links
Fixes: #33977 PR-URL: #33996 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 2ca6a45 commit eb04ba3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+88
-5
lines changed

doc/api/assert.md

+2

doc/api/async_hooks.md

+2

doc/api/buffer.md

+2

doc/api/child_process.md

+2

doc/api/cluster.md

+2

doc/api/console.md

+2

doc/api/crypto.md

+2

doc/api/dgram.md

+2

doc/api/dns.md

+2

doc/api/domain.md

+2

doc/api/events.md

+2

doc/api/fs.md

+2

doc/api/http.md

+2

doc/api/http2.md

+2

doc/api/https.md

+2

doc/api/inspector.md

+2

doc/api/net.md

+2

doc/api/os.md

+2

doc/api/path.md

+2

doc/api/perf_hooks.md

+2

doc/api/process.md

+2

doc/api/punycode.md

+2

doc/api/querystring.md

+2

doc/api/readline.md

+2

doc/api/repl.md

+2

doc/api/stream.md

+2

doc/api/string_decoder.md

+2

doc/api/timers.md

+2

doc/api/tls.md

+2

doc/api/tracing.md

+2

doc/api/tty.md

+2

doc/api/url.md

+2

doc/api/util.md

+2

doc/api/v8.md

+2

doc/api/vm.md

+2

doc/api/wasi.md

+2

doc/api/worker_threads.md

+2

doc/api/zlib.md

+2

test/doctool/test-doctool-html.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function toHTML({ input, filename, nodeVersion, versions }) {
4141
.use(replaceLinks, { filename, linksMapper: testLinksMapper })
4242
.use(markdown)
4343
.use(html.firstHeader)
44-
.use(html.preprocessText)
44+
.use(html.preprocessText, { nodeVersion })
4545
.use(html.preprocessElements, { filename })
4646
.use(html.buildToc, { filename, apilinks: {} })
4747
.use(remark2rehype, { allowDangerousHTML: true })

tools/doc/common.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ function isYAMLBlock(text) {
77
return /^<!-- YAML/.test(text);
88
}
99

10+
function isSourceLink(text) {
11+
return /^<!-- source_link=([^\s/]+\/)+\w+\.\w+ -->/.test(text);
12+
}
13+
1014
function arrify(value) {
1115
return Array.isArray(value) ? value : [value];
1216
}
@@ -43,4 +47,4 @@ function extractAndParseYAML(text) {
4347
return meta;
4448
}
4549

46-
module.exports = { arrify, isYAMLBlock, extractAndParseYAML };
50+
module.exports = { arrify, isYAMLBlock, isSourceLink, extractAndParseYAML };

tools/doc/generate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async function main() {
8282
const content = await unified()
8383
.use(replaceLinks, { filename, linksMapper })
8484
.use(markdown)
85-
.use(html.preprocessText)
85+
.use(html.preprocessText, { nodeVersion })
8686
.use(json.jsonAPI, { filename })
8787
.use(html.firstHeader)
8888
.use(html.preprocessElements, { filename })

tools/doc/html.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,13 @@ function firstHeader() {
104104

105105
// Handle general body-text replacements.
106106
// For example, link man page references to the actual page.
107-
function preprocessText() {
107+
function preprocessText({ nodeVersion }) {
108108
return (tree) => {
109109
visit(tree, null, (node) => {
110-
if (node.type === 'text' && node.value) {
110+
if (common.isSourceLink(node.value)) {
111+
const [path] = node.value.match(/(?<=<!-- source_link=).*(?= -->)/);
112+
node.value = `<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/${nodeVersion}/${path}">${path}</a></p>`;
113+
} else if (node.type === 'text' && node.value) {
111114
const value = linkJsTypeDocs(linkManPages(node.value));
112115
if (value !== node.value) {
113116
node.type = 'html';

0 commit comments

Comments
 (0)