docs/verify-links: update to disallow path-absolute doc links

This commit is contained in:
Patrik Oldsberg
2020-08-13 18:49:31 +02:00
parent 4b05b9a96a
commit 8ac58ad988
+22 -7
View File
@@ -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}`);