unified

Project: remarkjs/remark-validate-links

Package: remark-validate-links@12.1.0

  1. Dependents: 0
  2. remark plugin to validate links to headings and files
  1. remark 214
  2. unified 181
  3. markdown 154
  4. plugin 140
  5. mdast 88
  6. remark-plugin 82
  7. file 34
  8. heading 26
  9. link 16
  10. reference 14
  11. validate 5

remark-validate-links

Build Coverage Downloads Sponsors Backers Chat

remark plugin to check that markdown links and images point to existing local files and headings in a Git repo.

For example, this document does not have a heading named Hello. So if we’d link to it ([welcome](#hello)), we’d get a warning. Links to headings in other markdown documents (examples/foo.md#hello) and links to files (license or index.js) are also checked.

This is specifically for Git repos. Like this one. Not for say a website.

Contents

What is this?

This package is a unified (remark) plugin to check local links in a Git repo.

When should I use this?

This project is useful if you have a Git repo, such as this one, with docs in markdown and links to headings and other files, and want to check whether they’re correct. Compared to other links checkers, this project can work offline (making it fast en prone to fewer false positives), and is specifically made for local links in Git repos. This plugin does not check external URLs (see remark-lint-no-dead-urls) or undefined references (see remark-lint-no-undefined-references).

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install remark-validate-links

In Deno with esm.sh:

import remarkValidateLinks from 'https://esm.sh/remark-validate-links@13'

In browsers with esm.sh:

<script type="module">
  import remarkValidateLinks from 'https://esm.sh/remark-validate-links@13?bundle'
</script>

Use

Say we have the following file example.md in this project:

# Alpha

Links are checked:

This [exists](#alpha).
This [one does not](#apha).

# Bravo

Headings in `readme.md` are [checked](readme.md#no-such-heading).
And [missing files are reported](missing-example.js).

Definitions are also checked:

[alpha]: #alpha
[charlie]: #charlie

References w/o definitions are not checked: [delta]

…and a module example.js:

import {remark} from 'remark'
import remarkValidateLinks from 'remark-validate-links'
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'

const file = await remark()
  .use(remarkValidateLinks)
  .process(await read('example.md'))

console.log(reporter(file))

…then running node example.js yields:

example.md
6:6-6:27   warning Cannot find heading for `#apha`; did you mean `alpha` missing-heading remark-validate-links:missing-heading
11:5-11:53 warning Cannot find file `missing-example.js`                 missing-file    remark-validate-links:missing-file
16:1-16:20 warning Cannot find heading for `#charlie`                    missing-heading remark-validate-links:missing-heading

⚠ 3 warnings

👉 Note: readme.md#no-such-heading is not warned about on the API, as it does not check headings in other markdown files. The remark CLI is able to do that.

API

This package exports no identifiers. The default export is remarkValidateLinks.

Check that markdown links and images point to existing local files and headings in a Git repo.

⚠️ Important: The API in Node.js checks links to headings and files but does not check whether headings in other files exist. The API in browsers only checks links to headings in the same file. The CLI can check everything.

Parameters
Returns

Transform (Transformer).

Options

Configuration (TypeScript type).

Fields

UrlConfig

Hosted Git info (TypeScript type).

Fields
Notes

For this repository (remarkjs/remark-validate-links on GitHub) urlConfig looks as follows:

{
  // Domain of URLs:
  hostname: 'github.com',
  // Path prefix before files:
  prefix: '/remarkjs/remark-validate-links/blob/',
  // Prefix of headings:
  headingPrefix: '#',
  // Hash to top of markdown documents:
  topAnchor: '#readme',
  // Whether lines in files can be linked:
  lines: true
}

If this project were hosted on Bitbucket, it would be:

{
  hostname: 'bitbucket.org',
  prefix: '/remarkjs/remark-validate-links/src/',
  headingPrefix: '#markdown-header-',
  lines: false
}

Examples

Example: CLI

It’s recommended to use remark-validate-links on the CLI with remark-cli. Install both with npm:

npm install remark-cli remark-validate-links --save-dev

Let’s say we have a readme.md (this current document) and an example.md with the following text:

# Hello

Read more [whoops, this does not exist](#world).

This doesn’t exist either [whoops!](readme.md#foo).

But this does exist: [license](license).

So does this: [readme](readme.md#install).

Now, running ./node_modules/.bin/remark --use remark-validate-links . yields:

example.md
  3:11-3:48  warning  Link to unknown heading: `world`               missing-heading          remark-validate-links
  5:27-5:51  warning  Link to unknown heading in `readme.md`: `foo`  missing-heading-in-file  remark-validate-links

readme.md: no issues found

⚠ 2 warnings

Example: CLI in npm scripts

You can use remark-validate-links and remark-cli in an npm script to check and format markdown in your project. Install both with npm:

npm install remark-cli remark-validate-links --save-dev

Then, add a format script and configuration to package.json:

{
  // …
  "scripts": {
    // …
    "format": "remark . --quiet --frail --output",
    // …
  },
  "remarkConfig": {
    "plugins": [
      "remark-validate-links"
    ]
  },
  // …
}

💡 Tip: Add other tools such as prettier or ESLint to check and format other files.

💡 Tip: Run ./node_modules/.bin/remark --help for help with remark-cli.

Now you check and format markdown in your project with:

npm run format

Integration

remark-validate-links can detect anchors on nodes through several properties on nodes:

Types

This package is fully typed with TypeScript. It exports the additional types Options and UrlConfig.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, remark-validate-links@^13, compatible with Node.js 16.

This plugin works with unified version 6+, remark version 7+, and remark-cli version 8+.

Security

remark-validate-links, in Node, accesses the file system based on user content, and this may be dangerous. In Node git remote and git rev-parse also runs for processed files.

The tree is not modified, so there are no openings for cross-site scripting (XSS) attacks.

Contribute

See contributing.md in remarkjs/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer