From 450cdb84f0f06d4daacf97a6ec6c5fde77d7d887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Mon, 27 May 2024 22:26:21 +0200 Subject: [PATCH] feat: support columns numbers (and line range) in links fragments Fixes #7 --- src/index.js | 5 + src/utils.js | 3 + .../awesome.md | 1 + ...valid-line-column-range-number-fragment.md | 31 +++++++ .../awesome.md | 3 + ...valid-line-column-range-number-fragment.md | 11 +++ test/index.test.js | 92 ++++++++++++++----- 7 files changed, 123 insertions(+), 23 deletions(-) create mode 100644 test/fixtures/invalid/invalid-line-column-range-number-fragment/awesome.md create mode 100644 test/fixtures/invalid/invalid-line-column-range-number-fragment/invalid-line-column-range-number-fragment.md create mode 100644 test/fixtures/valid/valid-line-column-range-number-fragment/awesome.md create mode 100644 test/fixtures/valid/valid-line-column-range-number-fragment/valid-line-column-range-number-fragment.md diff --git a/src/index.js b/src/index.js index d2c99fe..463de35 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,7 @@ const { isValidIntegerString, getNumberOfLines, getLineNumberStringFromFragment, + lineFragmentRe, } = require("./utils.js") /** @typedef {import('markdownlint').Rule} MarkdownLintRule */ @@ -133,6 +134,10 @@ const customRule = { const hasOnlyDigits = isValidIntegerString(lineNumberFragmentString) if (!hasOnlyDigits) { + if (lineFragmentRe.test(url.hash)) { + continue + } + onError({ lineNumber, detail: `${detail} should have a valid fragment identifier`, diff --git a/src/utils.js b/src/utils.js index a1b28e8..3a7c458 100644 --- a/src/utils.js +++ b/src/utils.js @@ -4,6 +4,8 @@ const { getHtmlAttributeRe } = require("./markdownlint-rule-helpers/helpers.js") const markdownIt = new MarkdownIt({ html: true }) +const lineFragmentRe = /^#(?:L\d+(?:C\d+)?-L\d+(?:C\d+)?|L\d+)$/ + /** * Converts a Markdown heading into an HTML fragment according to the rules * used by GitHub. @@ -151,6 +153,7 @@ const getLineNumberStringFromFragment = (fragment) => { } module.exports = { + lineFragmentRe, convertHeadingToHTMLFragment, getMarkdownHeadings, getMarkdownIdOrAnchorNameFragments, diff --git a/test/fixtures/invalid/invalid-line-column-range-number-fragment/awesome.md b/test/fixtures/invalid/invalid-line-column-range-number-fragment/awesome.md new file mode 100644 index 0000000..5dce6a7 --- /dev/null +++ b/test/fixtures/invalid/invalid-line-column-range-number-fragment/awesome.md @@ -0,0 +1 @@ +# Awesome diff --git a/test/fixtures/invalid/invalid-line-column-range-number-fragment/invalid-line-column-range-number-fragment.md b/test/fixtures/invalid/invalid-line-column-range-number-fragment/invalid-line-column-range-number-fragment.md new file mode 100644 index 0000000..84f7855 --- /dev/null +++ b/test/fixtures/invalid/invalid-line-column-range-number-fragment/invalid-line-column-range-number-fragment.md @@ -0,0 +1,31 @@ +# Invalid + +[Invalid](./awesome.md#L12-not-a-line-link) + +[Invalid](./awesome.md#l7) + +[Invalid](./awesome.md#L) + +[Invalid](./awesome.md#L7extra) + +[Invalid](./awesome.md#L30C) + +[Invalid](./awesome.md#L30Cextra) + +[Invalid](./awesome.md#L30L12) + +[Invalid](./awesome.md#L30C12) + +[Invalid](./awesome.md#L30C11-) + +[Invalid](./awesome.md#L30C11-L) + +[Invalid](./awesome.md#L30C11-L31C) + +[Invalid](./awesome.md#L30C11-C31) + +[Invalid](./awesome.md#C30) + +[Invalid](./awesome.md#C11-C31) + +[Invalid](./awesome.md#C11-L4C31) diff --git a/test/fixtures/valid/valid-line-column-range-number-fragment/awesome.md b/test/fixtures/valid/valid-line-column-range-number-fragment/awesome.md new file mode 100644 index 0000000..fca734f --- /dev/null +++ b/test/fixtures/valid/valid-line-column-range-number-fragment/awesome.md @@ -0,0 +1,3 @@ +# Awesome + +## L12 Not A Line Link diff --git a/test/fixtures/valid/valid-line-column-range-number-fragment/valid-line-column-range-number-fragment.md b/test/fixtures/valid/valid-line-column-range-number-fragment/valid-line-column-range-number-fragment.md new file mode 100644 index 0000000..a4f8e89 --- /dev/null +++ b/test/fixtures/valid/valid-line-column-range-number-fragment/valid-line-column-range-number-fragment.md @@ -0,0 +1,11 @@ +# Valid + +[Valid](./awesome.md#l12-not-a-line-link) + +[Valid](./awesome.md#L30-L31) + +[Valid](./awesome.md#L3C24-L88) + +[Valid](./awesome.md#L304-L314C98) + +[Valid](./awesome.md#L200C4-L3244C2) diff --git a/test/index.test.js b/test/index.test.js index 7bf5a87..4726a43 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -29,92 +29,126 @@ test("ensure the rule validates correctly", async (t) => { name: "should be invalid with an empty id fragment", fixturePath: "test/fixtures/invalid/empty-id-fragment/empty-id-fragment.md", - error: '"./awesome.md#" should have a valid fragment identifier', + errors: ['"./awesome.md#" should have a valid fragment identifier'], }, { name: "should be invalid with a name fragment other than for an anchor", fixturePath: "test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/ignore-name-fragment-if-not-an-anchor.md", - error: + errors: [ '"./awesome.md#name-should-be-ignored" should have a valid fragment identifier', + ], }, { name: "should be invalid with a non-existing id fragment (data-id !== id)", fixturePath: "test/fixtures/invalid/ignore-not-an-id-fragment/ignore-not-an-id-fragment.md", - error: + errors: [ '"./awesome.md#not-an-id-should-be-ignored" should have a valid fragment identifier', + ], }, { name: "should be invalid with invalid heading with #L fragment", fixturePath: "test/fixtures/invalid/invalid-heading-with-L-fragment/invalid-heading-with-L-fragment.md", - error: '"./awesome.md#L7abc" should have a valid fragment identifier', + errors: [ + '"./awesome.md#L7abc" should have a valid fragment identifier', + ], + }, + { + name: "should be invalid with a invalid line column range number fragment", + fixturePath: + "test/fixtures/invalid/invalid-line-column-range-number-fragment/invalid-line-column-range-number-fragment.md", + errors: [ + '"./awesome.md#L12-not-a-line-link" should have a valid fragment identifier', + '"./awesome.md#l7" should have a valid fragment identifier', + '"./awesome.md#L" should have a valid fragment identifier', + '"./awesome.md#L7extra" should have a valid fragment identifier', + '"./awesome.md#L30C" should have a valid fragment identifier', + '"./awesome.md#L30Cextra" should have a valid fragment identifier', + '"./awesome.md#L30L12" should have a valid fragment identifier', + '"./awesome.md#L30C12" should have a valid fragment identifier', + '"./awesome.md#L30C11-" should have a valid fragment identifier', + '"./awesome.md#L30C11-L" should have a valid fragment identifier', + '"./awesome.md#L30C11-L31C" should have a valid fragment identifier', + '"./awesome.md#L30C11-C31" should have a valid fragment identifier', + '"./awesome.md#C30" should have a valid fragment identifier', + '"./awesome.md#C11-C31" should have a valid fragment identifier', + '"./awesome.md#C11-L4C31" should have a valid fragment identifier', + ], }, { name: "should be invalid with a invalid line number fragment", fixturePath: "test/fixtures/invalid/invalid-line-number-fragment/invalid-line-number-fragment.md", - error: + errors: [ '"./awesome.md#L7" should have a valid fragment identifier, "./awesome.md#L7" should have at least 7 lines to be valid', + ], }, { name: "should be invalid with a non-existing anchor name fragment", fixturePath: "test/fixtures/invalid/non-existing-anchor-name-fragment/non-existing-anchor-name-fragment.md", - error: + errors: [ '"./awesome.md#non-existing-anchor-name-fragment" should have a valid fragment identifier', + ], }, { name: "should be invalid with a non-existing element id fragment", fixturePath: "test/fixtures/invalid/non-existing-element-id-fragment/non-existing-element-id-fragment.md", - error: + errors: [ '"./awesome.md#non-existing-element-id-fragment" should have a valid fragment identifier', + ], }, { name: "should be invalid with a non-existing heading fragment", fixturePath: "test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md", - error: + errors: [ '"./awesome.md#non-existing-heading" should have a valid fragment identifier', + ], }, { name: "should be invalid with a link to an image with a empty fragment", fixturePath: "test/fixtures/invalid/ignore-empty-fragment-checking-for-image.md", - error: + errors: [ '"../image.png#" should not have a fragment identifier as it is an image', + ], }, { name: "should be invalid with a link to an image with a fragment", fixturePath: "test/fixtures/invalid/ignore-fragment-checking-for-image.md", - error: + errors: [ '"../image.png#non-existing-fragment" should not have a fragment identifier as it is an image', + ], }, { name: "should be invalid with a non-existing file", fixturePath: "test/fixtures/invalid/non-existing-file.md", - error: '"./index.test.js" should exist in the file system', + errors: ['"./index.test.js" should exist in the file system'], }, { name: "should be invalid with a non-existing image", fixturePath: "test/fixtures/invalid/non-existing-image.md", - error: '"./image.png" should exist in the file system', + errors: ['"./image.png" should exist in the file system'], }, ] - for (const { name, fixturePath, error } of testCases) { + for (const { name, fixturePath, errors } of testCases) { await t.test(name, async () => { - const lintResults = await validateMarkdownLint(fixturePath) - assert.equal(lintResults?.length, 1) - assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names) - assert.equal( - lintResults?.[0]?.ruleDescription, - relativeLinksRule.description, - ) - assert.equal(lintResults?.[0]?.errorDetail, error) + const lintResults = (await validateMarkdownLint(fixturePath)) ?? [] + const errorsDetails = lintResults.map((result) => { + assert.deepEqual(result.ruleNames, relativeLinksRule.names) + assert.deepEqual( + result.ruleDescription, + relativeLinksRule.description, + ) + return result.errorDetail + }) + assert.deepStrictEqual(errorsDetails, errors) }) } }) @@ -151,6 +185,11 @@ test("ensure the rule validates correctly", async (t) => { fixturePath: "test/fixtures/valid/only-parse-markdown-files-for-fragments/only-parse-markdown-files-for-fragments.md", }, + { + name: "should support lines and columns range numbers in link fragments", + fixturePath: + "test/fixtures/valid/valid-line-column-range-number-fragment/valid-line-column-range-number-fragment.md", + }, { name: 'should be valid with valid heading "like" line number fragment', fixturePath: @@ -186,8 +225,15 @@ test("ensure the rule validates correctly", async (t) => { for (const { name, fixturePath } of testCases) { await t.test(name, async () => { - const lintResults = await validateMarkdownLint(fixturePath) - assert.equal(lintResults?.length, 0) + const lintResults = (await validateMarkdownLint(fixturePath)) ?? [] + const errorsDetails = lintResults.map((result) => { + return result.errorDetail + }) + assert.equal( + errorsDetails.length, + 0, + `Expected no errors, got ${errorsDetails.join(", ")}`, + ) }) } })