diff --git a/Makefile b/Makefile index d46b4a0c18a..21ab5de0b9d 100644 --- a/Makefile +++ b/Makefile @@ -745,7 +745,7 @@ $(LINK_DATA): $(wildcard lib/*.js) tools/doc/apilinks.js $(call available-node, $(gen-apilink)) out/doc/api/%.json out/doc/api/%.html: doc/api/%.md tools/doc/generate.js \ - tools/doc/html.js tools/doc/json.js tools/doc/apilinks.js | $(LINK_DATA) + tools/doc/markdown.js tools/doc/html.js tools/doc/json.js tools/doc/apilinks.js | $(LINK_DATA) $(call available-node, $(gen-api)) out/doc/api/all.html: $(apidocs_html) tools/doc/allhtml.js \ diff --git a/test/doctool/test-doctool-html.js b/test/doctool/test-doctool-html.js index 11b28dc8a2a..ee8099f8cd8 100644 --- a/test/doctool/test-doctool-html.js +++ b/test/doctool/test-doctool-html.js @@ -11,6 +11,7 @@ try { const assert = require('assert'); const { readFile } = require('fs'); const fixtures = require('../common/fixtures'); +const { replaceLinks } = require('../../tools/doc/markdown.js'); const html = require('../../tools/doc/html.js'); const path = require('path'); @@ -22,8 +23,22 @@ const remark2rehype = require('remark-rehype'); const raw = require('rehype-raw'); const htmlStringify = require('rehype-stringify'); +// Test links mapper is an object of the following structure: +// { +// [filename]: { +// [link definition identifier]: [url to the linked resource] +// } +// } +const testLinksMapper = { + 'foo': { + 'command line options': 'cli.html#cli-options', + 'web server': 'example.html' + } +}; + async function toHTML({ input, filename, nodeVersion }) { const content = unified() + .use(replaceLinks, { filename, linksMapper: testLinksMapper }) .use(markdown) .use(html.firstHeader) .use(html.preprocessText) @@ -96,6 +111,21 @@ const testData = [ file: fixtures.path('altdocs.md'), html: '
  • 8.x', }, + { + file: fixtures.path('document_with_links.md'), + html: '

    Usage and Example#' + + '

    Usage#

    node \\[options\\] index.js' + + '

    Please see the' + + 'Command Line Optionsdocument for more information.

    ' + + 'Example' + + '#

    An example of a' + + 'webserverwritten with Node.js which responds with' + + '\'Hello, World!\':

    See also#

    Check' + + 'out alsothis guide

    ' + }, ]; const spaces = /\s/g; diff --git a/test/fixtures/document_with_links.md b/test/fixtures/document_with_links.md new file mode 100644 index 00000000000..1392029a30b --- /dev/null +++ b/test/fixtures/document_with_links.md @@ -0,0 +1,20 @@ +# Usage and Example + +## Usage + +`node \[options\] index.js` + +Please see the [Command Line Options][] document for more information. + +## Example + +An example of a [web server][] written with Node.js which responds with +`'Hello, World!'`: + +## See also + +Check out also [this guide][] + +[Command Line Options]: cli.md#options +[this guide]: https://nodejs.org/ +[web server]: example.md diff --git a/tools/doc/generate.js b/tools/doc/generate.js index cd85d536579..aac8bfd5e7b 100644 --- a/tools/doc/generate.js +++ b/tools/doc/generate.js @@ -29,6 +29,8 @@ const remark2rehype = require('remark-rehype'); const raw = require('rehype-raw'); const htmlStringify = require('rehype-stringify'); +const { replaceLinks } = require('./markdown'); +const linksMapper = require('./links-mapper'); const html = require('./html'); const json = require('./json'); @@ -70,6 +72,7 @@ async function main() { const input = await fs.readFile(filename, 'utf8'); const content = await unified() + .use(replaceLinks, { filename, linksMapper }) .use(markdown) .use(html.preprocessText) .use(json.jsonAPI, { filename }) diff --git a/tools/doc/links-mapper.json b/tools/doc/links-mapper.json new file mode 100644 index 00000000000..158d72c7fe5 --- /dev/null +++ b/tools/doc/links-mapper.json @@ -0,0 +1,6 @@ +{ + "doc/api/synopsis.md": { + "command line options": "cli.html#cli_command_line_options", + "web server": "http.html" + } +} \ No newline at end of file diff --git a/tools/doc/markdown.js b/tools/doc/markdown.js new file mode 100644 index 00000000000..97feadbf999 --- /dev/null +++ b/tools/doc/markdown.js @@ -0,0 +1,21 @@ +'use strict'; + +const visit = require('unist-util-visit'); + +module.exports = { + replaceLinks +}; + +function replaceLinks({ filename, linksMapper }) { + return (tree) => { + const fileHtmlUrls = linksMapper[filename]; + + visit(tree, 'definition', (node) => { + const htmlUrl = fileHtmlUrls && fileHtmlUrls[node.identifier]; + + if (htmlUrl && typeof htmlUrl === 'string') { + node.url = htmlUrl; + } + }); + }; +}