Skip to content

Broken Link Checker #23

Broken Link Checker

Broken Link Checker #23

name: Broken Link Checker
on:
pull_request:
paths:
- '**/*.md'
workflow_dispatch:
jobs:
markdown-link-check:
name: Check Markdown Broken Links
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
# === PR MODE ===
- name: Get Changed Markdown Files (PR)
id: changed-files
if: github.event_name == 'pull_request'
run: |
git fetch origin ${{ github.base_ref }}
files=$(git diff --name-only origin/${{ github.base_ref }}...HEAD | grep '\.md$' || true)
echo "md_files<<EOF" >> $GITHUB_OUTPUT
echo "$files" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Initial Lychee Check on Changed Files (PR)
if: github.event_name == 'pull_request' && steps.changed-files.outputs.md_files != ''
uses: lycheeverse/[email protected]
with:
output: lychee/pr.out
args: >
--verbose --no-progress --exclude-mail --exclude '^https?://'
${{ steps.changed-files.outputs.md_files }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Affected Files (PR)
if: github.event_name == 'pull_request'
id: affected-pr
run: |
mkdir -p sanitized_md
affected=$(grep -Eo 'file://[^ ]+\.md' lychee/pr.out | sed 's|file://||' | sort -u)
echo "$affected"
echo "affected_files<<EOF" >> $GITHUB_OUTPUT
echo "$affected" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Strip HTML Comments from Affected Files (PR)
if: steps.affected-pr.outputs.affected_files != ''
run: |
echo "${{ steps.affected-pr.outputs.affected_files }}" | while read -r file; do
if [ -f "$file" ]; then
target="sanitized_md/$file"
mkdir -p "$(dirname "$target")"
perl -0777 -pe 's/<!--.*?-->//gs' "$file" > "$target"
fi
done
- name: Re-check Sanitized Affected Files (PR)
if: steps.affected-pr.outputs.affected_files != ''
uses: lycheeverse/[email protected]
with:
args: >
--verbose --no-progress --exclude-mail --exclude '^https?://'
sanitized_md/**/*.md
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# === MANUAL MODE ===
- name: Initial Lychee Check on All Markdown (Manual)
if: github.event_name == 'workflow_dispatch'
uses: lycheeverse/[email protected]
with:
output: lychee/manual.out
args: >
--verbose --no-progress --exclude-mail --exclude '^https?://'
'**/*.md'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Affected Files (Manual)
if: github.event_name == 'workflow_dispatch'
id: affected-manual
run: |
mkdir -p sanitized_md
affected=$(grep -Eo 'file://[^ ]+\.md' lychee/manual.out | sed 's|file://||' | sort -u)
echo "$affected"
echo "affected_files<<EOF" >> $GITHUB_OUTPUT
echo "$affected" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Strip HTML Comments from Affected Files (Manual)
if: steps.affected-manual.outputs.affected_files != ''
run: |
echo "${{ steps.affected-manual.outputs.affected_files }}" | while read -r file; do
if [ -f "$file" ]; then
target="sanitized_md/$file"
mkdir -p "$(dirname "$target")"
perl -0777 -pe 's/<!--.*?-->//gs' "$file" > "$target"
fi
done
- name: Re-check Sanitized Affected Files (Manual)
if: steps.affected-manual.outputs.affected_files != ''
uses: lycheeverse/[email protected]
with:
args: >
--verbose --no-progress --exclude-mail --exclude '^https?://'
sanitized_md/**/*.md
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}