-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathenforce-trailing-slashes.js
More file actions
61 lines (49 loc) · 1.62 KB
/
enforce-trailing-slashes.js
File metadata and controls
61 lines (49 loc) · 1.62 KB
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
/**
* Recursively scans the /examples directory
* and ADDS trailing slashes to scichart.com/demo
* URLs INSIDE MARKDOWN LINKS SAFELY.
*
* Example:
* [Demo](https://www.scichart.com/demo/react)
* → [Demo](https://www.scichart.com/demo/react/)
*/
const fs = require("fs");
const path = require("path");
const MD_URL_REGEX = /\]\((https?:\/\/(?:www\.)?scichart\.com\/demo(?:\/[^\s)"]*)?)\)/g;
function processFile(filePath) {
let content = fs.readFileSync(filePath, "utf8");
const newContent = content.replace(MD_URL_REGEX, (match, url) => {
// If it already ends with slash, leave it alone
if (url.endsWith("/")) return match;
return `](${url}/)`;
});
if (newContent !== content) {
fs.writeFileSync(filePath, newContent, "utf8");
console.log("- Updated:", filePath);
}
}
function walk(dir) {
const files = fs.readdirSync(dir);
const DONT_TOUCH = [
"node_modules",
".git",
"build",
"remove-trailing-slashes.js",
"add-trailing-slashes.js",
"server",
];
for (const file of files) {
if (DONT_TOUCH.includes(file)) continue;
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
walk(fullPath);
} else if (stat.isFile()) {
processFile(fullPath);
}
}
}
console.log("- Scanning `/examples` for Markdown demo links without trailing slashes...");
const targetDir = path.join(__dirname, "src", "components", "Examples");
walk(targetDir);
console.log("Done! All Markdown demo links are now slash-safe.");