|
| 1 | +import { exec } from "node:child_process"; |
| 2 | +import { readFile } 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 findAllFilesInGit = 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(Boolean)); |
| 15 | + }); |
| 16 | + }); |
| 17 | +}; |
| 18 | +const findMarkdownFiles = (files) => { |
| 19 | + const ignorePattern = /^(README|LICENSE|contributing\/)/; |
| 20 | + return files.filter((f) => f.toLocaleLowerCase().endsWith(".md") && !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 gitFiles = await findAllFilesInGit(); |
| 32 | + // For now, we assume that there are no case clashes |
| 33 | + const lowercaseGitFiles = gitFiles.map((s) => s.toLocaleLowerCase()); |
| 34 | + const markdownFilenames = findMarkdownFiles(gitFiles); |
| 35 | + const parsedFiles = await scanForLinks(markdownFilenames); |
| 36 | + let errors = 0; |
| 37 | + for (const parsedFile of parsedFiles) { |
| 38 | + for (const img of parsedFile.images) { |
| 39 | + if (!isExternalLink(img.src)) { |
| 40 | + const resolved = path.join(dirname(parsedFile.filename), img.src); |
| 41 | + const exists = lowercaseGitFiles.includes(resolved.toLocaleLowerCase()); |
| 42 | + if (!exists) { |
| 43 | + console.log(`error BROKEN-INTERNAL-IMAGE ${parsedFile.filename}:0 Broken internal image reference ${img.src}`); |
| 44 | + ++errors; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + for (const link of parsedFile.links) { |
| 49 | + if (link.target.startsWith("#")) { |
| 50 | + // Already checked by the linter |
| 51 | + continue; |
| 52 | + } |
| 53 | + if (!isExternalLink(link.target)) { |
| 54 | + const target = link.target.split("#")[0]; |
| 55 | + let resolved; |
| 56 | + if (isAbsolute(target)) { |
| 57 | + resolved = normalize(`./${target}`); |
| 58 | + } |
| 59 | + else { |
| 60 | + resolved = normalize(path.join(dirname(parsedFile.filename), target)); |
| 61 | + } |
| 62 | + const isFile = lowercaseGitFiles.includes(resolved.toLocaleLowerCase()); |
| 63 | + const resolvedWithTrailingSlash = resolved.endsWith("/") |
| 64 | + ? resolved.toLocaleLowerCase() |
| 65 | + : `${resolved.toLocaleLowerCase()}/`; |
| 66 | + const isDirectory = lowercaseGitFiles.some((s) => s.startsWith(resolvedWithTrailingSlash)); |
| 67 | + if (!isFile && !isDirectory) { |
| 68 | + console.log(`error BROKEN-INTERNAL-LINK ${parsedFile.filename}:0 Link target does not exist: ${target}`); |
| 69 | + ++errors; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + if (errors > 0) |
| 75 | + process.exit(1); |
| 76 | +}; |
| 77 | +main().catch((error) => { |
| 78 | + console.error(error); |
| 79 | + process.exit(1); |
| 80 | +}); |
0 commit comments