-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54b346b
commit df8351e
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} |