mirror of
https://github.com/theoludwig/markdownlint-rule-relative-links.git
synced 2025-05-27 11:37:24 +02:00
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
b6b092dc1f | |||
56882727fc |
31
README.md
31
README.md
@ -4,15 +4,13 @@
|
|||||||
<strong>Custom rule for <a href="https://github.com/DavidAnson/markdownlint">markdownlint</a> to validate relative links.</strong>
|
<strong>Custom rule for <a href="https://github.com/DavidAnson/markdownlint">markdownlint</a> to validate relative links.</strong>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="./CONTRIBUTING.md"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" /></a>
|
<a href="./CONTRIBUTING.md"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" /></a>
|
||||||
<a href="./LICENSE"><img src="https://img.shields.io/badge/licence-MIT-blue.svg" alt="Licence MIT"/></a>
|
<a href="./LICENSE"><img src="https://img.shields.io/badge/licence-MIT-blue.svg" alt="Licence MIT"/></a>
|
||||||
<a href="./CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg" alt="Contributor Covenant" /></a>
|
<a href="./CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg" alt="Contributor Covenant" /></a>
|
||||||
<br />
|
<br />
|
||||||
<a href="https://github.com/Divlo/markdownlint-rule-relative-links/actions/workflows/lint.yml"><img src="https://github.com/Divlo/markdownlint-rule-relative-links/actions/workflows/lint.yml/badge.svg?branch=develop" /></a>
|
<a href="https://github.com/Divlo/markdownlint-rule-relative-links/actions/workflows/lint.yml"><img src="https://github.com/Divlo/markdownlint-rule-relative-links/actions/workflows/lint.yml/badge.svg?branch=develop" /></a>
|
||||||
<a href="https://github.com/Divlo/emarkdownlint-rule-relative-linksactions/workflows/test.yml"><img src="https://github.com/Divlo/markdownlint-rule-relative-links/actions/workflows/test.yml/badge.svg?branch=develop" /></a>
|
<a href="https://github.com/Divlo/markdownlint-rule-relative-linksactions/workflows/test.yml"><img src="https://github.com/Divlo/markdownlint-rule-relative-links/actions/workflows/test.yml/badge.svg?branch=develop" /></a>
|
||||||
<br />
|
<br />
|
||||||
<a href="https://conventionalcommits.org"><img src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg" alt="Conventional Commits" /></a>
|
<a href="https://conventionalcommits.org"><img src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg" alt="Conventional Commits" /></a>
|
||||||
<a href="https://github.com/semantic-release/semantic-release"><img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-release" /></a>
|
<a href="https://github.com/semantic-release/semantic-release"><img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-release" /></a>
|
||||||
@ -23,9 +21,32 @@
|
|||||||
|
|
||||||
**markdownlint-rule-relative-links** is a [markdownlint](https://github.com/DavidAnson/markdownlint) custom rule to validate relative links.
|
**markdownlint-rule-relative-links** is a [markdownlint](https://github.com/DavidAnson/markdownlint) custom rule to validate relative links.
|
||||||
|
|
||||||
It ensures that relative links that start with `./` or `../` (or not starting with external protocols like `http://` or `https://`) are working and not "dead" which means that it exists in the file system of the project that uses `markdownlint`.
|
It ensures that relative links (using `file:` protocol) are working and not "dead" which means that it exists in the file system of the project that uses `markdownlint`.
|
||||||
|
|
||||||
Related links:
|
### Example
|
||||||
|
|
||||||
|
File structure:
|
||||||
|
|
||||||
|
```txt
|
||||||
|
├── abc.txt
|
||||||
|
└── awesome.md
|
||||||
|
```
|
||||||
|
|
||||||
|
With `awesome.md` content:
|
||||||
|
|
||||||
|
```md
|
||||||
|
[abc](./abc.txt)
|
||||||
|
|
||||||
|
[Dead link](./dead.txt)
|
||||||
|
```
|
||||||
|
|
||||||
|
Running `markdownlint-cli2` with `markdownlint-rule-relative-links` will output:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
awesome.md:3 relative-links Relative links should be valid [Link "./dead.txt" is dead]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Related links
|
||||||
|
|
||||||
- [DavidAnson/markdownlint#253](https://github.com/DavidAnson/markdownlint/issues/253)
|
- [DavidAnson/markdownlint#253](https://github.com/DavidAnson/markdownlint/issues/253)
|
||||||
- [DavidAnson/markdownlint#121](https://github.com/DavidAnson/markdownlint/issues/121)
|
- [DavidAnson/markdownlint#121](https://github.com/DavidAnson/markdownlint/issues/121)
|
||||||
|
15
src/index.js
15
src/index.js
@ -40,14 +40,6 @@ const addError = (onError, lineNumber, detail, context, range, fixInfo) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const EXTERNAL_PROTOCOLS = new Set([
|
|
||||||
'http:',
|
|
||||||
'https:',
|
|
||||||
'mailto:',
|
|
||||||
'tel:',
|
|
||||||
'ftp:'
|
|
||||||
])
|
|
||||||
|
|
||||||
const customRule = {
|
const customRule = {
|
||||||
names: ['relative-links'],
|
names: ['relative-links'],
|
||||||
description: 'Relative links should be valid',
|
description: 'Relative links should be valid',
|
||||||
@ -79,11 +71,8 @@ const customRule = {
|
|||||||
if (hrefSrc != null) {
|
if (hrefSrc != null) {
|
||||||
const url = new URL(hrefSrc, pathToFileURL(params.name))
|
const url = new URL(hrefSrc, pathToFileURL(params.name))
|
||||||
url.hash = ''
|
url.hash = ''
|
||||||
const isRelative =
|
const isRelative = url.protocol === 'file:'
|
||||||
hrefSrc.startsWith('./') ||
|
if (isRelative && !fs.existsSync(url)) {
|
||||||
hrefSrc.startsWith('../') ||
|
|
||||||
!EXTERNAL_PROTOCOLS.has(url.protocol)
|
|
||||||
if (isRelative && !fs.existsSync(url.pathname)) {
|
|
||||||
const detail = `Link "${hrefSrc}" is dead`
|
const detail = `Link "${hrefSrc}" is dead`
|
||||||
addError(onError, lineNumber, detail)
|
addError(onError, lineNumber, detail)
|
||||||
}
|
}
|
||||||
|
6
test/fixtures/Valid.md
vendored
6
test/fixtures/Valid.md
vendored
@ -3,3 +3,9 @@
|
|||||||
[basic.js](../basic.test.js)
|
[basic.js](../basic.test.js)
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
[External https link](https://example.com/)
|
||||||
|
|
||||||
|
[External https link 2](https:./external.https)
|
||||||
|
|
||||||
|
[External ftp link](ftp:./external.ftp)
|
||||||
|
Reference in New Issue
Block a user