From 85f465306f780a76d8ffdc610c8c95f4aa84fe6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Mon, 27 May 2024 22:50:43 +0200 Subject: [PATCH] feat: stricter validation of heading fragments by being Case sensitive Fixes #8 BREAKING CHANGE: Heading fragments is now Case sensitive. For example "#ExistIng-Heading" is invalid as it should be "#existing-heading". --- src/index.js | 4 +++- src/utils.js | 2 +- .../awesome.md | 0 .../invalid-heading-case-sensitive.md} | 0 .../valid-heading-like-number-fragment.md | 2 +- test/index.test.js | 21 ++++++++++++------- 6 files changed, 19 insertions(+), 10 deletions(-) rename test/fixtures/{valid/existing-heading-case-insensitive => invalid/invalid-heading-case-sensitive}/awesome.md (100%) rename test/fixtures/{valid/existing-heading-case-insensitive/existing-heading-case-insensitive.md => invalid/invalid-heading-case-sensitive/invalid-heading-case-sensitive.md} (100%) diff --git a/src/index.js b/src/index.js index 463de35..37c9fd3 100644 --- a/src/index.js +++ b/src/index.js @@ -126,7 +126,7 @@ const customRule = { fragmentsHTML.push(...idOrAnchorNameHTMLFragments) - if (!fragmentsHTML.includes(url.hash.toLowerCase())) { + if (!fragmentsHTML.includes(url.hash)) { if (url.hash.startsWith("#L")) { const lineNumberFragmentString = getLineNumberStringFromFragment( url.hash, @@ -157,6 +157,8 @@ const customRule = { }) continue } + + continue } onError({ diff --git a/src/utils.js b/src/utils.js index 3a7c458..dc64ebe 100644 --- a/src/utils.js +++ b/src/utils.js @@ -28,7 +28,7 @@ const convertHeadingToHTMLFragment = (inlineText) => { "", ) .replace(/ /gu, "-"), - ).toLowerCase() + ) ) } diff --git a/test/fixtures/valid/existing-heading-case-insensitive/awesome.md b/test/fixtures/invalid/invalid-heading-case-sensitive/awesome.md similarity index 100% rename from test/fixtures/valid/existing-heading-case-insensitive/awesome.md rename to test/fixtures/invalid/invalid-heading-case-sensitive/awesome.md diff --git a/test/fixtures/valid/existing-heading-case-insensitive/existing-heading-case-insensitive.md b/test/fixtures/invalid/invalid-heading-case-sensitive/invalid-heading-case-sensitive.md similarity index 100% rename from test/fixtures/valid/existing-heading-case-insensitive/existing-heading-case-insensitive.md rename to test/fixtures/invalid/invalid-heading-case-sensitive/invalid-heading-case-sensitive.md diff --git a/test/fixtures/valid/valid-heading-like-number-fragment/valid-heading-like-number-fragment.md b/test/fixtures/valid/valid-heading-like-number-fragment/valid-heading-like-number-fragment.md index 31d7886..f65382b 100644 --- a/test/fixtures/valid/valid-heading-like-number-fragment/valid-heading-like-number-fragment.md +++ b/test/fixtures/valid/valid-heading-like-number-fragment/valid-heading-like-number-fragment.md @@ -1,3 +1,3 @@ # Valid -[Link fragment](./awesome.md#L7) +[Link fragment](./awesome.md#l7) diff --git a/test/index.test.js b/test/index.test.js index 4726a43..d5081c9 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -47,6 +47,14 @@ test("ensure the rule validates correctly", async (t) => { '"./awesome.md#not-an-id-should-be-ignored" should have a valid fragment identifier', ], }, + { + name: "should be invalid with uppercase letters in fragment (case sensitive)", + fixturePath: + "test/fixtures/invalid/invalid-heading-case-sensitive/invalid-heading-case-sensitive.md", + errors: [ + '"./awesome.md#ExistIng-Heading" should have a valid fragment identifier', + ], + }, { name: "should be invalid with invalid heading with #L fragment", fixturePath: @@ -148,7 +156,11 @@ test("ensure the rule validates correctly", async (t) => { ) return result.errorDetail }) - assert.deepStrictEqual(errorsDetails, errors) + assert.deepStrictEqual( + errorsDetails, + errors, + `${fixturePath}: Expected errors`, + ) }) } }) @@ -165,11 +177,6 @@ test("ensure the rule validates correctly", async (t) => { fixturePath: "test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md", }, - { - name: "should be valid with an existing heading fragment (case insensitive)", - fixturePath: - "test/fixtures/valid/existing-heading-case-insensitive/existing-heading-case-insensitive.md", - }, { name: "should be valid with an existing heading fragment", fixturePath: @@ -232,7 +239,7 @@ test("ensure the rule validates correctly", async (t) => { assert.equal( errorsDetails.length, 0, - `Expected no errors, got ${errorsDetails.join(", ")}`, + `${fixturePath}: Expected no errors, got ${errorsDetails.join(", ")}`, ) }) }