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

Adds support for glob patterns with the exclude option #42

Merged
merged 1 commit into from
May 25, 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
14 changes: 9 additions & 5 deletions docs/src/content/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,26 @@ export default defineConfig({
**Type:** `string[]`
**Default:** `[]`

A list of links that should be excluded from validation.
A list of links or [glob patterns](https://github.com/micromatch/picomatch#globbing-features) that should be excluded from validation.

The links in this list must exactly match links as they appear in Markdown and will be ignored by the plugin.

This option should be used with caution but can be useful to exclude links that are not meant to be validated like redirects only existing in production or links to [custom pages](https://starlight.astro.build/guides/pages/#custom-pages) that are not part of your documentation.
This option should be used with caution but can be useful to exclude links that are not meant to be validated like redirects only existing in production or links to [custom pages](https://starlight.astro.build/guides/pages/#custom-pages) that are automatically generated or not part of your documentation.

```js {6}
export default defineConfig({
integrations: [
starlight({
plugins: [
starlightLinksValidator({
exclude: ['/social/discord', '/social/twitter'],
exclude: ['/social/twitter', '/api/{interface,functions}/**/*'],
}),
],
}),
],
})
```

:::tip

You can use this [webpage](https://www.digitalocean.com/community/tools/glob) to generate and test glob patterns.

:::
3 changes: 1 addition & 2 deletions packages/starlight-links-validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ const starlightLinksValidatorOptionsSchema = z
*/
errorOnRelativeLinks: z.boolean().default(true),
/**
* Defines a list of links that should be excluded from validation.
* Defines a list of links or glob patterns 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 []
*/
Expand Down
5 changes: 3 additions & 2 deletions packages/starlight-links-validator/libs/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url'
import type { StarlightPlugin } from '@astrojs/starlight/types'
import type { AstroConfig, AstroIntegrationLogger } from 'astro'
import { bgGreen, black, blue, dim, green, red } from 'kleur/colors'
import picomatch from 'picomatch'

import type { StarlightLinksValidatorOptions } from '..'

Expand Down Expand Up @@ -216,10 +217,10 @@ function isValidAsset(path: string, context: ValidationContext) {
}

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

function addError(errors: ValidationErrors, filePath: string, link: string, type: ValidationErrorType) {
Expand Down
2 changes: 2 additions & 0 deletions packages/starlight-links-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
"is-absolute-url": "4.0.1",
"kleur": "4.1.5",
"mdast-util-to-string": "4.0.0",
"picomatch": "4.0.2",
"unist-util-visit": "5.0.0"
},
"devDependencies": {
"@astrojs/starlight": "0.15.0",
"@types/hast": "3.0.3",
"@types/mdast": "4.0.3",
"@types/node": "18.17.18",
"@types/picomatch": "2.3.3",
"astro": "4.0.4",
"mdast-util-mdx-jsx": "3.0.0",
"typescript": "5.1.3",
Expand Down
6 changes: 4 additions & 2 deletions packages/starlight-links-validator/tests/exclude.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import { ValidationErrorType } from '../libs/validation'

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

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

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

expectValidationErrors(error, '/', [
['/excluded/', ValidationErrorType.InvalidLink],
['/excluded#test', ValidationErrorType.InvalidLink],
['/test/excluded', ValidationErrorType.InvalidLink],
['/test/excluded/test', ValidationErrorType.InvalidLink],
['/api/getting-started', ValidationErrorType.InvalidLink],
['/api/class/baz', ValidationErrorType.InvalidLink],
])
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import starlightLinksValidator from '../..'
export default defineConfig({
integrations: [
starlight({
plugins: [starlightLinksValidator({ exclude: ['/excluded'] })],
plugins: [
starlightLinksValidator({ exclude: ['/excluded', '/glob/*', '/api/{interface,functions}/**/*', '/tests/**'] }),
],
title: 'Starlight Links Validator Tests - exclude',
}),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ title: Index
- [More non-excluded link](/excluded#test)
- [Another non-excluded link](/test/excluded)
- [And another non-excluded link](/test/excluded/test)

# Glob exclude

- [Glob excluded link](/glob/test/)
- [Another glob excluded link](/api/interface/foo)
- [More glob excluded link](/api/functions/bar)
- [And another glob excluded link](/tests/e2e/thing)
- [Glob non-excluded link](/api/getting-started)
- [Another glob non-excluded link](/api/class/baz)
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading