Skip to content

Commit 7acfb38

Browse files
authored
Add function to generate log file that contains broken links (#1046)
* Create filter-broken-link-warnings.js * Make all broken links show warning A list of all broken links for versions of docs still under Maintenance Support are generated in **brokenLinks.log**. To eliminate potentially redundant entries in the log, only `onBrokenLinks` is set to `warn`. * Add `brokenLinks.log` **brokenLinks.log** is for internal/local purposes, so we don't need to include it in the build files. * Add broken-link log generator to `build` command * Show broken links for versions under Maintenance Support To reduce work in fixing broken links for versions that we no longer support, the script checks for broken link for versions of docs that are currently under Maintenance Support.
1 parent 305ed6e commit 7acfb38

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# Generated files
88
.docusaurus
99
.cache-loader
10+
brokenLinks.log
1011

1112
# Gradle build files
1213
.gradle

docusaurus.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const config = {
2424
projectName: 'docs-scalardb', // Usually your repo name.
2525

2626
onBrokenLinks: 'warn',
27-
onBrokenMarkdownLinks: 'warn',
27+
onBrokenMarkdownLinks: 'ignore',
28+
onBrokenAnchors: 'ignore',
2829

2930
// Even if you don't use internationalization, you can use this field to set
3031
// useful metadata like html lang. For example, if your site is Chinese, you

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"docusaurus": "docusaurus",
77
"start": "docusaurus start",
8-
"build": "docusaurus build && node scripts/generate-glossary-json.js",
8+
"build": "docusaurus build 2>&1 | tee brokenLinks.log && node scripts/filter-broken-link-warnings.js && node scripts/generate-glossary-json.js",
99
"swizzle": "docusaurus swizzle",
1010
"deploy": "docusaurus deploy",
1111
"clear": "docusaurus clear",
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
// Paths
5+
const logFile = "brokenLinks.log";
6+
const unsupportedVersionsFile = path.join(process.cwd(), "src", "pages", "unsupported-versions.mdx");
7+
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.
31+
fs.readFile(logFile, "utf8", (err, data) => {
32+
if (err) {
33+
console.error("Error reading log file:", err);
34+
process.exit(1);
35+
}
36+
37+
const filteredLines = data
38+
.split("\n")
39+
.filter((line) =>
40+
prefixes.some((prefix) => line.startsWith(prefix)) &&
41+
!unsupportedVersions.some((version) => line.includes(`/docs/${version}/`)) // Exclude unsupported versions.
42+
);
43+
44+
// Overwrite the log file with filtered lines.
45+
fs.writeFile(logFile, filteredLines.join("\n"), "utf8", (err) => {
46+
if (err) {
47+
console.error("Error writing filtered log file:", err);
48+
process.exit(1);
49+
}
50+
console.log(`Filtered broken link warnings saved to ${logFile}`);
51+
});
52+
});

0 commit comments

Comments
 (0)