From a33c682974189fdd97ffcbbdf167d397491932f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Tue, 27 Jun 2023 13:15:03 +0200 Subject: [PATCH] feat: improve errors message to distinguish between file system and fragment errors --- README.md | 6 +++--- src/index.js | 14 +++++++++++--- src/utils.js | 3 +-- test/basic.test.js | 6 +++--- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c535187..bb2b370 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/index.js b/src/index.js index 800b65d..c0d2a78 100644 --- a/src/index.js +++ b/src/index.js @@ -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 } @@ -68,7 +72,11 @@ const customRule = { }) if (!headingsHTMLFragments.includes(url.hash)) { - addError(onError, lineNumber, detail) + addError( + onError, + lineNumber, + `${detail} should have a valid fragment` + ) } } } diff --git a/src/utils.js b/src/utils.js index f2ebe29..dff6bc1 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 - * + * @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. */ diff --git a/test/basic.test.js b/test/basic.test.js index ce0ded7..5d7965b 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -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( @@ -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( @@ -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' ) })