Refactor linkValidator

This commit is contained in:
Piotr Rogowski 2023-09-11 20:33:10 +02:00
parent 7672457443
commit 8ffcbfa230
No known key found for this signature in database
GPG Key ID: 4A842D702D9C6F8F
1 changed files with 18 additions and 7 deletions

View File

@ -1,8 +1,10 @@
const fs = require('fs');
const glob = require('glob');
const red = (text) => `\x1b[31m${text}\x1b[0m`;
/**
* Load all md and mdx files from /docs and check whether links are not staring with "wiki.fome.tech"
* Check whether links are not staring with "wiki.fome.tech"
* @param {Array<string>} files - List of file names to check
*/
const validateAbsoluteUrls = (files) => {
@ -12,6 +14,8 @@ const validateAbsoluteUrls = (files) => {
/** @type {Array<{ fileName: string, lineContent: string, lineNo: number }>} */
const errors = [];
console.log('Validating absolute URLs...');
files.forEach((fileName) => {
const lines = fs.readFileSync(fileName, 'utf8').split('\n');
@ -28,7 +32,7 @@ const validateAbsoluteUrls = (files) => {
if (errors.length > 0) {
console.log('❌ Failed\n');
console.log(`Absolute URLs to "${wikiUrl}" found in the following files:\n`);
console.log(red(`Absolute URLs to "${wikiUrl}" found in the following files:\n`));
errors.forEach((error) => {
console.log(
`[${error.fileName}:${error.lineNo}] ${error.lineContent.trim()}`
@ -37,12 +41,19 @@ const validateAbsoluteUrls = (files) => {
process.exit(1);
}
}
/**
* Load all md and mdx files from / docs and process them
*/
const main = () => {
const files = glob.sync('docs/**/*.md?(x)');
// process
validateAbsoluteUrls(files);
// validateRelativeUrls(files);
console.log('✅ Ok');
}
const files = glob.sync('docs/**/*.md?(x)');
console.log('Validating links...');
validateAbsoluteUrls(files);
main();