remark plugin to validate links to headings and files
npm install remark-validate-links[![Build][badge-build-image]][badge-build-url]
[![Coverage][badge-coverage-image]][badge-coverage-url]
[![Downloads][badge-downloads-image]][badge-downloads-url]
[![Size][badge-size-image]][badge-size-url]
[remark][github-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 that heading (welcome),
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.
* What is this?
* When should I use this?
* Install
* Use
* API
* [unified().use(remarkValidateLinks[, options])](#unifieduseremarkvalidatelinks-options)
* Options
* UrlConfig
* Examples
* Example: CLI
* Example: CLI in npm scripts
* Integration
* Compatibility
* Security
* Related
* Contribute
* License
This package is a [unified][github-unified] ([remark][github-remark])
plugin to check local links in a Git repo.
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 this plugin 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][github-remark-lint-no-dead-urls])
or undefined references (see
[remark-lint-no-undefined-references][github-remark-lint-undefined]).
This package is [ESM only][github-gist-esm].
In Node.js (version 16+),
install with [npm][npmjs-install]:
``sh`
npm install remark-validate-links
In Deno with [esm.sh][esmsh]:
`js`
import remarkValidateLinks from 'https://esm.sh/remark-validate-links@13'
In browsers with [esm.sh][esmsh]:
`html`
Say we have the following file example.md in this project:
`markdownAlpha
Links are checked:
This exists.
This one does not.
Headings in readme.md are checked.
And missing files are reported.
Definitions are also checked:
[alpha]: #alpha
[charlie]: #charlie
References w/o definitions are not checked: [delta]
`
…and a module example.js:
`js
import remarkValidateLinks from 'remark-validate-links'
import {remark} from 'remark'
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:
`markdown#apha
example.md
6:6-6:27 warning Cannot find heading for ; did you mean alpha missing-heading remark-validate-links:missing-headingmissing-example.js
11:5-11:53 warning Cannot find file missing-file remark-validate-links:missing-file#charlie
16:1-16:20 warning Cannot find heading for missing-heading remark-validate-links:missing-heading
⚠ 3 warnings
`
> 👉 Note:
> readme.md#no-such-heading is not warned about on the API,
> as the API does not check headings in other markdown files;
> the remark CLI is able to do that.
This package exports no identifiers.
The default export is [remarkValidateLinks][api-remark-validate-links].Options
It exports the [TypeScript][] types
[][api-options]UrlConfig
and
[][api-url-config].
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
* options ([Options][api-options], optional)
— configuration
###### Returns
Transform ([Transformer][github-unified-transformer]).
Configuration (TypeScript type).
###### Fields
* repositorystring
( or false, default: detected from Git remote)false
— URL to hosted Git;
if you’re not in a Git repository,
you must pass ;https://github.com/remarkjs/remark-validate-links/readme.md#install
if the repository resolves to something npm understands as a Git host such
as GitHub, GitLab, or Bitbucket,
then full URLs to that host
(say,
)root
are checked
* string
(, default: local Git folder)root
— path to Git root folder;
if both and repository are nullish,root
the Git root is detected;
if is not given but repository is,file.cwd
is usedskipPathPatterns
* Array
(, optional)new RegExp(pattern).test(value) === true
— list of patterns for paths that should be skipped;
each absolute local path + hash will be tested against each pattern and
will be ignored if ;/Users/tilde/path/to/repo/readme.md#some-heading
example value are then .urlConfig
* UrlConfig
([][api-url-config], default: detected from repo)github.com
— config on how hosted Git works;
, gitlab.com, or bitbucket.orgurlConfig
work automatically;
otherwise,
pass manually
Hosted Git info (TypeScript type).
###### Fields
* headingPrefixstring
(, optional, example: '#', '#markdown-header-')hostname
— prefix of headings
* string
(, optional, example: 'github.com','bitbucket.org'
)lines
— domain of URLs
* boolean
(, default: false)path
— whether lines in files can be linked
* string
(, optional, example:'/remarkjs/remark-validate-links/blob/'
,'/remarkjs/remark-validate-links/src/'
)topAnchor
— path prefix before files
* string
(, optional, example: #readme)
— hash to top of readme
###### Notes
For this repository (remarkjs/remark-validate-links on GitHub) urlConfig
looks as follows:
`js`
{
// Prefix of headings:
headingPrefix: '#',
// Domain of URLs:
hostname: 'github.com',
// Whether lines in files can be linked:
lines: true,
// Path prefix before files:
prefix: '/remarkjs/remark-validate-links/blob/',
// Hash to top of markdown documents:
topAnchor: '#readme'
}
If this project were hosted on Bitbucket,
the config would be:
`js`
{
headingPrefix: '#markdown-header-',
hostname: 'bitbucket.org',
lines: false,
prefix: '/remarkjs/remark-validate-links/src/'
}
It’s recommended to use remark-validate-links on the CLI withremark-cli
[][github-remark-cli].
Install both with [npm][npmjs-install]:
`sh`
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:
`markdownHello
Read more whoops, this does not exist.
This doesn’t exist either whoops!.
But this does exist: license.
So does this: readme.
`
Now,
running ./node_modules/.bin/remark --use remark-validate-links . yields:
`textworld
example.md
3:11-3:48 warning Link to unknown heading: missing-heading remark-validate-linksreadme.md
5:27-5:51 warning Link to unknown heading in : foo missing-heading-in-file remark-validate-links
readme.md: no issues found
⚠ 2 warnings
`
You can use remark-validate-links and [remark-cli][github-remark-cli] in
an npm script to check and format markdown in your project.
Install both with [npm][npmjs-install]:
`sh`
npm install remark-cli remark-validate-links --save-dev
Then,
add a format script and configuration to package.json:
`js`
{
// …
"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:
`sh`
npm run format
remark-validate-links can detect anchors on nodes through several properties
on nodes:
* node.data.hProperties.namemdast-util-to-hast
— used by
[][github-mdast-util-to-hast-notes]name
to create a attribute,node.data.hProperties.id
which anchors can link to
* mdast-util-to-hast
— used by
[][github-mdast-util-to-hast-notes]id
to create an attribute,node.data.id
which anchors can link to
*
— used potentially in the future by other tools to signal unique identifiers
on nodes
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+,remark-cli
and version 8+.
remark-validate-links,git remote
in Node,
accesses the file system based on user content,
and this may be dangerous.
In Node and git rev-parse also runs for processed files.
The tree is not modified,
so there are no openings for [cross-site scripting (XSS)][wikipedia-xss]
attacks.
* [remark-lint][github-remark-lint]remark-lint-no-dead-urls
— markdown code style linter
* [][github-remark-lint-no-dead-urls]
— check that external links are alive
See [contributing.md][health-contributing]remarkjs/.github
in
[][health]support.md`][health-support] for ways to get help.
for ways to get started.
See [
This project has a [code of conduct][health-coc].
By interacting with this repository,
organization,
or community you agree to abide by its terms.
[MIT][file-license] © [Titus Wormer][wooorm]
[api-options]: #options
[api-remark-validate-links]: #unifieduseremarkvalidatelinks-options
[api-url-config]: #urlconfig
[badge-build-image]: https://github.com/remarkjs/remark-validate-links/workflows/main/badge.svg
[badge-build-url]: https://github.com/remarkjs/remark-validate-links/actions
[badge-coverage-image]: https://img.shields.io/codecov/c/github/remarkjs/remark-validate-links.svg
[badge-coverage-url]: https://codecov.io/github/remarkjs/remark-validate-links
[badge-downloads-image]: https://img.shields.io/npm/dm/remark-validate-links.svg
[badge-downloads-url]: https://www.npmjs.com/package/remark-validate-links
[badge-size-image]: https://img.shields.io/bundlejs/size/remark-validate-links
[badge-size-url]: https://bundlejs.com/?q=remark-validate-links
[esmsh]: https://esm.sh
[file-license]: license
[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[github-mdast-util-to-hast-notes]: https://github.com/syntax-tree/mdast-util-to-hast#notes
[github-remark]: https://github.com/remarkjs/remark
[github-remark-cli]: https://github.com/remarkjs/remark/tree/main/packages/remark-cli#readme
[github-remark-lint]: https://github.com/remarkjs/remark-lint
[github-remark-lint-no-dead-urls]: https://github.com/remarkjs/remark-lint-no-dead-urls
[github-remark-lint-undefined]: https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-no-undefined-references
[github-unified]: https://github.com/unifiedjs/unified
[github-unified-transformer]: https://github.com/unifiedjs/unified#transformer
[health]: https://github.com/remarkjs/.github
[health-coc]: https://github.com/remarkjs/.github/blob/main/code-of-conduct.md
[health-contributing]: https://github.com/remarkjs/.github/blob/main/contributing.md
[health-support]: https://github.com/remarkjs/.github/blob/main/support.md
[npmjs-install]: https://docs.npmjs.com/cli/install
[typescript]: https://www.typescriptlang.org
[wikipedia-xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
[wooorm]: https://wooorm.com