Broken Link Checker #33
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Broken Link Checker | |
| on: | |
| pull_request: | |
| paths: | |
| - '**/*.md' | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| jobs: | |
| markdown-link-check: | |
| name: Check Markdown Broken Links | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Repo | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Get Added/Modified Markdown Files (PR only) | |
| 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: Check Broken Links in Added/Modified Files (PR) | |
| if: github.event_name == 'pull_request' && steps.changed-files.outputs.md_files != '' | |
| uses: lycheeverse/[email protected] | |
| with: | |
| args: > | |
| --verbose --exclude-mail --no-progress --exclude ^https?:// | |
| ${{ steps.changed-files.outputs.md_files }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Check Broken Links in Entire Repo (Manual) | |
| if: github.event_name == 'workflow_dispatch' | |
| uses: lycheeverse/[email protected] | |
| with: | |
| args: > | |
| --verbose --exclude-mail --no-progress --exclude ^https?:// | |
| '**/*.md' | |
| output: lychee/out.md | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Clean lychee/out.md by removing errors inside HTML comments | |
| if: github.event_name == 'workflow_dispatch' | |
| run: | | |
| FILTERED="lychee/out.md.cleaned" | |
| > "$FILTERED" | |
| awk ' | |
| BEGIN { RS="### "; FS="\n" } | |
| NR == 1 { next } | |
| { | |
| header = "### " $1 | |
| file_section = $1 | |
| file_path = "" | |
| if (match(file_section, /in (.*\.md)/, m)) { | |
| file_path = m[1] | |
| } | |
| keep_section = "" | |
| for (i = 2; i <= NF; i++) { | |
| line = $i | |
| if (match(line, /\((file:\/\/[^\)]+)\)/, mfile)) { | |
| fullpath = mfile[1] | |
| localpath = substr(fullpath, 8) | |
| filename = gensub(/^.*\//, "", "g", localpath) | |
| cmd = "awk \047BEGIN{incomment=0} /<!--/{incomment=1} /-->/&&incomment{incomment=0; next} incomment && index(\\\$0, \\\"" filename "\\\")>0 {exit 0} END{exit 1}\047 \"" file_path "\"" | |
| status = system(cmd) | |
| if (status != 0) { | |
| keep_section = keep_section line "\n" | |
| } | |
| } else { | |
| keep_section = keep_section line "\n" | |
| } | |
| } | |
| if (length(keep_section) > 0) { | |
| print header "\n" keep_section >> "'"$FILTERED"'" | |
| } | |
| } | |
| ' lychee/out.md | |
| mv "$FILTERED" lychee/out.md | |
| - name: Show Cleaned Report | |
| if: github.event_name == 'workflow_dispatch' | |
| run: | | |
| echo '```markdown' | |
| cat lychee/out.md | |
| echo '```' |