starlight-links-validator 0.5.1 → 0.5.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/index.ts +1 -1
- package/libs/validation.ts +16 -4
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -82,7 +82,7 @@ export default function starlightLinksValidatorPlugin(
|
|
|
82
82
|
function throwPluginError(message: string): never {
|
|
83
83
|
throw new AstroError(
|
|
84
84
|
message,
|
|
85
|
-
`See the error report above for more informations.\n\nIf you believe this is a bug, please file an issue at https://github.com/HiDeoo/starlight-links-validator/issues/new/choose
|
|
85
|
+
`See the error report above for more informations.\n\nIf you believe this is a bug, please file an issue at https://github.com/HiDeoo/starlight-links-validator/issues/new/choose`,
|
|
86
86
|
)
|
|
87
87
|
}
|
|
88
88
|
|
package/libs/validation.ts
CHANGED
|
@@ -41,6 +41,7 @@ export function validateLinks(
|
|
|
41
41
|
for (const [filePath, fileLinks] of links) {
|
|
42
42
|
for (const link of fileLinks) {
|
|
43
43
|
const validationContext: ValidationContext = {
|
|
44
|
+
base,
|
|
44
45
|
errors,
|
|
45
46
|
filePath,
|
|
46
47
|
headings,
|
|
@@ -100,7 +101,7 @@ export function logErrors(pluginLogger: AstroIntegrationLogger, errors: Validati
|
|
|
100
101
|
* Validate a link to another internal page that may or may not have a hash.
|
|
101
102
|
*/
|
|
102
103
|
function validateLink(context: ValidationContext) {
|
|
103
|
-
const { errors, filePath, link, localeConfig, options,
|
|
104
|
+
const { errors, filePath, link, localeConfig, options, pages } = context
|
|
104
105
|
|
|
105
106
|
const sanitizedLink = link.replace(/^\//, '')
|
|
106
107
|
const segments = sanitizedLink.split('#')
|
|
@@ -120,7 +121,7 @@ function validateLink(context: ValidationContext) {
|
|
|
120
121
|
return
|
|
121
122
|
}
|
|
122
123
|
|
|
123
|
-
if (isValidAsset(path,
|
|
124
|
+
if (isValidAsset(path, context)) {
|
|
124
125
|
return
|
|
125
126
|
}
|
|
126
127
|
|
|
@@ -173,8 +174,18 @@ function validateSelfAnchor({ errors, link, filePath, headings }: ValidationCont
|
|
|
173
174
|
/**
|
|
174
175
|
* Check if a link is a valid asset in the build output directory.
|
|
175
176
|
*/
|
|
176
|
-
function isValidAsset(path: string,
|
|
177
|
-
|
|
177
|
+
function isValidAsset(path: string, context: ValidationContext) {
|
|
178
|
+
if (context.base !== '/') {
|
|
179
|
+
const base = stripLeadingSlash(context.base)
|
|
180
|
+
|
|
181
|
+
if (path.startsWith(base)) {
|
|
182
|
+
path = path.replace(new RegExp(`^${stripLeadingSlash(base)}/?`), '')
|
|
183
|
+
} else {
|
|
184
|
+
return false
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const filePath = fileURLToPath(new URL(path, context.outputDir))
|
|
178
189
|
|
|
179
190
|
try {
|
|
180
191
|
const stats = statSync(filePath)
|
|
@@ -213,6 +224,7 @@ interface PageData {
|
|
|
213
224
|
type Pages = Set<PageData['pathname']>
|
|
214
225
|
|
|
215
226
|
interface ValidationContext {
|
|
227
|
+
base: string
|
|
216
228
|
errors: ValidationErrors
|
|
217
229
|
filePath: string
|
|
218
230
|
headings: Headings
|