Recently, fastai has been hard at work improving and overhauling nbdev, a literate programming environment for python. A key feature of nbdev is automated generation of documentation from Jupyter notebooks. This documentation system adds many niceties, such as the following types of hyperlinks automatically:
Links to source code on GitHub.
Links to both internal and external documentation by introspecting variable names in backticks.
Because documentation is so easy to create and maintain in nbdev, we find ourselves and others creating much more of it! In addition to automatic hyperlinks, we often include our own links to relevant websites, blogs and videos when documenting code. For example, one of the largest nbdev generated sites, docs.fast.ai, has more than 300 external and internal links at the time of this writing.
The Solution
Due to the continued popularity of fastai and the growth of new nbdev projects, grooming these links manually became quite tedious. We investigated solutions that could verify links for us automatically, but were not satisfied with any existing solutions. These are the features we desired:
A platform independent solution that is not tied to a specific static site generator like Jekyll or Hugo.
Intelligent introspection of external links that are actually internal links. For example, if we are building the site docs.fast.ai, a link to https://docs.fast.ai/tutorial should not result in a web request, but rather introspection of the local file system for the presence of tutorial.html in the right location.
Verification of any links to assets like CSS, data, javascript or other files.
Logs that are well organized that allow us to see each broken link or reference to a non-existent path, and the pages these are found in.
Parallelism to verify links as fast as possible.
Lightweight, easy to install with minimal dependencies.
We tried tools such as linkchecker and pylinkvalidator, but these required your site to be first be hosted. Since we wanted to check links on a static site, hosting is overhead we wanted to avoid.
This is what led us to create fastlinkcheck, which we discuss below.
Note
For Ruby users, htmlproofer apperas to provide overlapping functionality. We have not tried this library.
A tour of fastlinkcheck
For this tour we will be referring to the files in the fastlinkcheck repo. You should clone this repo in the current directory in order to follow along:
After installing fastlinkcheck, the cli command link_check is available from the command line. We can see various options with the --help flag.
link_check --help
usage: link_check [-h] [--host HOST] [--config_file CONFIG_FILE] [--pdb]
[--xtra XTRA]
path
Check for broken links recursively in `path`.
positional arguments:
path Root directory searched recursively for HTML files
optional arguments:
-h, --help show this help message and exit
--host HOST Host and path (without protocol) of web server
--config_file CONFIG_FILE
Location of file with urls to ignore
--pdb Run in pdb debugger (default: False)
--xtra XTRA Parse for additional args (default: '')
From the root of fastlinkcheck repo, We can search the directory _example/broken_links recursively for broken links like this:
link_check _example/broken_links
ERROR: The Following Broken Links or Paths were found:
- 'http://fastlinkcheck.com/test.html' was found in the following pages:
- `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`
- 'http://somecdn.com/doesntexist.html' was found in the following pages:
- `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`
- Path('/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.js') was found in the following pages:
- `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`
: 1
Specifying the --host parameter allows you detect links that are internal by identifying links with that host name. External links are verified by making a request to the appropriate website. On the other hand, internal links are verified by inspecting the presence and content of local files.
We must be careful when using the --host argument to only pass the host (and path, if necessary) without the protocol. For example, this is how we specify the hostname if your site’s url is http://fastlinkcheck.com/test.html:
ERROR: The Following Broken Links or Paths were found:
- 'http://somecdn.com/doesntexist.html' was found in the following pages:
- `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`
- Path('/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.js') was found in the following pages:
- `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`
: 1
We now have one less broken link as there is indeed a file named test.html in the root of the path we are searching. However, if we add a path to the end of --host , such as fastlinkcheck.com/mysite the link would again be listed as broken because _example/broken_links/mysite/test.html does not exist:
ERROR: The Following Broken Links or Paths were found:
- 'http://fastlinkcheck.com/test.html' was found in the following pages:
- `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`
- 'http://somecdn.com/doesntexist.html' was found in the following pages:
- `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`
- Path('/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.js') was found in the following pages:
- `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`
: 1
You can ignore links by creating a text file that contains a list of urls and paths to ignore. For example, the file _example/broken_links/linkcheck.rc contains:
cat _example/broken_links/linkcheck.rc
test.js
https://www.google.com
We can use this file to ignore urls and paths with the --config_file argument. This will filter out references to the broken link /test.js from our earlier results:
ERROR: The Following Broken Links or Paths were found:
- 'http://somecdn.com/doesntexist.html' was found in the following pages:
- `/Users/hamelsmu/github/fastlinkcheck/_example/broken_links/test.html`
: 1
Finally, if there are no broken links, link_check will not return anything. The directory _example/no_broken_links/ does not contain any HTML files with broken links:
You can also use these utilities from python instead of the terminal. Please see these docs for more information.
Using link_check in GitHub Actions
The link_check CLI utility that is installed with fastlinkcheck can be very useful in continuous integration systems like GitHub Actions. Here is an example GitHub Actions workflow that uses link_check: