DEV Community

loading...
Cover image for Check markdown links with github action

Check markdown links with github action

thibaultduponchelle profile image Tib ・2 min read

In a previous blog post, I discussed checking links with Perl and LWP::Simple then LWP::UserAgent.

My original idea was to keep perlres (a directory of Perl resources) up to date.

My previous post discussed the links checker but ignored the markdown parsing. This time, I propose to use a github action that will do the whole thing, elegantly.

During my research, I found 2 github actions to check links in markdown files, both based on markdown parser written in javascript.

lychee-action based on lychee and github-action-markdown-links-check based on markdown-links-check.

I tried the second one, that has more "stars" 😀

The code

Simple, I just copy pasted the code provided by the maintainer !

name: Check Markdown links

on: push

jobs:
  markdown-link-check:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: gaurav-nelson/github-action-markdown-link-check@v1
Enter fullscreen mode Exit fullscreen mode

In a file .github/workflow/checklinks.yml and on a branch checklinks (to test my stuff before integrating in the main branch).

I push the code, it starts testing, freezes a moment without logs (about 700 links to check) and fails admirably.

Yes, this github action fails on dead links (lychee-action seems to behaves differently on this point)

Do not fail on error

First, I have some "flaky" links, so I would prefer to not fail but only report.

The solution is continue-on-error: true

So this first customization is fine but my action is still not perfect.

Do not run on every push

I often edit 3 typos in a row of 3 commits, it's too brutal to run at each code push, I would prefer to schedule it once a week:

on:
  schedule:
  - cron: "0 13 * * 1"
Enter fullscreen mode Exit fullscreen mode

See this other post from where I extracted the cron line.

429 Too Many Requests

This is the typical github answer after too much requests, so I would like to ignore this status when it happens.

To achieve this, I added a mlc_config.json to my root git directory and added this config inside:

{
  "aliveStatusCodes": [429, 200]
}
Enter fullscreen mode Exit fullscreen mode

And finally look at our nice report:
Links

And the errors:
Errors

Conclusion

You can see my addition in this Pull Request

The final action code is:

name: Check Markdown links

on:
  schedule:
  - cron: "0 13 * * 1"

jobs:
  markdown-link-check:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: gaurav-nelson/github-action-markdown-link-check@v1
      continue-on-error: true
Enter fullscreen mode Exit fullscreen mode

With mlc_config.json:

{
  "aliveStatusCodes": [429, 200]
}
Enter fullscreen mode Exit fullscreen mode

Discussion (3)

pic
Editor guide
Collapse
jj profile image
Juan Julián Merelo Guervós

I'm so going to use this. Many thanks!

Collapse
davidmaceachern profile image
David MacEachern

Thanks for sharing this awesome post, that's one less thing I need to figure out!

Collapse
thibaultduponchelle profile image
Tib Author

Thank you a lot for the kind words :)