File size: 4,193 Bytes
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env node

const fs = require("node:fs");
const path = require("node:path");

const defaultDocsDir = path.join(__dirname, "..");
// The .yml issue forms are listed so a renamed or deleted template is caught;
// they only link out today, so the link check finds nothing in them.
const documentationFiles = [
  "README.md",
  ".github/CONTRIBUTING.md",
  ".github/CODE_OF_CONDUCT.md",
  ".github/SECURITY.md",
  ".github/PULL_REQUEST_TEMPLATE.md",
  ".github/ISSUE_TEMPLATE/bug_report.yml",
  ".github/ISSUE_TEMPLATE/feature_request.yml",
  ".github/ISSUE_TEMPLATE/security_vulnerability.yml",
];

const markdownLinkRegex = /\[([^\]]+)\]\(([^)]+)\)/g;
const htmlLinkRegex = /<a[^>]+href="([^"]+)"[^>]*>/g;

function validateFile(filePath, docsDir = defaultDocsDir) {
  try {
    const content = fs.readFileSync(filePath, "utf8");
    const relativePath = path.relative(docsDir, filePath);
    const issues = [];

    let match;
    while (true) {
      match = markdownLinkRegex.exec(content);
      if (match === null) break;
      const linkText = match[1];
      const linkTarget = match[2];

      if (
        linkTarget.startsWith("http") ||
        linkTarget.startsWith("mailto:") ||
        linkTarget.startsWith("#") ||
        linkTarget.startsWith("www.")
      ) {
        continue;
      }

      const targetPath = path.resolve(path.dirname(filePath), linkTarget);

      if (!fs.existsSync(targetPath)) {
        issues.push({
          type: "missing-file",
          linkText,
          linkTarget,
          targetPath: path.relative(docsDir, targetPath),
        });
      }
    }

    while (true) {
      match = htmlLinkRegex.exec(content);
      if (match === null) break;
      const linkTarget = match[1];

      if (
        linkTarget.startsWith("http") ||
        linkTarget.startsWith("mailto:") ||
        linkTarget.startsWith("#")
      ) {
        continue;
      }

      const targetPath = path.resolve(path.dirname(filePath), linkTarget);

      if (!fs.existsSync(targetPath)) {
        issues.push({
          type: "missing-file",
          linkText: "HTML link",
          linkTarget,
          targetPath: path.relative(docsDir, targetPath),
        });
      }
    }

    return { file: relativePath, issues };
  } catch (error) {
    return {
      file: path.relative(docsDir, filePath),
      error: error.message,
    };
  }
}

function main(docsDir = defaultDocsDir) {
  console.log("πŸ” Validating documentation links...\n");

  let totalIssues = 0;
  let totalFiles = 0;

  for (const docFile of documentationFiles) {
    const filePath = path.join(docsDir, docFile);

    if (!fs.existsSync(filePath)) {
      console.log(`❌ File not found: ${docFile}`);
      totalIssues++;
      continue;
    }

    totalFiles++;
    const result = validateFile(filePath, docsDir);

    if (result.error) {
      console.log(`❌ Error reading ${result.file}: ${result.error}`);
      totalIssues++;
      continue;
    }

    if (result.issues.length > 0) {
      console.log(`❌ ${result.file} has ${result.issues.length} issue(s):`);
      result.issues.forEach((issue) => {
        console.log(
          `   β€’ Missing target: "${issue.linkTarget}" (${issue.linkText})`,
        );
        console.log(`     Expected: ${issue.targetPath}`);
      });
      totalIssues += result.issues.length;
    } else {
      console.log(`βœ… ${result.file} - All links valid`);
    }
  }

  console.log(`\nπŸ“Š Summary:`);
  console.log(`   Files checked: ${totalFiles}/${documentationFiles.length}`);
  console.log(`   Issues found: ${totalIssues}`);

  if (totalIssues > 0) {
    console.log(
      `\n❌ Documentation validation failed with ${totalIssues} issue(s)`,
    );
    process.exit(1);
  } else {
    console.log(`\nβœ… All documentation links are valid!`);
    process.exit(0);
  }
}

if (require.main === module) {
  const rootDirArg = process.argv[2];

  if (
    rootDirArg &&
    !fs.statSync(rootDirArg, { throwIfNoEntry: false })?.isDirectory()
  ) {
    console.error(`❌ Directory not found: ${rootDirArg}`);
    process.exit(1);
  }

  main(rootDirArg || undefined);
}

module.exports = { validateFile, documentationFiles };