Skip to content

Commit

Permalink
fix: validate reference-style links
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo committed Nov 6, 2023
1 parent ba1be29 commit 9a9f2fb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
16 changes: 15 additions & 1 deletion packages/starlight-links-validator/libs/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion packages/starlight-links-validator/tests/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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`),
)

Expand Down

0 comments on commit 9a9f2fb

Please sign in to comment.