-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcheck-links.js
More file actions
50 lines (42 loc) · 1.52 KB
/
Copy pathcheck-links.js
File metadata and controls
50 lines (42 loc) · 1.52 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
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const glob = require('glob');
const rootDirectory = './Resource/'; // Replace with the root directory of your project
const duplicateLinks = new Set();
async function findLinksInFile(filePath) {
try {
const fileContents = fs.readFileSync(filePath, 'utf-8');
// const links = fileContents.match(/https?:\/\/[^\s]+/g) || [];
const links = fileContents.match(/\bhttps?:\/\/[^\s"']+\b/g) || [];
for (const link of links) {
if (duplicateLinks.has(link)) {
console.error(`Duplicate link found: ${link}`);
process.exit(1);
}
duplicateLinks.add(link);
try {
const response = await axios.head(link);
if (response.status >= 400) {
console.error(`Invalid link: ${link}`);
continue; // Continue checking other links
}
console.log(`Valid link: ${link}`);
} catch (error) {
console.error(`Error checking link: ${link}, error: ${error.message}`);
continue; // Continue checking other links
}
}
} catch (error) {
console.error(`Error reading file ${filePath}: ${error}`);
}
}
function findAndProcessMarkdownFiles(rootDir) {
const markdownFiles = glob.sync('**/*.md', { cwd: rootDir, absolute: true });
for (const file of markdownFiles) {
findLinksInFile(file);
}
}
// Start the search in the root directory
findAndProcessMarkdownFiles(rootDirectory);
console.log('No duplicate links found. Merge action can proceed.');