fix: only detect file: protocol links

fixes #1
This commit is contained in:
Divlo 2023-01-06 18:37:40 +01:00
parent 56882727fc
commit b6b092dc1f
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
3 changed files with 33 additions and 15 deletions

View File

@ -21,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)

View File

@ -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)
} }

View File

@ -3,3 +3,9 @@
[basic.js](../basic.test.js) [basic.js](../basic.test.js)
![Image](./image.png) ![Image](./image.png)
[External https link](https://example.com/)
[External https link 2](https:./external.https)
[External ftp link](ftp:./external.ftp)