Skip to content

Commit

Permalink
lint: add script to check deadlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
mujahidkay committed Oct 12, 2024
1 parent 3b40b16 commit 947fe9f
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions scripts/checkLinks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const extractLinks = (content) => {
const linkRegex = /link:\s*'([^']+)'/g;
const links = [];
let match;
while ((match = linkRegex.exec(content)) !== null) {
links.push(match[1]);
}
return links;
}

const fileExists = (filePath) => {
try {
return fs.existsSync(filePath);
} catch (err) {
return false;
}
}

const checkLink = (link) => {
if (link.startsWith('http')) {
return true;
}

const basePath = path.join(__dirname, '../main');
const cleanLink = link.replace(/^\//, '').replace(/\/$/, '');

// Check for index.md in directory
const indexPath = path.join(basePath, cleanLink, 'index.md');
if (fileExists(indexPath)) {
return true;
}

// Check for .md file
const mdPath = path.join(basePath, `${cleanLink}.md`);
if (fileExists(mdPath)) {
return true;
}

return false;
}

const navContent = fs.readFileSync(path.join(__dirname, '../main/.vitepress/themeConfig/nav.js'), 'utf8');
const configContent = fs.readFileSync(path.join(__dirname, '../main/.vitepress/config.mjs'), 'utf8');

const navLinks = extractLinks(navContent);
const configLinks = extractLinks(configContent);
const allLinks = [...new Set([...navLinks, ...configLinks])];

const deadLinks = allLinks.filter(link => !checkLink(link));

if (deadLinks.length > 0) {
console.error('Dead links found:');
deadLinks.forEach(link => console.error(link));
process.exit(1);
} else {
console.log('All links are valid.');
}

0 comments on commit 947fe9f

Please sign in to comment.