diff --git a/package.json b/package.json index e0050a980265..499f0f18c56d 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "lint:markdown": "markdownlint --config ./.markdownlint.json *.md ./src/content/**/*.md --ignore './src/content/**/_*.md'", "lint:social": "alex . -q", "lint:prose": "cp .proselintrc ~/ && proselint src/content", - "lint:links": "hyperlink -r dist/index.html --canonicalroot https://webpack.js.org/ -i --todo https://img.shields.io | tee internal-links.tap | tap-spot", + "lint:links": "hyperlink -r dist/index.html --canonicalroot https://webpack.js.org/ -i --todo https://img.shields.io --todo 'content-type-mismatch https://travis-ci.org' | tee internal-links.tap | tap-spot", "linkcheck": "hyperlink -r dist/index.html --canonicalroot https://webpack.js.org/ --skip support__ --skip sidecar.gitter.im --skip vimdoc.sourceforge.net --skip img.shields.io --skip npmjs.com/package/ --skip opencollective.com/webpack --todo external-redirect | tee external-links.tap | tap-spot", "sitemap": "cd dist && sitemap-static --prefix=https://webpack.js.org/ > sitemap.xml", "serve": "npm run build && sirv start ./dist --port 4000", diff --git a/src/utilities/process-readme.js b/src/utilities/process-readme.js index 30e162f908a2..8d467253c3fe 100644 --- a/src/utilities/process-readme.js +++ b/src/utilities/process-readme.js @@ -1,5 +1,7 @@ const url = require('url'); +const beginsWithDocsDomainRegex = /^https?:\/\/webpack\.js\.org/; + module.exports = function processREADME(body, options = {}) { return body .replace(/[^]*?
([^]*?)<\/div>/, (match, content) => { @@ -14,11 +16,27 @@ module.exports = function processREADME(body, options = {}) { // EXAMPLE: [Contributing](./.github/CONTRIBUTING.md) // EXAMPLE: [Contributing](CONTRIBUTING.md) .replace(/\[([^\]]*)\]\(([^)]+)\)/g, (markdownLink, content, href) => { + const oldHref = href; + if (href.includes('//npmjs.com')) { href = href.replace('//www.npmjs.com'); } - return `[${content}](${url.resolve(options.source, href)})`; + // Only resolve non-absolute urls from their source if they are not a document fragment link + if (!href.startsWith('#')) { + href = url.resolve(options.source, href); + } + + // Modify absolute documenation links to be root relative + if (beginsWithDocsDomainRegex.test(href)) { + href = href.replace(beginsWithDocsDomainRegex, ''); + } + + if (oldHref !== href) { + console.log('REWRITE URL:', oldHref, '-->', href); + } + + return `[${content}](${href})`; }) // Modify links to keep them within the site .replace(/https?:\/\/github.com\/(webpack|webpack-contrib)\/([-A-za-z0-9]+-loader\/?)([)"])/g, '/loaders/$2/$3')