|
1 | 1 | import type { StarlightPlugin } from '@astrojs/starlight/types' |
2 | 2 | import type { IntegrationResolvedRoute } from 'astro' |
3 | 3 | import { AstroError } from 'astro/errors' |
4 | | -import { z } from 'astro/zod' |
5 | 4 |
|
6 | 5 | import { clearContentLayerCache } from './libs/astro' |
| 6 | +import { StarlightLinksValidatorOptionsSchema, type StarlightLinksValidatorUserOptions } from './libs/config' |
7 | 7 | import { pathnameToSlug, stripTrailingSlash } from './libs/path' |
8 | 8 | import { remarkStarlightLinksValidator, type RemarkStarlightLinksValidatorConfig } from './libs/remark' |
9 | 9 | import { logErrors, validateLinks } from './libs/validation' |
10 | 10 |
|
11 | | -const starlightLinksValidatorOptionsSchema = z |
12 | | - .object({ |
13 | | - /** |
14 | | - * Defines a list of additional components and their props that should be validated as links. |
15 | | - * |
16 | | - * By default, the plugin will only validate links defined in the `href` prop of the `<LinkButton>` and `<LinkCard>` |
17 | | - * built-in Starlight components. |
18 | | - * Adding custom components to this list will allow the plugin to validate links in those components as well. |
19 | | - * |
20 | | - * @default [] |
21 | | - */ |
22 | | - components: z.tuple([z.string(), z.string()]).array().default([]), |
23 | | - /** |
24 | | - * Defines whether the plugin should error on fallback pages. |
25 | | - * |
26 | | - * If you do not expect to have all pages translated in all configured locales and want to use the fallback pages |
27 | | - * feature built-in into Starlight, you should set this option to `false`. |
28 | | - * |
29 | | - * @default true |
30 | | - * @see https://starlight.astro.build/guides/i18n/#fallback-content |
31 | | - */ |
32 | | - errorOnFallbackPages: z.boolean().default(true), |
33 | | - /** |
34 | | - * Defines whether the plugin should error on inconsistent locale links. |
35 | | - * |
36 | | - * When set to `true`, the plugin will error on links that are pointing to a page in a different locale. |
37 | | - * |
38 | | - * @default false |
39 | | - */ |
40 | | - errorOnInconsistentLocale: z.boolean().default(false), |
41 | | - /** |
42 | | - * Defines whether the plugin should error on internal relative links. |
43 | | - * |
44 | | - * When set to `false`, the plugin will ignore relative links (e.g. `./foo` or `../bar`). |
45 | | - * |
46 | | - * @default true |
47 | | - */ |
48 | | - errorOnRelativeLinks: z.boolean().default(true), |
49 | | - /** |
50 | | - * Defines whether the plugin should error on invalid hashes. |
51 | | - * |
52 | | - * When set to `false`, the plugin will only validate link pages and ignore hashes. |
53 | | - * |
54 | | - * @default true |
55 | | - */ |
56 | | - errorOnInvalidHashes: z.boolean().default(true), |
57 | | - /** |
58 | | - * Defines whether the plugin should error on local links, e.g. URLs with a hostname of `localhost` or `127.0.0.1`. |
59 | | - * |
60 | | - * @default true |
61 | | - */ |
62 | | - errorOnLocalLinks: z.boolean().default(true), |
63 | | - /** |
64 | | - * Defines a list of links or glob patterns that should be excluded from validation or a function that will be |
65 | | - * called for each link to determine if it should be excluded from validation or not. |
66 | | - * |
67 | | - * The links in this list or links where the function returns `true` will be ignored by the plugin and will not be |
68 | | - * validated. |
69 | | - * |
70 | | - * @default [] |
71 | | - */ |
72 | | - exclude: z |
73 | | - .union([ |
74 | | - z.array(z.string()), |
75 | | - z |
76 | | - .function() |
77 | | - .args( |
78 | | - z.object({ |
79 | | - /** |
80 | | - * The absolute path to the file where the link is defined. |
81 | | - */ |
82 | | - file: z.string(), |
83 | | - /** |
84 | | - * The link to validate as authored in the content. |
85 | | - */ |
86 | | - link: z.string(), |
87 | | - /** |
88 | | - * The slug of the page where the link is defined. |
89 | | - */ |
90 | | - slug: z.string(), |
91 | | - }), |
92 | | - ) |
93 | | - .returns(z.boolean()), |
94 | | - ]) |
95 | | - .default([]), |
96 | | - /** |
97 | | - * Defines the policy for external links with an origin matching the Astro `site` option. |
98 | | - * |
99 | | - * By default, all external links are ignored and not validated by the plugin. |
100 | | - * Setting this option to `error` will make the plugin error on external links with an origin matching the Astro |
101 | | - * `site` option and hint that the link can be rewritten without the origin. |
102 | | - * Setting this option to `validate` will make the plugin validate external links with an origin matching the Astro |
103 | | - * `site` option as if they were internal links. |
104 | | - * |
105 | | - * @default 'ignore' |
106 | | - * @see https://docs.astro.build/en/reference/configuration-reference/#site |
107 | | - * @see https://developer.mozilla.org/en-US/docs/Web/API/URL/origin |
108 | | - */ |
109 | | - sameSitePolicy: z.enum(['error', 'ignore', 'validate']).default('ignore'), |
110 | | - }) |
111 | | - .default({}) |
| 11 | +export type { StarlightLinksValidatorOptions } from './libs/config' |
112 | 12 |
|
113 | 13 | export default function starlightLinksValidatorPlugin( |
114 | 14 | userOptions?: StarlightLinksValidatorUserOptions, |
115 | 15 | ): StarlightPlugin { |
116 | | - const options = starlightLinksValidatorOptionsSchema.safeParse(userOptions) |
| 16 | + const options = StarlightLinksValidatorOptionsSchema.safeParse(userOptions) |
117 | 17 |
|
118 | 18 | if (!options.success) { |
119 | 19 | throwPluginError('Invalid options passed to the starlight-links-validator plugin.') |
@@ -195,6 +95,3 @@ function throwPluginError(message: string, additionalHint?: string): never { |
195 | 95 |
|
196 | 96 | throw new AstroError(message, hint) |
197 | 97 | } |
198 | | - |
199 | | -type StarlightLinksValidatorUserOptions = z.input<typeof starlightLinksValidatorOptionsSchema> |
200 | | -export type StarlightLinksValidatorOptions = z.output<typeof starlightLinksValidatorOptionsSchema> |
0 commit comments