Skip to content

Commit 41afa74

Browse files
Update Broken-links-checker-final.yml
1 parent fc7b175 commit 41afa74

File tree

1 file changed

+68
-109
lines changed

1 file changed

+68
-109
lines changed
Lines changed: 68 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,71 @@
1-
name: Broken Link Checker
2-
1+
name: Broken Link Checker (lychee with comment strip)
32
on:
4-
pull_request:
5-
paths:
6-
- '**/*.md'
7-
workflow_dispatch:
8-
3+
pull_request:
4+
paths:
5+
- '**/*.md'
6+
workflow_dispatch:
97
permissions:
10-
contents: read
11-
8+
contents: read
129
jobs:
13-
markdown-link-check:
14-
name: Check Markdown Broken Links
15-
runs-on: ubuntu-latest
16-
17-
steps:
18-
- name: Checkout Repo
19-
uses: actions/checkout@v4
20-
with:
21-
fetch-depth: 0
22-
23-
- name: Get Added/Modified Markdown Files
24-
id: changed
25-
if: github.event_name == 'pull_request'
26-
run: |
27-
git fetch origin ${{ github.base_ref }}
28-
files=$(git diff --name-only origin/${{ github.base_ref }}...HEAD | grep '\.md$' || true)
29-
echo "md_files<<EOF" >> $GITHUB_OUTPUT
30-
echo "$files" >> $GITHUB_OUTPUT
31-
echo "EOF" >> $GITHUB_OUTPUT
32-
33-
- name: Strip HTML comments from Markdown
34-
if: github.event_name == 'pull_request' && steps.changed.outputs.md_files != ''
35-
run: |
36-
for file in ${{ steps.changed.outputs.md_files }}; do
37-
awk -v FS="" '
38-
BEGIN { in_comment = 0 }
39-
{
40-
if (in_comment) {
41-
if (match($0, /-->/)) {
42-
$0 = substr($0, RSTART + RLENGTH)
43-
in_comment = 0
44-
} else { next }
45-
}
46-
while (match($0, /<!--/)) {
47-
prefix = substr($0, 1, RSTART - 1)
48-
rest = substr($0, RSTART + 4)
49-
if (match(rest, /-->/)) {
50-
rest = substr(rest, RSTART + RLENGTH)
51-
$0 = prefix rest
52-
} else {
53-
$0 = prefix
54-
in_comment = 1
55-
break
56-
}
57-
}
58-
print
59-
}
60-
' "$file" > tmp && mv tmp "$file"
61-
done
62-
63-
- name: Check Broken Links in Added/Modified Files
64-
if: github.event_name == 'pull_request' && steps.changed.outputs.md_files != ''
65-
uses: lycheeverse/[email protected]
66-
with:
67-
args: >
68-
--verbose --exclude-mail --no-progress --exclude ^https?://
69-
${{ steps.changed.outputs.md_files }}
70-
env:
71-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
72-
73-
- name: Strip HTML comments (manual run)
74-
if: github.event_name == 'workflow_dispatch'
75-
run: |
76-
for file in **/*.md; do
77-
awk -v FS="" '
78-
BEGIN { in_comment = 0 }
79-
{
80-
if (in_comment) {
81-
if (match($0, /-->/)) {
82-
$0 = substr($0, RSTART + RLENGTH)
83-
in_comment = 0
84-
} else { next }
85-
}
86-
while (match($0, /<!--/)) {
87-
prefix = substr($0, 1, RSTART - 1)
88-
rest = substr($0, RSTART + 4)
89-
if (match(rest, /-->/)) {
90-
rest = substr(rest, RSTART + RLENGTH)
91-
$0 = prefix rest
92-
} else {
93-
$0 = prefix
94-
in_comment = 1
95-
break
96-
}
97-
}
98-
print
99-
}
100-
' "$file" > tmp && mv tmp "$file"
101-
done
102-
103-
- name: Check Broken Links in Entire Repo
104-
if: github.event_name == 'workflow_dispatch'
105-
uses: lycheeverse/[email protected]
106-
with:
107-
args: >
108-
--verbose --exclude-mail --no-progress --exclude ^https?://
109-
'**/*.md'
110-
output: lychee/out.md
111-
env:
112-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
10+
markdown-link-check:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout Repo
14+
uses: actions/checkout@v4
15+
- name: Install Python
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: '3.x'
19+
- name: Strip HTML comments from changed Markdown files
20+
if: github.event_name == 'pull_request'
21+
id: strip
22+
run: |
23+
echo "cleaned_files=" >> $GITHUB_OUTPUT
24+
for file in ${{ github.event.pull_request.changed_files }}; do
25+
[ "${file##*.}" != "md" ] && continue
26+
python3 - <<'EOF'
27+
import re, sys
28+
path = sys.argv[1]
29+
with open(path, 'r') as f:
30+
content = f.read()
31+
clean = re.sub(r'<!--.*?-->', '', content, flags=re.DOTALL)
32+
out = path + ".cleaned"
33+
with open(out, 'w') as f:
34+
f.write(clean)
35+
print(out)
36+
EOF
37+
echo "cleaned_files<<EOF" >> $GITHUB_OUTPUT
38+
echo "${file}.cleaned" >> $GITHUB_OUTPUT
39+
echo "EOF" >> $GITHUB_OUTPUT
40+
done
41+
- name: Run lychee on cleaned files
42+
if: github.event_name == 'pull_request' && steps.strip.outputs.cleaned_files != ''
43+
uses: lycheeverse/[email protected]
44+
with:
45+
args: >
46+
--verbose --exclude-mail --no-progress --exclude ^https?://
47+
${{ steps.strip.outputs.cleaned_files }}
48+
env:
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
- name: Run lychee on all Markdown (manual)
51+
if: github.event_name == 'workflow_dispatch'
52+
run: |
53+
python3 - <<'EOF'
54+
import re, glob
55+
for path in glob.glob('**/*.md', recursive=True):
56+
with open(path) as f: data = f.read()
57+
clean = re.sub(r'<!--.*?-->', '', data, flags=re.DOTALL)
58+
with open(path + ".cleaned", 'w') as f: f.write(clean)
59+
EOF
60+
echo "::set-output name=all_cleaned::$(echo **/*.md.cleaned)"
61+
env:
62+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
63+
- name: Check all Markdown (manual)
64+
if: github.event_name == 'workflow_dispatch'
65+
uses: lycheeverse/[email protected]
66+
with:
67+
args: >
68+
--verbose --exclude-mail --no-progress --exclude ^https?://
69+
${{ steps.strip.outputs.all_cleaned }}
70+
env:
71+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)