mirror of
https://github.com/theoludwig/markdownlint-rule-relative-links.git
synced 2025-05-27 11:37:24 +02:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
c985af6156 | |||
77b8988bea | |||
b6b092dc1f | |||
56882727fc | |||
5dab1976d3 |
4
.github/workflows/lint.yml
vendored
4
.github/workflows/lint.yml
vendored
@ -10,10 +10,10 @@ jobs:
|
|||||||
lint:
|
lint:
|
||||||
runs-on: 'ubuntu-latest'
|
runs-on: 'ubuntu-latest'
|
||||||
steps:
|
steps:
|
||||||
- uses: 'actions/checkout@v3.0.0'
|
- uses: 'actions/checkout@v3.5.0'
|
||||||
|
|
||||||
- name: 'Use Node.js'
|
- name: 'Use Node.js'
|
||||||
uses: 'actions/setup-node@v3.1.0'
|
uses: 'actions/setup-node@v3.6.0'
|
||||||
with:
|
with:
|
||||||
node-version: 'lts/*'
|
node-version: 'lts/*'
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
|
4
.github/workflows/release.yml
vendored
4
.github/workflows/release.yml
vendored
@ -8,13 +8,13 @@ jobs:
|
|||||||
release:
|
release:
|
||||||
runs-on: 'ubuntu-latest'
|
runs-on: 'ubuntu-latest'
|
||||||
steps:
|
steps:
|
||||||
- uses: 'actions/checkout@v3.0.0'
|
- uses: 'actions/checkout@v3.5.0'
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
||||||
- name: 'Use Node.js'
|
- name: 'Use Node.js'
|
||||||
uses: 'actions/setup-node@v3.1.0'
|
uses: 'actions/setup-node@v3.6.0'
|
||||||
with:
|
with:
|
||||||
node-version: 'lts/*'
|
node-version: 'lts/*'
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
|
4
.github/workflows/test.yml
vendored
4
.github/workflows/test.yml
vendored
@ -10,10 +10,10 @@ jobs:
|
|||||||
test:
|
test:
|
||||||
runs-on: 'ubuntu-latest'
|
runs-on: 'ubuntu-latest'
|
||||||
steps:
|
steps:
|
||||||
- uses: 'actions/checkout@v3.0.0'
|
- uses: 'actions/checkout@v3.5.0'
|
||||||
|
|
||||||
- name: 'Use Node.js'
|
- name: 'Use Node.js'
|
||||||
uses: 'actions/setup-node@v3.1.0'
|
uses: 'actions/setup-node@v3.6.0'
|
||||||
with:
|
with:
|
||||||
node-version: 'lts/*'
|
node-version: 'lts/*'
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
|
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)
|
||||||
|
13801
package-lock.json
generated
13801
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
32
package.json
32
package.json
@ -29,7 +29,7 @@
|
|||||||
"lint:commit": "commitlint",
|
"lint:commit": "commitlint",
|
||||||
"lint:editorconfig": "editorconfig-checker",
|
"lint:editorconfig": "editorconfig-checker",
|
||||||
"lint:markdown": "markdownlint-cli2",
|
"lint:markdown": "markdownlint-cli2",
|
||||||
"lint:eslint": "eslint \"**/*.{js,jsx,ts,tsx}\" --ignore-path \".gitignore\"",
|
"lint:eslint": "eslint \".\" --ignore-path \".gitignore\"",
|
||||||
"lint:prettier": "prettier \".\" --check --ignore-path \".gitignore\"",
|
"lint:prettier": "prettier \".\" --check --ignore-path \".gitignore\"",
|
||||||
"lint:staged": "lint-staged",
|
"lint:staged": "lint-staged",
|
||||||
"test": "tap",
|
"test": "tap",
|
||||||
@ -39,24 +39,24 @@
|
|||||||
"postpublish": "pinst --enable"
|
"postpublish": "pinst --enable"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@commitlint/cli": "17.3.0",
|
"@commitlint/cli": "17.5.1",
|
||||||
"@commitlint/config-conventional": "17.3.0",
|
"@commitlint/config-conventional": "17.4.4",
|
||||||
"@types/tap": "15.0.7",
|
"@types/tap": "15.0.8",
|
||||||
"editorconfig-checker": "4.0.2",
|
"editorconfig-checker": "5.0.1",
|
||||||
"eslint": "8.31.0",
|
"eslint": "8.37.0",
|
||||||
"eslint-config-conventions": "6.0.0",
|
"eslint-config-conventions": "8.0.0",
|
||||||
"eslint-config-prettier": "8.5.0",
|
"eslint-config-prettier": "8.8.0",
|
||||||
"eslint-plugin-import": "2.26.0",
|
"eslint-plugin-import": "2.27.5",
|
||||||
"eslint-plugin-prettier": "4.2.1",
|
"eslint-plugin-prettier": "4.2.1",
|
||||||
"eslint-plugin-promise": "6.1.1",
|
"eslint-plugin-promise": "6.1.1",
|
||||||
"eslint-plugin-unicorn": "45.0.2",
|
"eslint-plugin-unicorn": "46.0.0",
|
||||||
"husky": "8.0.2",
|
"husky": "8.0.3",
|
||||||
"lint-staged": "13.1.0",
|
"lint-staged": "13.2.0",
|
||||||
"markdownlint": "0.27.0",
|
"markdownlint": "0.28.0",
|
||||||
"markdownlint-cli2": "0.6.0",
|
"markdownlint-cli2": "0.6.0",
|
||||||
"pinst": "3.0.0",
|
"pinst": "3.0.0",
|
||||||
"prettier": "2.8.1",
|
"prettier": "2.8.7",
|
||||||
"semantic-release": "19.0.5",
|
"semantic-release": "21.0.1",
|
||||||
"tap": "16.3.2"
|
"tap": "16.3.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
43
src/index.js
43
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',
|
||||||
@ -56,23 +48,36 @@ const customRule = {
|
|||||||
filterTokens(params, 'inline', (token) => {
|
filterTokens(params, 'inline', (token) => {
|
||||||
token.children.forEach((child) => {
|
token.children.forEach((child) => {
|
||||||
const { lineNumber, type, attrs } = child
|
const { lineNumber, type, attrs } = child
|
||||||
|
|
||||||
|
/** @type {string | null} */
|
||||||
|
let hrefSrc = null
|
||||||
|
|
||||||
if (type === 'link_open') {
|
if (type === 'link_open') {
|
||||||
attrs.forEach((attr) => {
|
attrs.forEach((attr) => {
|
||||||
if (attr[0] === 'href') {
|
if (attr[0] === 'href') {
|
||||||
const href = attr[1]
|
hrefSrc = attr[1]
|
||||||
const url = new URL(href, pathToFileURL(params.name))
|
|
||||||
url.hash = ''
|
|
||||||
const isRelative =
|
|
||||||
href.startsWith('./') ||
|
|
||||||
href.startsWith('../') ||
|
|
||||||
!EXTERNAL_PROTOCOLS.has(url.protocol)
|
|
||||||
if (isRelative && !fs.existsSync(url.pathname)) {
|
|
||||||
const detail = `Link "${href}" is dead`
|
|
||||||
addError(onError, lineNumber, detail)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type === 'image') {
|
||||||
|
attrs.forEach((attr) => {
|
||||||
|
if (attr[0] === 'src') {
|
||||||
|
hrefSrc = attr[1]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hrefSrc != null) {
|
||||||
|
const url = new URL(hrefSrc, pathToFileURL(params.name))
|
||||||
|
url.hash = ''
|
||||||
|
const isRelative =
|
||||||
|
url.protocol === 'file:' && !hrefSrc.startsWith('/')
|
||||||
|
if (isRelative && !fs.existsSync(url)) {
|
||||||
|
const detail = `Link "${hrefSrc}" is dead`
|
||||||
|
addError(onError, lineNumber, detail)
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -12,13 +12,23 @@ tap.test('ensure we validate correctly', async (t) => {
|
|||||||
customRules: [relativeLinks]
|
customRules: [relativeLinks]
|
||||||
})
|
})
|
||||||
t.equal(lintResults['test/fixtures/Valid.md'].length, 0)
|
t.equal(lintResults['test/fixtures/Valid.md'].length, 0)
|
||||||
t.equal(lintResults['test/fixtures/Invalid.md'].length, 1)
|
t.equal(lintResults['test/fixtures/Invalid.md'].length, 2)
|
||||||
|
|
||||||
t.equal(
|
t.equal(
|
||||||
lintResults['test/fixtures/Invalid.md'][0].ruleDescription,
|
lintResults['test/fixtures/Invalid.md'][0]?.ruleDescription,
|
||||||
'Relative links should be valid'
|
'Relative links should be valid'
|
||||||
)
|
)
|
||||||
t.equal(
|
t.equal(
|
||||||
lintResults['test/fixtures/Invalid.md'][0].errorDetail,
|
lintResults['test/fixtures/Invalid.md'][0]?.errorDetail,
|
||||||
'Link "./basic.test.js" is dead'
|
'Link "./basic.test.js" is dead'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
t.equal(
|
||||||
|
lintResults['test/fixtures/Invalid.md'][1]?.ruleDescription,
|
||||||
|
'Relative links should be valid'
|
||||||
|
)
|
||||||
|
t.equal(
|
||||||
|
lintResults['test/fixtures/Invalid.md'][1]?.errorDetail,
|
||||||
|
'Link "../image.png" is dead'
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
2
test/fixtures/Invalid.md
vendored
2
test/fixtures/Invalid.md
vendored
@ -1,3 +1,5 @@
|
|||||||
# Invalid
|
# Invalid
|
||||||
|
|
||||||
[basic.js](./basic.test.js)
|
[basic.js](./basic.test.js)
|
||||||
|
|
||||||
|

|
||||||
|
10
test/fixtures/Valid.md
vendored
10
test/fixtures/Valid.md
vendored
@ -1,3 +1,13 @@
|
|||||||
# Valid
|
# Valid
|
||||||
|
|
||||||
[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)
|
||||||
|
BIN
test/fixtures/image.png
vendored
Executable file
BIN
test/fixtures/image.png
vendored
Executable file
Binary file not shown.
After Width: | Height: | Size: 95 B |
Reference in New Issue
Block a user