Skip to content

Commit

Permalink
Refactor linkValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
karniv00l committed Sep 11, 2023
1 parent 7672457 commit 8ffcbfa
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions scripts/linkValidator.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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');

Expand All @@ -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()}`
Expand All @@ -37,12 +41,19 @@ const validateAbsoluteUrls = (files) => {

process.exit(1);
}

console.log('✅ Ok');
}

const files = glob.sync('docs/**/*.md?(x)');
/**
* 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('Validating links...');
console.log('✅ Ok');
}

validateAbsoluteUrls(files);
main();

0 comments on commit 8ffcbfa

Please sign in to comment.