|
| 1 | +import { exec } from "node:child_process"; |
| 2 | +import { readFile, stat } from "node:fs/promises"; |
| 3 | +import { parse } from "./parse.js"; |
| 4 | +import path, { dirname, normalize } from "node:path/posix"; |
| 5 | +import { isAbsolute } from "node:path"; |
| 6 | +const findMarkdownFilesInGit = async () => { |
| 7 | + return await new Promise((resolve, reject) => { |
| 8 | + exec("git ls-files -z", (error, stdout, stderr) => { |
| 9 | + if (error) |
| 10 | + reject(error); |
| 11 | + if (stderr) |
| 12 | + reject(new Error(`git ls-files outputted on stderr: ${stderr}`)); |
| 13 | + else |
| 14 | + resolve(stdout.split("\0").filter((s) => s.endsWith(".md"))); |
| 15 | + }); |
| 16 | + }); |
| 17 | +}; |
| 18 | +const findMarkdownFiles = async () => { |
| 19 | + const ignorePattern = /^(README|LICENSE|contributing\/)/; |
| 20 | + return (await findMarkdownFilesInGit()).filter((f) => !ignorePattern.test(f)); |
| 21 | +}; |
| 22 | +const scanForLinks = async (filenames) => { |
| 23 | + return Promise.all(filenames.map(async (filename) => { |
| 24 | + const content = await readFile(filename, "utf-8"); |
| 25 | + return { filename, ...parse(content) }; |
| 26 | + })); |
| 27 | +}; |
| 28 | +const externalLinkPattern = /^\w+:/; |
| 29 | +const isExternalLink = (t) => externalLinkPattern.test(t); |
| 30 | +const main = async () => { |
| 31 | + const markdownFilenames = await findMarkdownFiles(); |
| 32 | + const parsedFiles = await scanForLinks(markdownFilenames); |
| 33 | + let errors = 0; |
| 34 | + for (const parsedFile of parsedFiles) { |
| 35 | + for (const img of parsedFile.images) { |
| 36 | + if (!isExternalLink(img.src)) { |
| 37 | + const resolved = path.join(dirname(parsedFile.filename), img.src); |
| 38 | + const exists = await stat(resolved).then(() => true, () => false); |
| 39 | + if (!exists) { |
| 40 | + console.log(`error BROKEN-INTERNAL-IMAGE ${parsedFile.filename}:0 Broken internal image reference ${img.src}`); |
| 41 | + ++errors; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + for (const link of parsedFile.links) { |
| 46 | + if (link.target.startsWith("#")) { |
| 47 | + // Already checked by the linter |
| 48 | + continue; |
| 49 | + } |
| 50 | + if (!isExternalLink(link.target)) { |
| 51 | + const target = link.target.split("#")[0]; |
| 52 | + let resolved; |
| 53 | + if (isAbsolute(target)) { |
| 54 | + resolved = normalize(`./${target}`); |
| 55 | + } |
| 56 | + else { |
| 57 | + resolved = normalize(path.join(dirname(parsedFile.filename), target)); |
| 58 | + } |
| 59 | + const stats = await stat(resolved).catch(() => undefined); |
| 60 | + if (stats?.isDirectory()) { |
| 61 | + const readmeExists = await stat(`${resolved}/README.md`).catch(() => undefined); |
| 62 | + if (readmeExists) { |
| 63 | + // console.log( |
| 64 | + // `info LINK-TO-DIR-WITH-README ${parsedFile.filename}:0 Link to a directory, which has a README: ${target}` |
| 65 | + // ); |
| 66 | + } |
| 67 | + else { |
| 68 | + console.log(`error LINK-TO-RAW-DIR ${parsedFile.filename}:0 Link to a directory, which has no README: ${target}`); |
| 69 | + ++errors; |
| 70 | + } |
| 71 | + } |
| 72 | + else if (stats === undefined) { |
| 73 | + console.log(`error BROKEN-INTERNAL-LINK ${parsedFile.filename}:0 Link target does not exist: ${target}`); |
| 74 | + ++errors; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + if (errors > 0) { |
| 80 | + console.error("Link validation found errors"); |
| 81 | + process.exit(1); |
| 82 | + } |
| 83 | +}; |
| 84 | +main().catch((error) => { |
| 85 | + console.error(error); |
| 86 | + process.exit(1); |
| 87 | +}); |
0 commit comments