Skip to content

Commit 20371de

Browse files
committed
Change absolute docs links the root relative ones and also resolve some redirects for READMEs that are unchangeable
1 parent fb50a9d commit 20371de

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/utilities/process-readme.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
const url = require('url');
22

3+
const beginsWithDocsDomainRegex = /^https?:\/\/webpack\.js\.org/;
4+
5+
const redirects = {
6+
'/guides/migrating/#json-loader-is-not-required-anymore': '/migrate/3#json-loader-is-not-required-anymore'
7+
};
8+
39
module.exports = function processREADME(body, options = {}) {
410
return body
511
.replace(/[^]*?<div align="center">([^]*?)<\/div>/, (match, content) => {
@@ -14,11 +20,30 @@ module.exports = function processREADME(body, options = {}) {
1420
// EXAMPLE: [Contributing](./.github/CONTRIBUTING.md)
1521
// EXAMPLE: [Contributing](CONTRIBUTING.md)
1622
.replace(/\[([^\]]*)\]\(([^)]+)\)/g, (markdownLink, content, href) => {
23+
const oldHref = href;
24+
1725
if (href.includes('//npmjs.com')) {
1826
href = href.replace('//www.npmjs.com');
1927
}
2028

21-
return `[${content}](${url.resolve(options.source, href)})`;
29+
if (!href.startsWith('#')) {
30+
href = url.resolve(options.source, href);
31+
}
32+
33+
// Modify absolute documenation links to be root relative
34+
if (beginsWithDocsDomainRegex.test(href)) {
35+
href = href.replace(beginsWithDocsDomainRegex, '');
36+
}
37+
38+
if (href in redirects) {
39+
href = redirects[href];
40+
}
41+
42+
if (oldHref !== href) {
43+
console.log('REWRITE URL:', oldHref, '-->', href);
44+
}
45+
46+
return `[${content}](${href})`;
2247
})
2348
// Modify links to keep them within the site
2449
.replace(/https?:\/\/github.com\/(webpack|webpack-contrib)\/([-A-za-z0-9]+-loader\/?)([)"])/g, '/loaders/$2/$3')

0 commit comments

Comments
 (0)