From 8ac58ad9880617fbffb246c64925c5fad810ed8a Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 13 Aug 2020 18:49:31 +0200 Subject: [PATCH] docs/verify-links: update to disallow path-absolute doc links --- docs/verify-links.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/docs/verify-links.js b/docs/verify-links.js index 1d6d1ec714..a218cde71b 100755 --- a/docs/verify-links.js +++ b/docs/verify-links.js @@ -42,14 +42,25 @@ async function verifyUrl(basePath, url) { } // Only verify existence of local files for now, so skip anything with a schema - if (!url.match(/[a-z]+:/)) { - const path = url.startsWith('/') - ? resolvePath(projectRoot, `.${url}`) - : resolvePath(dirname(resolvePath(projectRoot, basePath)), url); - const exists = await fs.pathExists(path); - if (!exists) { - return { url, basePath, problem: 'missing' }; + if (url.match(/[a-z]+:/)) { + return; + } + + let path = ''; + + if (url.startsWith('/')) { + if (url.startsWith('/docs/') && basePath.match(/^(?:docs|microsite)\//)) { + return { url, basePath, problem: 'not-relative' }; } + + path = resolvePath(projectRoot, `.${url}`); + } else { + path = resolvePath(dirname(resolvePath(projectRoot, basePath)), url); + } + + const exists = await fs.pathExists(path); + if (!exists) { + return { url, basePath, problem: 'missing' }; } return; @@ -93,6 +104,10 @@ async function main() { for (const { url, basePath, problem } of badUrls) { if (problem === 'missing') { console.error(`Unable to reach ${url}, linked from ${basePath}`); + } else if (problem === 'not-relative') { + console.error('Links to /docs/ must be relative'); + console.error(` From: ${basePath}`); + console.error(` To: ${url}`); } else if (problem === 'absolute') { console.error(`Link to docs/ should be replaced by a relative URL`); console.error(` From: ${basePath}`);