catch broken things @mdsvex/plugin-link-check
Link check
Link checking, warns about #anchors and links to other documents that go nowhere
Install
pnpm add -D @mdsvex/plugin-link-check Turn it on
import { link_check } from '@mdsvex/plugin-link-check';
mdsvex({ heading_ids: true, plugins: [link_check()] }); It waits, as it reads the documents a page links to: the Vite plugin and compile_async compile such a document, compile throws a PluginAsyncError.
What it reads
[text](#id)link [text](./page.svx#id)acts on links · reads the whole document
An example
# Install See [the setup](#setup), [install](#install) and [the guide](./guide.svx#usage).
and the file it reads, ./guide.svx
# Guide
## Use it
The page is the same without the plugin. What it gives is its warnings:
- line 3
missing_anchor#setup links to no element of this document, none has the id "setup" - line 4
missing_anchor./guide.svx#usage links to /docs/guide.svx, which has no element with the id "usage"
Warns at every link that goes nowhere, with the line of the link, as the compile warns about anything else:
# Install
See [the setup](#setup), [the guide](../guide/+page.md#usage) and [the api](/api#options).#setup links to no element of this document, none has the id "setup"
../guide/+page.md#usage links to src/routes/guide/+page.md, which has no element with the id "usage"link_check({
documents: boolean = true, // check links to other documents too
heading_ids: boolean | { slug } = true, // how other documents' headings get ids
routes: string, // the directory whose documents are routes, as links.routes
extensions: string[] = ['.md', '.svx'],
base: string, // a root url written with the base has it taken off
ignore: (href, filename) => boolean,
strict: boolean = false // fail the compile with a LinkCheckError
});- Within a page,
#idis checked against every id the document gives an element: the ids of headings, which needheading_ids, anidtyped on an element, thenameof an<a>, theidarg of a directive, thefn-ids of footnotes and an id a plugin gave a node.#topand#need none. An id a component makes as it renders is not known, skip it withignore. - Other documents are checked by file,
./guide.md#usage, relative to the document, and by route,/guide#usage, whenroutesnames the directory of your routes, aslinks.routesdoes. A route is the documentguide.md,guide/+page.mdorguide/index.md, with each extension. A route with no such document is not reported, since it may be a Svelte page, and neither is a route inside a(group). A root url with no fragment is not checked. - What is read. Each linked document is read and compiled once for every document that links to it, with the
heading_idsgiven to the plugin, so give it the value you give mdsvex when that is nottrue. A link to a document that does not exist is a warning too, and the file it names is watched, so creating it checks the link again. - In the dev server a document that links to another is a dependency of it. Change the headings of
guide.mdand every document that links to it compiles again and warns about what now goes nowhere. strictfails a document that has a link that goes nowhere with aLinkCheckError, after the warnings, itslinkslisting each one. Turn it on for a build:
export default defineConfig(({ command }) => ({
plugins: [mdsvex({ plugins: [link_check({ strict: command === 'build' })] })]
}));Other documents are read while the document compiles, so the Vite plugin and compile_async, with a read option, check them, and compile throws a PluginAsyncError for a document that links to another. documents: false checks links within a page alone, under compile too. Markdown links are checked, not an <a> typed as html, and not files that are no documents, such as images.