feat: validate relative links fragments

Similar to https://github.com/DavidAnson/markdownlint/blob/main/doc/md051.md

Fixes #2

BREAKING CHANGE: Validate links fragments in relative links
This commit is contained in:
2023-06-24 11:42:09 +02:00
parent 9e28311791
commit 6c4e8cec9c
8 changed files with 237 additions and 53 deletions

View File

@ -5,16 +5,17 @@ const { markdownlint } = require('markdownlint').promises
const relativeLinks = require('../src/index.js')
test('ensure we validate correctly', async () => {
test('ensure the rule validate correctly', async () => {
const lintResults = await markdownlint({
files: ['test/fixtures/Valid.md', 'test/fixtures/Invalid.md'],
config: {
default: false,
'relative-links': true
},
customRules: [relativeLinks]
})
assert.equal(lintResults['test/fixtures/Valid.md'].length, 0)
assert.equal(lintResults['test/fixtures/Invalid.md'].length, 2)
assert.equal(lintResults['test/fixtures/Invalid.md'].length, 3)
assert.equal(
lintResults['test/fixtures/Invalid.md'][0]?.ruleDescription,
@ -22,7 +23,7 @@ test('ensure we validate correctly', async () => {
)
assert.equal(
lintResults['test/fixtures/Invalid.md'][0]?.errorDetail,
'Link "./basic.test.js" is dead'
'Link "./basic.test.js" is not valid'
)
assert.equal(
@ -31,6 +32,15 @@ test('ensure we validate correctly', async () => {
)
assert.equal(
lintResults['test/fixtures/Invalid.md'][1]?.errorDetail,
'Link "../image.png" is dead'
'Link "../image.png" is not valid'
)
assert.equal(
lintResults['test/fixtures/Invalid.md'][2]?.ruleDescription,
'Relative links should be valid'
)
assert.equal(
lintResults['test/fixtures/Invalid.md'][2]?.errorDetail,
'Link "./Valid.md#not-existing-heading" is not valid'
)
})

View File

@ -3,3 +3,17 @@
[basic.js](./basic.test.js)
![Image](../image.png)
[Link fragment](./Valid.md#not-existing-heading)
## Existing Heading
### Repeated Heading
Text
### Repeated Heading
Text
### Repeated Heading

View File

@ -11,3 +11,11 @@
[External https link 2](https:./external.https)
[External ftp link](ftp:./external.ftp)
[Link fragment](./Invalid.md#existing-heading)
[Link fragment Repeated 0](./Invalid.md#repeated-heading)
[Link fragment Repeated 1](./Invalid.md#repeated-heading-1)
[Link fragment Repeated 2](./Invalid.md#repeated-heading-2)

37
test/utils.test.js Normal file
View File

@ -0,0 +1,37 @@
const test = require('node:test')
const assert = require('node:assert/strict')
const {
convertHeadingToHTMLFragment,
getMarkdownHeadings
} = require('../src/utils.js')
test('utils', async (t) => {
await t.test('convertHeadingToHTMLFragment', async () => {
assert.strictEqual(
convertHeadingToHTMLFragment('Valid Fragments'),
'#valid-fragments'
)
assert.strictEqual(
convertHeadingToHTMLFragment('Valid Heading With Underscores _'),
'#valid-heading-with-underscores-_'
)
assert.strictEqual(
convertHeadingToHTMLFragment(
`Valid Heading With Quotes ' And Double Quotes "`
),
'#valid-heading-with-quotes--and-double-quotes-'
)
assert.strictEqual(
convertHeadingToHTMLFragment('🚀 Valid Heading With Emoji'),
'#-valid-heading-with-emoji'
)
})
await t.test('getMarkdownHeadings', async () => {
assert.deepStrictEqual(
getMarkdownHeadings('# Hello\n\n## World\n\n## Hello, world!\n'),
['Hello', 'World', 'Hello, world!']
)
})
})