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

Issue:
Not checking reference-style links
### Describe the bug

If you use [reference style links](https://www.markdownguide.org/basic-syntax/#reference-style-links), this check does not fail when the relative path does not exist.

```markdown

[this will be caught](/link-that-does-not-exist)

[but this will not]

[but this will not]: /link-that-does-not-exist
```

### To Reproduce

I created [this repo](https://github.com/dbanty/reproduce-link-checker-reference) from the starlight starter and replaced the content with a single reference that should be caught and is not.

### Expected behavior

Reference-style relative links should be caught the same way as inline links.

### How often does this bug happen?

Every time

### System Info

_No response_

### Additional Context

_No response_

Pull request:
Validate reference-style links
Closes #14

Patch:
diff --git a/packages/starlight-links-validator/libs/remark.ts b/packages/starlight-links-validator/libs/remark.ts
index 524c9a7..92b2bec 100644
--- a/packages/starlight-links-validator/libs/remark.ts
+++ b/packages/starlight-links-validator/libs/remark.ts
@@ -24,8 +24,13 @@ export const remarkStarlightLinksValidator: Plugin<[], Root> = function () {
 
     const fileHeadings: string[] = []
     const fileLinks: string[] = []
+    const fileDefinitions = new Map<string, string>()
 
-    visit(tree, ['heading', 'html', 'link', 'mdxJsxFlowElement', 'mdxJsxTextElement'], (node) => {
+    visit(tree, 'definition', (node) => {
+      fileDefinitions.set(node.identifier, node.url)
+    })
+
+    visit(tree, ['heading', 'html', 'link', 'linkReference', 'mdxJsxFlowElement', 'mdxJsxTextElement'], (node) => {
       // https://github.com/syntax-tree/mdast#nodes
       // https://github.com/syntax-tree/mdast-util-mdx-jsx#nodes
       switch (node.type) {
@@ -47,6 +52,15 @@ export const remarkStarlightLinksValidator: Plugin<[], Root> = function () {
 
           break
         }
+        case 'linkReference': {
+          const definition = fileDefinitions.get(node.identifier)
+
+          if (definition && isInternalLink(definition)) {
+            fileLinks.push(definition)
+          }
+
+          break
+        }
         case 'mdxJsxFlowElement': {
           for (const attribute of node.attributes) {
             if (isMdxIdAttribute(attribute)) {


Test patch:
diff --git a/packages/starlight-links-validator/tests/fixtures/with-invalid-links/src/content/docs/test.md b/packages/starlight-links-validator/tests/fixtures/with-invalid-links/src/content/docs/test.md
index 932b5f9..b0ef577 100644
--- a/packages/starlight-links-validator/tests/fixtures/with-invalid-links/src/content/docs/test.md
+++ b/packages/starlight-links-validator/tests/fixtures/with-invalid-links/src/content/docs/test.md
@@ -23,6 +23,14 @@ title: Test
 - [Link to invalid asset](/icon.svg)
 - [Link to another invalid asset](/guidelines/ui.pdf)
 
+## Links with references
+
+- [Link reference to unknwon page][ref-unknown-page]
+- [Link reference to invalid anchor][ref-invalid-anchor]
+
+[ref-unknown-page]: /unknown-ref
+[ref-invalid-anchor]: #unknown-ref
+
 <div id="aDiv">
 some content
 
diff --git a/packages/starlight-links-validator/tests/fixtures/with-valid-links/src/content/docs/index.md b/packages/starlight-links-validator/tests/fixtures/with-valid-links/src/content/docs/index.md
index b8bdc35..cffb9f8 100644
--- a/packages/starlight-links-validator/tests/fixtures/with-valid-links/src/content/docs/index.md
+++ b/packages/starlight-links-validator/tests/fixtures/with-valid-links/src/content/docs/index.md
@@ -29,3 +29,14 @@ title: Index
 ## A more `complex` heading
 
 - [Link to more complex anchor](#a-more-complex-heading)
+
+## Links with references
+
+- [ref]
+- [Link reference][ref]
+- [Link reference with anchor in this page][ref-with-anchor-internal]
+- [Link reference with anchor in another page][ref-with-anchor-external]
+
+[ref]: /test
+[ref-with-anchor-internal]: #some-links
+[ref-with-anchor-external]: /test#title
diff --git a/packages/starlight-links-validator/tests/validation.test.ts b/packages/starlight-links-validator/tests/validation.test.ts
index 996fe6c..8b8a8f6 100644
--- a/packages/starlight-links-validator/tests/validation.test.ts
+++ b/packages/starlight-links-validator/tests/validation.test.ts
@@ -16,7 +16,7 @@ test('should not build with invalid links', async () => {
   try {
     await loadFixture('with-invalid-links')
   } catch (error) {
-    expect(error).toMatch(/Found 18 invalid links in 3 files./)
+    expect(error).toMatch(/Found 20 invalid links in 3 files./)
 
     expect(error).toMatch(
       new RegExp(`▶ test/
@@ -29,6 +29,8 @@ test('should not build with invalid links', async () => {
   ├─ /guides/example/#links
   ├─ /icon.svg
   ├─ /guidelines/ui.pdf
+  ├─ /unknown-ref
+  ├─ #unknown-ref
   └─ #anotherDiv`),
     )