Metalsmith plugin to check for internal broken links
Small typos can often result in unexpected broken links. This plugin aims to catch them as early as possible.
It checks for relative and root-relative links which do not have a corresponding file in the Metalsmith pipeline. It (currently) ignores all absolute links.
Any broken links will cause an
Error to be thrown (or a warning to be printed if
options.warn is set).
By default, all
href attributes of
<a> tags and all
src attributes of
<img> tags are checked.
I also wrote a blog post about what I learned developing this plugin.
In your Metalsmith source dir, you have the following file (
(Root-relative link) Error if 'a.html' not in files(Relative link) Error if 'dir1/a.html' not in files(Relative link) Error if 'dir1/a.html' not in files(Relative link) Error if 'a.html' not in files(Root-relative link to dir) Error if 'index.html' not in files(Relative link to dir) Error if 'dir1/dir2/index.html' not in files(Hash fragment link) Always valid(Hash fragment link) Error if 'dir2/index.html' not in filesMissing href attribute, always broken
Note that links to directories are allowed if they have a trailing slash (the
index.html file will be looked for). However links to directories without a trailing slash are not allowed unless the
allowRedirects option is set.
$ npm install --save metalsmith-broken-link-checker
var Metalsmith =var blc =// Build your full site here...
<a>tag with a
nameattribute but no
hrefattribute (used for jumping to elements on a page with hash fragments)
true, the following would be allowed:
<a name="anchor">Anchor text</a>
file.html#someidcould link to a valid file but the file content could be missing the
someidid or name on an element
Errorwhen encountering the first broken link
http://example.com/base/then links to
/base/dir/file.htmlwill be valid if
dir/file.htmlexists in the metalsmith pipeline