Repository: HiDeoo/starlight-links-validator (ts)

Issue:
Validate fails for anchor tags on same page
### Describe the bug

Right now this is only checking for specific self-anchor formats such as `#heading-link`:

https://github.com/HiDeoo/starlight-links-validator/blob/fa5998f8611c41fae3ebd1cde3dd1e5b170f2d1c/packages/starlight-links-validator/src/libs/validation.ts#L15

This doesn't catch when you have some generators that with always include the full file path in the link so that it looks like this: `path/to/page/#heading-link`

Note the trailing slash that's also there, I'm not sure if that's also causing an issue with this bug or not.

### To Reproduce

Have a link like this:

```md
[`EventName`](/reference/namespaceevent/#eventname)
```

Note that we're on the same page that the link is pointing to (so the above link is both on and also referencing content in `/reference/namespaceevent`.

This will fail validation

### Expected behavior

Link validation should not fail.

### How often does this bug happen?

Every time

### System Info

`starlight-links-validator` 0.1.1
`astro` 2.10.7
`@astrojs/starlight` 0.7.2

### Additional Context

This particular scenario was created by using `typedoc-plugin-markdown` which seems to render out links like this. I'm using a custom implementation of that on top of Starlight because we needed to customise the output a bit more, but not sure if this would also affect your typedoc Starlight integration or not.

Pull request description:
Prevent invalid validation issues in files with a path containing special characters
Closes #4 

Patch:
diff --git a/packages/starlight-links-validator/libs/remark.ts b/packages/starlight-links-validator/libs/remark.ts
index 1eacba4..a7dbd30 100644
--- a/packages/starlight-links-validator/libs/remark.ts
+++ b/packages/starlight-links-validator/libs/remark.ts
@@ -130,6 +130,9 @@ function normalizeFilePath(filePath?: string) {
     .replace(/\.\w+$/, '')
     .replace(/index$/, '')
     .replace(/\/?$/, '/')
+    .split('/')
+    .map((segment) => slug(segment))
+    .join('/')
 }
 
 function isMdxIdAttribute(attribute: MdxJsxAttribute | MdxJsxExpressionAttribute): attribute is MdxIdAttribute {


Test patch:
diff --git a/packages/starlight-links-validator/tests/fixtures/with-invalid-links/src/content/docs/guides/Namespace.Test.md b/packages/starlight-links-validator/tests/fixtures/with-invalid-links/src/content/docs/guides/Namespace.Test.md
new file mode 100644
index 0000000..df7a859
--- /dev/null
+++ b/packages/starlight-links-validator/tests/fixtures/with-invalid-links/src/content/docs/guides/Namespace.Test.md
@@ -0,0 +1,8 @@
+---
+title: Namespace Test
+---
+
+## Some content
+
+[A link](#some-other-content)
+[Another link](/guides/namespacetest/#another-content)
diff --git a/packages/starlight-links-validator/tests/fixtures/with-valid-links/src/content/docs/guides/Namespace.Test.md b/packages/starlight-links-validator/tests/fixtures/with-valid-links/src/content/docs/guides/Namespace.Test.md
new file mode 100644
index 0000000..842577a
--- /dev/null
+++ b/packages/starlight-links-validator/tests/fixtures/with-valid-links/src/content/docs/guides/Namespace.Test.md
@@ -0,0 +1,8 @@
+---
+title: Namespace Test
+---
+
+## Some content
+
+[A link](#some-content)
+[Another link](/guides/namespacetest/#some-content)
diff --git a/packages/starlight-links-validator/tests/validation.test.ts b/packages/starlight-links-validator/tests/validation.test.ts
index 1f0d09d..2b28cd2 100644
--- a/packages/starlight-links-validator/tests/validation.test.ts
+++ b/packages/starlight-links-validator/tests/validation.test.ts
@@ -11,12 +11,12 @@ test('should build with valid links', async () => {
 })
 
 test('should not build with invalid links', async () => {
-  expect.assertions(3)
+  expect.assertions(4)
 
   try {
     await loadFixture('with-invalid-links')
   } catch (error) {
-    expect(error).toMatch(/Found 12 invalid links in 2 files./)
+    expect(error).toMatch(/Found 14 invalid links in 3 files./)
 
     expect(error).toMatch(
       new RegExp(`▶ test/
@@ -37,5 +37,11 @@ test('should not build with invalid links', async () => {
   ├─ /unknown
   └─ #anotherBlock`)
     )
+
+    expect(error).toMatch(
+      new RegExp(`▶ guides/namespacetest/
+  ├─ #some-other-content
+  └─ /guides/namespacetest/#another-content`)
+    )
   }
 })