Skip to content

Commit

Permalink
fix: prevent validation issues when using the trailingSlash option …
Browse files Browse the repository at this point in the history
…set to `never`
  • Loading branch information
HiDeoo committed Dec 12, 2023
1 parent cfc3b17 commit ea4128d
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/starlight-links-validator/libs/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function ensureTrailingSlash(path: string): string {
return path.endsWith('/') ? path : `${path}/`
}
7 changes: 3 additions & 4 deletions packages/starlight-links-validator/libs/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { bgGreen, black, blue, bold, dim, red } from 'kleur/colors'
import type { StarlightLinksValidatorOptions } from '..'

import { getFallbackHeadings, getLocaleConfig, type LocaleConfig } from './i18n'
import { ensureTrailingSlash } from './path'
import { getValidationData, type Headings } from './remark'

export function validateLinks(
Expand All @@ -19,7 +20,7 @@ export function validateLinks(

const localeConfig = getLocaleConfig(starlightConfig)
const { headings, links } = getValidationData()
const allPages: Pages = new Set(pages.map((page) => page.pathname))
const allPages: Pages = new Set(pages.map((page) => ensureTrailingSlash(page.pathname)))

const errors: ValidationErrors = new Map()

Expand Down Expand Up @@ -99,9 +100,7 @@ function validateLink(context: ValidationContext) {
return
}

if (path.length > 0 && !path.endsWith('/')) {
path += '/'
}
path = ensureTrailingSlash(path)

const isValidPage = pages.has(path)
const fileHeadings = getFileHeadings(path, context)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import starlight from '@astrojs/starlight'
import { defineConfig } from 'astro/config'

import starlightLinksValidator from '../..'

export default defineConfig({
integrations: [
starlight({
plugins: [starlightLinksValidator()],
title: 'Starlight Links Validator Tests - trailing always',
}),
],
trailingSlash: 'always',
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Example
---

## Description

This is an example page.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Test
---

# Some links

- [External link](https://starlight.astro.build/)

- [Example page](/guides/example)
- [Example page](/guides/example/)

- [Example page with hash](/guides/example#description)
- [Example page with hash](/guides/example/#description)

- [Unknown page](/unknown)
- [Unknown page](/unknown/)

- [Example page with unknown hash](/guides/example#unknown)
- [Example page with unknown hash](/guides/example/#unknown)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import starlight from '@astrojs/starlight'
import { defineConfig } from 'astro/config'

import starlightLinksValidator from '../..'

export default defineConfig({
integrations: [
starlight({
plugins: [starlightLinksValidator()],
title: 'Starlight Links Validator Tests - trailing never',
}),
],
trailingSlash: 'never',
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Example
---

## Description

This is an example page.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Test
---

# Some links

- [External link](https://starlight.astro.build/)

- [Example page](/guides/example)
- [Example page](/guides/example/)

- [Example page with hash](/guides/example#description)
- [Example page with hash](/guides/example/#description)

- [Unknown page](/unknown)
- [Unknown page](/unknown/)

- [Example page with unknown hash](/guides/example#unknown)
- [Example page with unknown hash](/guides/example/#unknown)
39 changes: 39 additions & 0 deletions packages/starlight-links-validator/tests/trailing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect, test } from 'vitest'

import { loadFixture } from './utils'

test('should validate links when the `trailingSlash` option is set to `never`', async () => {
expect.assertions(2)

try {
await loadFixture('trailing-never')
} catch (error) {
expect(error).toMatch(/Found 4 invalid links in 1 file./)

expect(error).toMatch(
new RegExp(`▶ test/
├─ /unknown
├─ /unknown/
├─ /guides/example#unknown
└─ /guides/example/#unknown`),
)
}
})

test('should validate links when the `trailingSlash` option is set to `always`', async () => {
expect.assertions(2)

try {
await loadFixture('trailing-always')
} catch (error) {
expect(error).toMatch(/Found 4 invalid links in 1 file./)

expect(error).toMatch(
new RegExp(`▶ test/
├─ /unknown
├─ /unknown/
├─ /guides/example#unknown
└─ /guides/example/#unknown`),
)
}
})

0 comments on commit ea4128d

Please sign in to comment.