Repository: HiDeoo/starlight-links-validator
Pull request #80 (2024-12-19 16:49:55)

Only trim trailing index from links if it is the full segment
Previously the replacement `replace(/index$/, '')` was called when normalizing file paths which would result in links like `/packages/spec/module_index` to be listed as invalid because it would get normalized to `/packages/spec/module_`.

This PR fixes that by only replacing `index` at the end of a file path if it is the full segment.

changeset-bot[bot]: ###  ⚠️  No Changeset found

Latest commit: 5d032249b3d936f17b077e09136c1db38759684b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.**

<details><summary>This PR includes no changesets</summary>

  When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

</details>

[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add a changeset to this PR](https://github.com/lukekarrys/starlight-links-validator/new/lk/fix-links-that-end-with-index?filename=.changeset/fair-ghosts-enjoy.md&value=---%0A%22starlight-links-validator%22%3A%20patch%0A%22%40starlight-links-validator-tests%2Fvalid-links%22%3A%20patch%0A---%0A%0Afix%3A%20dont%20strip%20trailing%20index%20from%20links%20unless%20it%20is%20the%20full%20segment%0A)


vercel[bot]: [vc]: #Dr9x63voN698JNYO6dlosv3BHASew7dzcJcZghQPl4A=:eyJpc01vbm9yZXBvIjp0cnVlLCJ0eXBlIjoiZ2l0aHViIiwicHJvamVjdHMiOlt7Im5hbWUiOiJzdGFybGlnaHQtbGlua3MtdmFsaWRhdG9yIiwicm9vdERpcmVjdG9yeSI6ImRvY3MiLCJpbnNwZWN0b3JVcmwiOiJodHRwczovL3ZlcmNlbC5jb20vaGlkZW9vcy1wcm9qZWN0cy9zdGFybGlnaHQtbGlua3MtdmFsaWRhdG9yLzVCV3VQNEV5MXhqS1p2V25vU1VES291UlZBSFIiLCJwcmV2aWV3VXJsIjoic3RhcmxpZ2h0LWxpbmtzLXZhbGlkYXRvci1naXQtZm9yay1sdWtlLTdhYzNiYi1oaWRlb29zLXByb2plY3RzLnZlcmNlbC5hcHAiLCJuZXh0Q29tbWl0U3RhdHVzIjoiUEVORElORyIsImxpdmVGZWVkYmFjayI6eyJyZXNvbHZlZCI6MCwidW5yZXNvbHZlZCI6MCwidG90YWwiOjAsImxpbmsiOiJzdGFybGlnaHQtbGlua3MtdmFsaWRhdG9yLWdpdC1mb3JrLWx1a2UtN2FjM2JiLWhpZGVvb3MtcHJvamVjdHMudmVyY2VsLmFwcCJ9fV19
**The latest updates on your projects**. Learn more about [Vercel for Git ↗︎](https://vercel.link/github-learn-more)

| Name | Status | Preview | Comments | Updated (UTC) |
| :--- | :----- | :------ | :------- | :------ |
| **starlight-links-validator** | 🔄 Building ([Inspect](https://vercel.com/hideoos-projects/starlight-links-validator/5BWuP4Ey1xjKZvWnoSUDKouRVAHR)) | [Visit Preview](https://starlight-links-validator-git-fork-luke-7ac3bb-hideoos-projects.vercel.app) | 💬 [**Add feedback**](https://vercel.live/open-feedback/starlight-links-validator-git-fork-luke-7ac3bb-hideoos-projects.vercel.app?via=pr-comment-feedback-link) | Dec 19, 2024 4:50pm |



Patch:
diff --git a/.changeset/wicked-trains-call.md b/.changeset/wicked-trains-call.md
new file mode 100644
index 0000000..fb3ddb3
--- /dev/null
+++ b/.changeset/wicked-trains-call.md
@@ -0,0 +1,5 @@
+---
+"starlight-links-validator": patch
+---
+
+Fixes validation issues for pages ending in `index`, e.g. `module_index`.
diff --git a/packages/starlight-links-validator/libs/remark.ts b/packages/starlight-links-validator/libs/remark.ts
index 40a8ab4..2c85f7b 100644
--- a/packages/starlight-links-validator/libs/remark.ts
+++ b/packages/starlight-links-validator/libs/remark.ts
@@ -178,7 +178,7 @@ function normalizeFilePath(base: string, srcDir: URL, filePath?: string) {
   const path = nodePath
     .relative(nodePath.join(fileURLToPath(srcDir), 'content/docs'), filePath)
     .replace(/\.\w+$/, '')
-    .replace(/index$/, '')
+    .replace(/(^|[/\\])index$/, '')
     .replace(/[/\\]?$/, '/')
     .split(/[/\\]/)
     .map((segment) => slug(segment))


Test patch:
diff --git a/packages/starlight-links-validator/tests/fixtures/valid-links/src/content/docs/a-page-that-ends-with-index.md b/packages/starlight-links-validator/tests/fixtures/valid-links/src/content/docs/a-page-that-ends-with-index.md
new file mode 100644
index 0000000..101bdac
--- /dev/null
+++ b/packages/starlight-links-validator/tests/fixtures/valid-links/src/content/docs/a-page-that-ends-with-index.md
@@ -0,0 +1,5 @@
+---
+title: A page that ends with index
+---
+
+Content
diff --git a/packages/starlight-links-validator/tests/fixtures/valid-links/src/content/docs/index.md b/packages/starlight-links-validator/tests/fixtures/valid-links/src/content/docs/index.md
index e9b7dc8..7e51eb0 100644
--- a/packages/starlight-links-validator/tests/fixtures/valid-links/src/content/docs/index.md
+++ b/packages/starlight-links-validator/tests/fixtures/valid-links/src/content/docs/index.md
@@ -19,6 +19,8 @@ title: Index
 - [An MDX nested page](/guides/example)
 - [An MDX nested page](/guides/example/)
 
+- [A page that ends with index](/a-page-that-ends-with-index)
+
 # More links
 
 - [Link to hash in this page](#some-links)