Skip to content

Commit

Permalink
feat: stricter validation of heading fragments by being Case sensitive
Browse files Browse the repository at this point in the history
Fixes #8

BREAKING CHANGE: Heading fragments is now Case sensitive.
For example "#ExistIng-Heading" is invalid as it should be "#existing-heading".
  • Loading branch information
theoludwig committed May 27, 2024
1 parent 450cdb8 commit 85f4653
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -157,6 +157,8 @@ const customRule = {
})
continue
}

continue
}

onError({
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const convertHeadingToHTMLFragment = (inlineText) => {
"",
)
.replace(/ /gu, "-"),
).toLowerCase()
)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Valid

[Link fragment](./awesome.md#L7)
[Link fragment](./awesome.md#l7)
21 changes: 14 additions & 7 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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`,
)
})
}
})
Expand All @@ -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:
Expand Down Expand Up @@ -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(", ")}`,
)
})
}
Expand Down

0 comments on commit 85f4653

Please sign in to comment.