starlight-links-validator 0.5.1 → 0.5.3
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/remark.ts +2 -1
- package/libs/validation.ts +17 -5
- package/package.json +2 -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/remark.ts
CHANGED
|
@@ -6,6 +6,7 @@ import GitHubSlugger, { slug } from 'github-slugger'
|
|
|
6
6
|
import type { Nodes } from 'hast'
|
|
7
7
|
import { fromHtml } from 'hast-util-from-html'
|
|
8
8
|
import { hasProperty } from 'hast-util-has-property'
|
|
9
|
+
import isAbsoluteUrl from 'is-absolute-url'
|
|
9
10
|
import type { Root } from 'mdast'
|
|
10
11
|
import type { MdxJsxAttribute, MdxJsxExpressionAttribute } from 'mdast-util-mdx-jsx'
|
|
11
12
|
import { toString } from 'mdast-util-to-string'
|
|
@@ -133,7 +134,7 @@ export function getValidationData() {
|
|
|
133
134
|
}
|
|
134
135
|
|
|
135
136
|
function isInternalLink(link: string) {
|
|
136
|
-
return
|
|
137
|
+
return !isAbsoluteUrl(link)
|
|
137
138
|
}
|
|
138
139
|
|
|
139
140
|
function normalizeFilePath(base: string, filePath?: string) {
|
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('#')
|
|
@@ -112,7 +113,7 @@ function validateLink(context: ValidationContext) {
|
|
|
112
113
|
throw new Error('Failed to validate a link with no path.')
|
|
113
114
|
}
|
|
114
115
|
|
|
115
|
-
if (path.startsWith('.')) {
|
|
116
|
+
if (path.startsWith('.') || !link.startsWith('/')) {
|
|
116
117
|
if (options.errorOnRelativeLinks) {
|
|
117
118
|
addError(errors, filePath, link, ValidationErrorType.RelativeLink)
|
|
118
119
|
}
|
|
@@ -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
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "starlight-links-validator",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Starlight plugin to validate internal links.",
|
|
6
6
|
"author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"github-slugger": "2.0.0",
|
|
14
14
|
"hast-util-from-html": "2.0.1",
|
|
15
15
|
"hast-util-has-property": "3.0.0",
|
|
16
|
+
"is-absolute-url": "4.0.1",
|
|
16
17
|
"kleur": "4.1.5",
|
|
17
18
|
"mdast-util-to-string": "4.0.0",
|
|
18
19
|
"unist-util-visit": "5.0.0"
|