Skip to content

Commit

Permalink
feat: improve errors message to distinguish between file system and f…
Browse files Browse the repository at this point in the history
…ragment errors
  • Loading branch information
theoludwig committed Jun 27, 2023
1 parent 6c4e8ce commit a33c682
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

**markdownlint-rule-relative-links** is a [markdownlint](https://github.com/DavidAnson/markdownlint) custom rule to validate relative links.

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](https://github.com/DavidAnson/markdownlint).
It ensures that relative links (using `file:` protocol) are working and exists in the file system of the project that uses [markdownlint](https://github.com/DavidAnson/markdownlint).

### Example

Expand All @@ -37,13 +37,13 @@ With `awesome.md` content:
```md
[abc](./abc.txt)

[Dead link](./dead.txt)
[Invalid link](./invalid.txt)
```

Running [markdownlint-cli2](https://github.com/DavidAnson/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]
awesome.md:3 relative-links Relative links should be valid [Link "./invalid.txt" should exist in the file system]
```

### Related links
Expand Down
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ const customRule = {
const isRelative =
url.protocol === 'file:' && !hrefSrc.startsWith('/')
if (isRelative) {
const detail = `Link "${hrefSrc}" is not valid`
const detail = `Link "${hrefSrc}"`

if (!fs.existsSync(url)) {
addError(onError, lineNumber, detail)
addError(
onError,
lineNumber,
`${detail} should exist in the file system`
)
return
}

Expand All @@ -68,7 +72,11 @@ const customRule = {
})

if (!headingsHTMLFragments.includes(url.hash)) {
addError(onError, lineNumber, detail)
addError(
onError,
lineNumber,
`${detail} should have a valid fragment`
)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const addError = (onError, lineNumber, detail, context, range, fixInfo) => {
* Converts a Markdown heading into an HTML fragment according to the rules
* used by GitHub.
*
* Taken from <https://github.com/DavidAnson/markdownlint/blob/d01180ec5a014083ee9d574b693a8d7fbc1e566d/lib/md051.js#L19>
*
* @see https://github.com/DavidAnson/markdownlint/blob/d01180ec5a014083ee9d574b693a8d7fbc1e566d/lib/md051.js#L1
* @param {string} inlineText Inline token for heading.
* @returns {string} Fragment string for heading.
*/
Expand Down
6 changes: 3 additions & 3 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('ensure the rule validate correctly', async () => {
)
assert.equal(
lintResults['test/fixtures/Invalid.md'][0]?.errorDetail,
'Link "./basic.test.js" is not valid'
'Link "./basic.test.js" should exist in the file system'
)

assert.equal(
Expand All @@ -32,7 +32,7 @@ test('ensure the rule validate correctly', async () => {
)
assert.equal(
lintResults['test/fixtures/Invalid.md'][1]?.errorDetail,
'Link "../image.png" is not valid'
'Link "../image.png" should exist in the file system'
)

assert.equal(
Expand All @@ -41,6 +41,6 @@ test('ensure the rule validate correctly', async () => {
)
assert.equal(
lintResults['test/fixtures/Invalid.md'][2]?.errorDetail,
'Link "./Valid.md#not-existing-heading" is not valid'
'Link "./Valid.md#not-existing-heading" should have a valid fragment'
)
})

0 comments on commit a33c682

Please sign in to comment.