Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new option to exclude a list of links from validation #38

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/src/content/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,28 @@ export default defineConfig({
],
})
```

### `exclude`

**Type:** `string[]`
**Default:** `[]`

A list of links that should be excluded from validation.

The links in this list will be ignored by the plugin and will not be validated.
As this option can lead to broken links in your documentation or a downgraded user/author experience, it should be used with caution.
The list must exactly match links as they appear in Markdown.

```js {6}
export default defineConfig({
integrations: [
starlight({
plugins: [
starlightLinksValidator({
exclude: ['/social/discord', '/social/twitter'],
}),
],
}),
],
})
```
9 changes: 9 additions & 0 deletions packages/starlight-links-validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ const starlightLinksValidatorOptionsSchema = z
* @default true
*/
errorOnRelativeLinks: z.boolean().default(true),
/**
* Defines a list of links that should be excluded from validation.
*
* The links in this list will be ignored by the plugin and will not be validated.
* The list must exactly match links as they appear in Markdown.
*
* @default []
*/
exclude: z.array(z.string()).default([]),
})
.default({})

Expand Down
11 changes: 11 additions & 0 deletions packages/starlight-links-validator/libs/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export function logErrors(pluginLogger: AstroIntegrationLogger, errors: Validati
function validateLink(context: ValidationContext) {
const { astroConfig, errors, filePath, link, localeConfig, options, pages } = context

if (isExcludedLink(link, context)) {
return
}

const sanitizedLink = link.replace(/^\//, '')
const segments = sanitizedLink.split('#')

Expand Down Expand Up @@ -211,6 +215,13 @@ function isValidAsset(path: string, context: ValidationContext) {
}
}

/**
* Check if a link is explicitly excluded from validation by the user.
*/
function isExcludedLink(link: string, context: ValidationContext) {
return context.options.exclude.includes(link)
}

function addError(errors: ValidationErrors, filePath: string, link: string, type: ValidationErrorType) {
const fileErrors = errors.get(filePath) ?? []
fileErrors.push({ link, type })
Expand Down
22 changes: 22 additions & 0 deletions packages/starlight-links-validator/tests/exclude.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect, test } from 'vitest'

import { ValidationErrorType } from '../libs/validation'

import { expectValidationErrorCount, expectValidationErrors, loadFixture } from './utils'

test('should ignore links that are explicitly excluded from validation', async () => {
expect.assertions(2)

try {
await loadFixture('exclude')
} catch (error) {
expectValidationErrorCount(error, 4, 1)

expectValidationErrors(error, '/', [
['/excluded/', ValidationErrorType.InvalidLink],
['/excluded#test', ValidationErrorType.InvalidLink],
['/test/excluded', ValidationErrorType.InvalidLink],
['/test/excluded/test', ValidationErrorType.InvalidLink],
])
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import starlight from '@astrojs/starlight'
import { defineConfig } from 'astro/config'

import starlightLinksValidator from '../..'

export default defineConfig({
integrations: [
starlight({
plugins: [starlightLinksValidator({ exclude: ['/excluded'] })],
title: 'Starlight Links Validator Tests - exclude',
}),
],
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Index
---

# Some links

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

- [Home page](/)

- [Test page](/test)

# Exclude

- [Excluded link](/excluded)

- [Non-excluded link](/excluded/)
- [More non-excluded link](/excluded#test)
- [Another non-excluded link](/test/excluded)
- [And another non-excluded link](/test/excluded/test)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Test
---

## Title

Some content.
Loading