starlight-links-validator 0.12.2 → 0.12.4
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/libs/remark.ts +3 -0
- package/libs/validation.ts +9 -4
- package/package.json +1 -1
package/libs/remark.ts
CHANGED
|
@@ -26,6 +26,8 @@ export const remarkStarlightLinksValidator: Plugin<[{ base: string; srcDir: URL
|
|
|
26
26
|
srcDir,
|
|
27
27
|
}) {
|
|
28
28
|
return (tree, file) => {
|
|
29
|
+
if (file.data.astro?.frontmatter?.draft) return
|
|
30
|
+
|
|
29
31
|
const slugger = new GitHubSlugger()
|
|
30
32
|
const filePath = normalizeFilePath(base, srcDir, file.history[0])
|
|
31
33
|
const slug = file.data.astro?.frontmatter?.slug
|
|
@@ -193,6 +195,7 @@ declare module 'vfile' {
|
|
|
193
195
|
interface DataMap {
|
|
194
196
|
astro?: {
|
|
195
197
|
frontmatter?: {
|
|
198
|
+
draft?: boolean
|
|
196
199
|
slug?: string
|
|
197
200
|
}
|
|
198
201
|
}
|
package/libs/validation.ts
CHANGED
|
@@ -58,7 +58,7 @@ export function validateLinks(
|
|
|
58
58
|
pages: allPages,
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
if (link.startsWith('#')) {
|
|
61
|
+
if (link.startsWith('#') || link.startsWith('?')) {
|
|
62
62
|
if (options.errorOnInvalidHashes) {
|
|
63
63
|
validateSelfHash(validationContext)
|
|
64
64
|
}
|
|
@@ -125,7 +125,7 @@ function validateLink(context: ValidationContext) {
|
|
|
125
125
|
throw new Error('Failed to validate a link with no path.')
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
if (path.startsWith('.') || !link.startsWith('/')) {
|
|
128
|
+
if (path.startsWith('.') || (!link.startsWith('/') && !link.startsWith('?'))) {
|
|
129
129
|
if (options.errorOnRelativeLinks) {
|
|
130
130
|
addError(errors, filePath, link, ValidationErrorType.RelativeLink)
|
|
131
131
|
}
|
|
@@ -137,7 +137,7 @@ function validateLink(context: ValidationContext) {
|
|
|
137
137
|
return
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
-
const sanitizedPath = ensureTrailingSlash(path)
|
|
140
|
+
const sanitizedPath = ensureTrailingSlash(stripQueryString(path))
|
|
141
141
|
|
|
142
142
|
const isValidPage = pages.has(sanitizedPath)
|
|
143
143
|
const fileHeadings = getFileHeadings(sanitizedPath, context)
|
|
@@ -183,7 +183,8 @@ function getFileHeadings(path: string, { headings, localeConfig, options }: Vali
|
|
|
183
183
|
* Validate a link to an hash in the same page.
|
|
184
184
|
*/
|
|
185
185
|
function validateSelfHash({ errors, link, filePath, headings }: ValidationContext) {
|
|
186
|
-
const
|
|
186
|
+
const hash = link.split('#')[1] ?? link
|
|
187
|
+
const sanitizedHash = hash.replace(/^#/, '')
|
|
187
188
|
const fileHeadings = headings.get(filePath)
|
|
188
189
|
|
|
189
190
|
if (!fileHeadings) {
|
|
@@ -226,6 +227,10 @@ function isExcludedLink(link: string, context: ValidationContext) {
|
|
|
226
227
|
return picomatch(context.options.exclude)(link)
|
|
227
228
|
}
|
|
228
229
|
|
|
230
|
+
function stripQueryString(path: string): string {
|
|
231
|
+
return path.split('?')[0] ?? path
|
|
232
|
+
}
|
|
233
|
+
|
|
229
234
|
function addError(errors: ValidationErrors, filePath: string, link: string, type: ValidationErrorType) {
|
|
230
235
|
const fileErrors = errors.get(filePath) ?? []
|
|
231
236
|
fileErrors.push({ link, type })
|