starlight-links-validator 0.10.1 → 0.12.0
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/README.md +2 -2
- package/index.ts +8 -0
- package/libs/remark.ts +1 -1
- package/libs/validation.ts +10 -6
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -27,8 +27,8 @@ Want to get started immediately? Check out the [getting started guide](https://s
|
|
|
27
27
|
A [Starlight](https://starlight.astro.build) plugin to validate **_internal_** links in Markdown and MDX files.
|
|
28
28
|
|
|
29
29
|
- Validate internal links to other pages
|
|
30
|
-
- Validate internal links to
|
|
31
|
-
- Validate internal links to
|
|
30
|
+
- Validate internal links to hashes in other pages
|
|
31
|
+
- Validate internal links to hashes in the same page
|
|
32
32
|
- Ignore external links
|
|
33
33
|
- Run only during a production build
|
|
34
34
|
|
package/index.ts
CHANGED
|
@@ -33,6 +33,14 @@ const starlightLinksValidatorOptionsSchema = z
|
|
|
33
33
|
* @default true
|
|
34
34
|
*/
|
|
35
35
|
errorOnRelativeLinks: z.boolean().default(true),
|
|
36
|
+
/**
|
|
37
|
+
* Defines whether the plugin should error on invalid hashes.
|
|
38
|
+
*
|
|
39
|
+
* When set to `false`, the plugin will only validate link pages and ignore hashes.
|
|
40
|
+
*
|
|
41
|
+
* @default true
|
|
42
|
+
*/
|
|
43
|
+
errorOnInvalidHashes: z.boolean().default(true),
|
|
36
44
|
/**
|
|
37
45
|
* Defines a list of links or glob patterns that should be excluded from validation.
|
|
38
46
|
*
|
package/libs/remark.ts
CHANGED
package/libs/validation.ts
CHANGED
|
@@ -15,7 +15,7 @@ import { getValidationData, type Headings } from './remark'
|
|
|
15
15
|
|
|
16
16
|
export const ValidationErrorType = {
|
|
17
17
|
InconsistentLocale: 'inconsistent locale',
|
|
18
|
-
|
|
18
|
+
InvalidHash: 'invalid hash',
|
|
19
19
|
InvalidLink: 'invalid link',
|
|
20
20
|
RelativeLink: 'relative link',
|
|
21
21
|
TrailingSlash: 'trailing slash',
|
|
@@ -59,7 +59,9 @@ export function validateLinks(
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
if (link.startsWith('#')) {
|
|
62
|
-
|
|
62
|
+
if (options.errorOnInvalidHashes) {
|
|
63
|
+
validateSelfHash(validationContext)
|
|
64
|
+
}
|
|
63
65
|
} else {
|
|
64
66
|
validateLink(validationContext)
|
|
65
67
|
}
|
|
@@ -151,7 +153,9 @@ function validateLink(context: ValidationContext) {
|
|
|
151
153
|
}
|
|
152
154
|
|
|
153
155
|
if (hash && !fileHeadings.includes(hash)) {
|
|
154
|
-
|
|
156
|
+
if (options.errorOnInvalidHashes) {
|
|
157
|
+
addError(errors, filePath, link, ValidationErrorType.InvalidHash)
|
|
158
|
+
}
|
|
155
159
|
return
|
|
156
160
|
}
|
|
157
161
|
|
|
@@ -176,9 +180,9 @@ function getFileHeadings(path: string, { headings, localeConfig, options }: Vali
|
|
|
176
180
|
}
|
|
177
181
|
|
|
178
182
|
/**
|
|
179
|
-
* Validate a link to an
|
|
183
|
+
* Validate a link to an hash in the same page.
|
|
180
184
|
*/
|
|
181
|
-
function
|
|
185
|
+
function validateSelfHash({ errors, link, filePath, headings }: ValidationContext) {
|
|
182
186
|
const sanitizedHash = link.replace(/^#/, '')
|
|
183
187
|
const fileHeadings = headings.get(filePath)
|
|
184
188
|
|
|
@@ -187,7 +191,7 @@ function validateSelfAnchor({ errors, link, filePath, headings }: ValidationCont
|
|
|
187
191
|
}
|
|
188
192
|
|
|
189
193
|
if (!fileHeadings.includes(sanitizedHash)) {
|
|
190
|
-
addError(errors, filePath, link,
|
|
194
|
+
addError(errors, filePath, link, ValidationErrorType.InvalidHash)
|
|
191
195
|
}
|
|
192
196
|
}
|
|
193
197
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "starlight-links-validator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Starlight plugin to validate internal links.",
|
|
6
6
|
"author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",
|
|
@@ -20,12 +20,12 @@
|
|
|
20
20
|
"unist-util-visit": "5.0.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@astrojs/starlight": "0.
|
|
23
|
+
"@astrojs/starlight": "0.26.1",
|
|
24
24
|
"@types/hast": "3.0.3",
|
|
25
25
|
"@types/mdast": "4.0.3",
|
|
26
26
|
"@types/node": "18.17.18",
|
|
27
27
|
"@types/picomatch": "2.3.3",
|
|
28
|
-
"astro": "4.
|
|
28
|
+
"astro": "4.8.6",
|
|
29
29
|
"mdast-util-mdx-jsx": "3.0.0",
|
|
30
30
|
"remark-custom-heading-id": "2.0.0",
|
|
31
31
|
"typescript": "5.1.3",
|