-
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.
Merge pull request #1236 from Agoric/mk/deadlink-checker
chore: add a deadlink checker for nav/sidebar items
- Loading branch information
Showing
4 changed files
with
157 additions
and
48 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 |
---|---|---|
|
@@ -5,14 +5,15 @@ | |
"type": "module", | ||
"scripts": { | ||
"docs:dev": "NODE_OPTIONS=--openssl-legacy-provider vitepress dev main", | ||
"docs:build": "NODE_OPTIONS=--openssl-legacy-provider vitepress build main", | ||
"docs:build": "yarn lint:check-links && NODE_OPTIONS=--openssl-legacy-provider vitepress build main", | ||
"docs:preview": "NODE_OPTIONS=--openssl-legacy-provider vitepress preview main", | ||
"docs:build-cf": "DEBUG='vitepress:*' NODE_OPTIONS=--openssl-legacy-provider vitepress build main && cp _redirects dist/", | ||
"docs:build-cf": "yarn lint:check-links && DEBUG='vitepress:*' NODE_OPTIONS=--openssl-legacy-provider vitepress build main && cp _redirects dist/", | ||
"test": "ava", | ||
"lint-fix": "yarn lint --fix", | ||
"lint": "eslint 'snippets/**/*.js'", | ||
"format": "node scripts/markdown-js-snippets-linter.mjs 'main/**/*.md' --fix && prettier --write '**/*.md' --config .prettierrc.json", | ||
"lint:format": "node scripts/format.mjs", | ||
"lint:check-links": "node scripts/checkLinks.mjs", | ||
"build": "exit 0" | ||
}, | ||
"packageManager": "[email protected]", | ||
|
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,72 @@ | ||
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 noSingleLineComments = content.replace(/\/\/.*$/gm, ''); | ||
const noComments = noSingleLineComments.replace(/\/\*[\s\S]*?\*\//g, ''); | ||
const linkRegex = /link:\s*(['"])([^'"]+)\1/g; | ||
const links = []; | ||
let match; | ||
while ((match = linkRegex.exec(noComments)) !== null) { | ||
links.push(match[2]); | ||
} | ||
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.'); | ||
} |
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import { exec } from 'child_process'; | ||
|
||
exec('node scripts/markdown-js-snippets-linter.mjs "main/**/*.md" && prettier --check "**/*.md" --config .prettierrc.json', (err, stdout, stderr) => { | ||
if (err) { | ||
const modifiedStderr = stderr.replace( | ||
'Run Prettier with --write to fix', | ||
'Run `yarn format` to fix' | ||
); | ||
console.warn(modifiedStderr); | ||
process.exit(1); | ||
} | ||
}); | ||
exec( | ||
'node scripts/markdown-js-snippets-linter.mjs "main/**/*.md" && prettier --check "**/*.md" --config .prettierrc.json', | ||
(err, stdout, stderr) => { | ||
if (err) { | ||
const modifiedStderr = stderr.replace( | ||
'Run Prettier with --write to fix', | ||
'Run `yarn format` to fix', | ||
); | ||
console.warn(modifiedStderr); | ||
process.exit(1); | ||
} | ||
}, | ||
); |
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