-
Notifications
You must be signed in to change notification settings - Fork 33
73 lines (61 loc) · 2.07 KB
/
Copy pathimg-link-checker.yml
File metadata and controls
73 lines (61 loc) · 2.07 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
62
63
64
65
66
67
68
69
70
71
72
73
name: Check Image Links
on:
pull_request:
paths:
- 'topics/**/*.qmd'
- 'pathway/**/*.qmd'
- '*.qmd'
jobs:
check-image-links:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
- name: Install dependencies
run: npm install axios glob
- name: Check image links
run: |
node -e "
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const glob = require('glob');
const markdownFiles = glob.sync('**/*.qmd');
const imageLinks = [];
markdownFiles.forEach(file => {
const content = fs.readFileSync(file, 'utf8');
const regex = /!\[.*?\]\((.*?)\)/g;
let match;
while ((match = regex.exec(content)) !== null) {
const url = match[1];
if (url.startsWith('http://') || url.startsWith('https://')) {
imageLinks.push({ file, url });
}
}
});
const checkLinks = async () => {
const results = await Promise.all(imageLinks.map(async ({ file, url }) => {
try {
const response = await axios.head(url);
return response.status === 200 ? null : { file, url, status: response.status };
} catch (error) {
return { file, url, status: error.response ? error.response.status : 'Unknown error' };
}
}));
const brokenLinks = results.filter(result => result !== null);
if (brokenLinks.length > 0) {
console.log('Broken image links found:');
brokenLinks.forEach(({ file, url, status }) => {
console.log(`File: ${file}, URL: ${url}, Status: ${status}`);
});
process.exit(1);
} else {
console.log('No broken image links found.');
}
};
checkLinks();
"