Skip to content

Commit

Permalink
refactor replaceLinks plugin to use separate
Browse files Browse the repository at this point in the history
linking json file
  • Loading branch information
Marek Łabuz authored and Trott committed Nov 6, 2019
1 parent 401a989 commit 90277e7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
17 changes: 15 additions & 2 deletions test/doctool/test-doctool-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +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)
.use(replaceLinks, { filename, linksMapper: testLinksMapper })
.use(markdown)
.use(html.firstHeader)
.use(html.preprocessText)
Expand Down Expand Up @@ -103,7 +116,7 @@ const testData = [
html: '<h1>Usage and Example<span><a class="mark"' +
'href="#foo_usage_and_example" id="foo_usage_and_example">#</a>' +
'</span></h1><h2>Usage<span><a class="mark" href="#foo_usage"' +
'id="foo_usage">#</a></span></h2><p><code>node [options] index.js' +
'id="foo_usage">#</a></span></h2><p><code>node \\[options\\] index.js' +
'</code></p><p>Please see the<a href="cli.html#cli-options">' +
'Command Line Options</a>document for more information.</p><h2>' +
'Example<span><a class="mark" href="#foo_example" id="foo_example">' +
Expand Down
3 changes: 0 additions & 3 deletions test/fixtures/document_with_links.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,3 @@ Check out also [this guide][]
[Command Line Options]: cli.md#options
[this guide]: https://nodejs.org/
[web server]: example.md

[Command Line Options `.html`]: cli.html#cli-options
[web server `.html`]: example.html
3 changes: 2 additions & 1 deletion tools/doc/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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');

Expand Down Expand Up @@ -71,7 +72,7 @@ async function main() {
const input = await fs.readFile(filename, 'utf8');

const content = await unified()
.use(replaceLinks)
.use(replaceLinks, { filename, linksMapper })
.use(markdown)
.use(html.preprocessText)
.use(json.jsonAPI, { filename })
Expand Down
6 changes: 6 additions & 0 deletions tools/doc/links-mapper.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"doc/api/synopsis.md": {
"command line options": "cli.html#cli_command_line_options",
"web server": "http.html"
}
}
15 changes: 7 additions & 8 deletions tools/doc/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ module.exports = {
replaceLinks
};

function replaceLinks() {
function replaceLinks({ filename, linksMapper }) {
return (tree) => {
const linksIdenfitiers = tree.children
.filter((child) => child.type === 'definition')
.map((child) => child.identifier);
const fileHtmlUrls = linksMapper[filename];

visit(tree, 'linkReference', (node) => {
const htmlLinkIdentifier = `${node.identifier} \`.html\``;
if (linksIdenfitiers.includes(htmlLinkIdentifier)) {
node.identifier = htmlLinkIdentifier;
visit(tree, 'definition', (node) => {
const htmlUrl = fileHtmlUrls && fileHtmlUrls[node.identifier];

if (htmlUrl && typeof htmlUrl === 'string') {
node.url = htmlUrl;
}
});
};
Expand Down

0 comments on commit 90277e7

Please sign in to comment.