|
1 | 1 | const fs = require("fs"); |
| 2 | +const path = require("path"); |
2 | 3 |
|
| 4 | +// Paths |
3 | 5 | const logFile = "brokenLinks.log"; |
| 6 | +const unsupportedVersionsFile = path.join(process.cwd(), "src", "pages", "unsupported-versions.mdx"); |
4 | 7 |
|
5 | | -// Read the full log file |
| 8 | +// Read unsupported versions from the MDX file. |
| 9 | +let unsupportedVersions = []; |
| 10 | +try { |
| 11 | + const mdxContent = fs.readFileSync(unsupportedVersionsFile, "utf8"); |
| 12 | + // Extract unsupported version numbers (like 3.7, 3.6, etc.). |
| 13 | + unsupportedVersions = Array.from(mdxContent.matchAll(/ScalarDB (\d+\.\d+)/g), match => match[1]); |
| 14 | +} catch (err) { |
| 15 | + console.error("Error reading unsupported versions file:", err); |
| 16 | + process.exit(1); |
| 17 | +} |
| 18 | + |
| 19 | +if (unsupportedVersions.length === 0) { |
| 20 | + console.log("No unsupported versions found. Exiting."); |
| 21 | + process.exit(0); |
| 22 | +} |
| 23 | + |
| 24 | +// Define the prefixes to filter for broken links. |
| 25 | +const prefixes = [ |
| 26 | + "- Broken link on source page path", |
| 27 | + " -> linking to " |
| 28 | +]; |
| 29 | + |
| 30 | +// Read the log file and filter lines based on unsupported versions. |
6 | 31 | fs.readFile(logFile, "utf8", (err, data) => { |
7 | 32 | if (err) { |
8 | 33 | console.error("Error reading log file:", err); |
9 | 34 | process.exit(1); |
10 | 35 | } |
11 | 36 |
|
12 | | - // Define the prefixes to filter |
13 | | - const prefixes = [ |
14 | | - "- Broken link on source page path", |
15 | | - " -> linking to " |
16 | | - ]; |
17 | | - |
18 | | - // Filter lines that start with any of the specified prefixes |
19 | 37 | const filteredLines = data |
20 | 38 | .split("\n") |
21 | | - .filter((line) => prefixes.some((prefix) => line.startsWith(prefix))); |
| 39 | + .filter((line) => |
| 40 | + prefixes.some((prefix) => line.startsWith(prefix)) && |
| 41 | + !unsupportedVersions.some((version) => line.includes(`/docs/${version}/`)) // Exclude unsupported versions. |
| 42 | + ); |
22 | 43 |
|
23 | | - // Overwrite the log file with only the filtered lines |
| 44 | + // Overwrite the log file with filtered lines. |
24 | 45 | fs.writeFile(logFile, filteredLines.join("\n"), "utf8", (err) => { |
25 | 46 | if (err) { |
26 | 47 | console.error("Error writing filtered log file:", err); |
|
0 commit comments