starlight-links-validator 0.17.0 → 0.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # starlight-links-validator
2
2
 
3
+ ## 0.17.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#115](https://github.com/HiDeoo/starlight-links-validator/pull/115) [`b042c61`](https://github.com/HiDeoo/starlight-links-validator/commit/b042c61d479ad6584a4065bf84dadb6f3078145a) Thanks [@trueberryless](https://github.com/trueberryless)! - Fixes validation issue for links to Starlight page's title anchor, e.g. `/getting-started/#_top`.
8
+
9
+ ## 0.17.1
10
+
11
+ ### Patch Changes
12
+
13
+ - [#113](https://github.com/HiDeoo/starlight-links-validator/pull/113) [`3e0a88c`](https://github.com/HiDeoo/starlight-links-validator/commit/3e0a88cd2f7f6f84c57248ae72a8e8df32c22dbe) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Prevents plugin remark plugin from running on Markdown and MDX content when using the Astro [`renderMarkdown()`](https://docs.astro.build/en/reference/content-loader-reference/#rendermarkdown) content loader API.
14
+
3
15
  ## 0.17.0
4
16
 
5
17
  ### Minor Changes
package/index.ts CHANGED
@@ -1,119 +1,19 @@
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>
package/libs/config.ts ADDED
@@ -0,0 +1,106 @@
1
+ import { z } from 'astro/zod'
2
+
3
+ export const StarlightLinksValidatorOptionsSchema = z
4
+ .object({
5
+ /**
6
+ * Defines a list of additional components and their props that should be validated as links.
7
+ *
8
+ * By default, the plugin will only validate links defined in the `href` prop of the `<LinkButton>` and `<LinkCard>`
9
+ * built-in Starlight components.
10
+ * Adding custom components to this list will allow the plugin to validate links in those components as well.
11
+ *
12
+ * @default []
13
+ */
14
+ components: z.tuple([z.string(), z.string()]).array().default([]),
15
+ /**
16
+ * Defines whether the plugin should error on fallback pages.
17
+ *
18
+ * If you do not expect to have all pages translated in all configured locales and want to use the fallback pages
19
+ * feature built-in into Starlight, you should set this option to `false`.
20
+ *
21
+ * @default true
22
+ * @see https://starlight.astro.build/guides/i18n/#fallback-content
23
+ */
24
+ errorOnFallbackPages: z.boolean().default(true),
25
+ /**
26
+ * Defines whether the plugin should error on inconsistent locale links.
27
+ *
28
+ * When set to `true`, the plugin will error on links that are pointing to a page in a different locale.
29
+ *
30
+ * @default false
31
+ */
32
+ errorOnInconsistentLocale: z.boolean().default(false),
33
+ /**
34
+ * Defines whether the plugin should error on internal relative links.
35
+ *
36
+ * When set to `false`, the plugin will ignore relative links (e.g. `./foo` or `../bar`).
37
+ *
38
+ * @default true
39
+ */
40
+ errorOnRelativeLinks: z.boolean().default(true),
41
+ /**
42
+ * Defines whether the plugin should error on invalid hashes.
43
+ *
44
+ * When set to `false`, the plugin will only validate link pages and ignore hashes.
45
+ *
46
+ * @default true
47
+ */
48
+ errorOnInvalidHashes: z.boolean().default(true),
49
+ /**
50
+ * Defines whether the plugin should error on local links, e.g. URLs with a hostname of `localhost` or `127.0.0.1`.
51
+ *
52
+ * @default true
53
+ */
54
+ errorOnLocalLinks: z.boolean().default(true),
55
+ /**
56
+ * Defines a list of links or glob patterns that should be excluded from validation or a function that will be
57
+ * called for each link to determine if it should be excluded from validation or not.
58
+ *
59
+ * The links in this list or links where the function returns `true` will be ignored by the plugin and will not be
60
+ * validated.
61
+ *
62
+ * @default []
63
+ */
64
+ exclude: z
65
+ .union([
66
+ z.array(z.string()),
67
+ z
68
+ .function()
69
+ .args(
70
+ z.object({
71
+ /**
72
+ * The absolute path to the file where the link is defined.
73
+ */
74
+ file: z.string(),
75
+ /**
76
+ * The link to validate as authored in the content.
77
+ */
78
+ link: z.string(),
79
+ /**
80
+ * The slug of the page where the link is defined.
81
+ */
82
+ slug: z.string(),
83
+ }),
84
+ )
85
+ .returns(z.boolean()),
86
+ ])
87
+ .default([]),
88
+ /**
89
+ * Defines the policy for external links with an origin matching the Astro `site` option.
90
+ *
91
+ * By default, all external links are ignored and not validated by the plugin.
92
+ * Setting this option to `error` will make the plugin error on external links with an origin matching the Astro
93
+ * `site` option and hint that the link can be rewritten without the origin.
94
+ * Setting this option to `validate` will make the plugin validate external links with an origin matching the Astro
95
+ * `site` option as if they were internal links.
96
+ *
97
+ * @default 'ignore'
98
+ * @see https://docs.astro.build/en/reference/configuration-reference/#site
99
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/URL/origin
100
+ */
101
+ sameSitePolicy: z.enum(['error', 'ignore', 'validate']).default('ignore'),
102
+ })
103
+ .default({})
104
+
105
+ export type StarlightLinksValidatorUserOptions = z.input<typeof StarlightLinksValidatorOptionsSchema>
106
+ export type StarlightLinksValidatorOptions = z.output<typeof StarlightLinksValidatorOptionsSchema>
package/libs/remark.ts CHANGED
@@ -36,6 +36,9 @@ export const remarkStarlightLinksValidator: Plugin<[RemarkStarlightLinksValidato
36
36
  )
37
37
 
38
38
  return (tree, file) => {
39
+ // If the content does not have a path, e.g. when rendered using the content loader `renderMarkdown()` API, skip it.
40
+ if (!file.path) return
41
+
39
42
  if (file.data.astro?.frontmatter?.['draft']) return
40
43
 
41
44
  const originalPath = file.history[0]
@@ -46,7 +49,7 @@ export const remarkStarlightLinksValidator: Plugin<[RemarkStarlightLinksValidato
46
49
  const slug: string | undefined =
47
50
  typeof file.data.astro?.frontmatter?.['slug'] === 'string' ? file.data.astro.frontmatter['slug'] : undefined
48
51
 
49
- const fileHeadings: string[] = []
52
+ const fileHeadings: string[] = ['_top']
50
53
  const fileLinks: Link[] = []
51
54
  const fileDefinitions = new Map<string, string>()
52
55
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-links-validator",
3
- "version": "0.17.0",
3
+ "version": "0.17.2",
4
4
  "license": "MIT",
5
5
  "description": "Starlight plugin to validate internal links.",
6
6
  "author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",
@@ -26,6 +26,8 @@
26
26
  "@types/mdast": "^4.0.4",
27
27
  "@types/node": "^18.19.68",
28
28
  "remark-custom-heading-id": "^2.0.0",
29
+ "remark-parse": "^11.0.0",
30
+ "remark-stringify": "^11.0.0",
29
31
  "unified": "^11.0.5",
30
32
  "vfile": "^6.0.3",
31
33
  "vitest": "2.1.6"