mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
This helps catch broken links as part of the test suite. This also improves the user experience when browsing the markdown files. PR-URL: https://github.com/nodejs/node/pull/35191 Fixes: https://github.com/nodejs/node/issues/35189 Reviewed-By: Derek Lewis <DerekNonGeneric@inf.is> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
33 lines
742 B
JavaScript
33 lines
742 B
JavaScript
'use strict';
|
|
|
|
const visit = require('unist-util-visit');
|
|
|
|
const referenceToLocalMdFile = /^(?![+a-z]+:)([^#?]+)\.md(#.+)?$/i;
|
|
|
|
module.exports = {
|
|
replaceLinks,
|
|
referenceToLocalMdFile,
|
|
};
|
|
|
|
function replaceLinks({ filename, linksMapper }) {
|
|
return (tree) => {
|
|
const fileHtmlUrls = linksMapper[filename];
|
|
|
|
visit(tree, (node) => {
|
|
if (node.url) {
|
|
node.url = node.url.replace(
|
|
referenceToLocalMdFile,
|
|
(_, filename, hash) => `${filename}.html${hash || ''}`
|
|
);
|
|
}
|
|
});
|
|
visit(tree, 'definition', (node) => {
|
|
const htmlUrl = fileHtmlUrls && fileHtmlUrls[node.identifier];
|
|
|
|
if (htmlUrl && typeof htmlUrl === 'string') {
|
|
node.url = htmlUrl;
|
|
}
|
|
});
|
|
};
|
|
}
|