starlight-links-validator 0.2.0 → 0.3.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/index.ts +2 -2
- package/libs/validation.ts +35 -4
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -18,8 +18,8 @@ export default function starlightLinksValidatorIntegration(): AstroIntegration {
|
|
|
18
18
|
},
|
|
19
19
|
})
|
|
20
20
|
},
|
|
21
|
-
'astro:build:done': ({ pages }) => {
|
|
22
|
-
const errors = validateLinks(pages)
|
|
21
|
+
'astro:build:done': ({ dir, pages }) => {
|
|
22
|
+
const errors = validateLinks(pages, dir)
|
|
23
23
|
|
|
24
24
|
logErrors(errors)
|
|
25
25
|
|
package/libs/validation.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { statSync } from 'node:fs'
|
|
2
|
+
import { fileURLToPath } from 'node:url'
|
|
3
|
+
|
|
1
4
|
import { bgGreen, black, bold, cyan, dim, red } from 'kleur/colors'
|
|
2
5
|
|
|
3
6
|
import { getValidationData, type Headings } from './remark'
|
|
4
7
|
|
|
5
|
-
export function validateLinks(pages: PageData[]): ValidationErrors {
|
|
8
|
+
export function validateLinks(pages: PageData[], outputDir: URL): ValidationErrors {
|
|
6
9
|
process.stdout.write(`\n${bgGreen(black(` validating links `))}\n`)
|
|
7
10
|
|
|
8
11
|
const { headings, links } = getValidationData()
|
|
@@ -15,7 +18,7 @@ export function validateLinks(pages: PageData[]): ValidationErrors {
|
|
|
15
18
|
if (link.startsWith('#')) {
|
|
16
19
|
validateSelfAnchor(errors, link, filePath, headings)
|
|
17
20
|
} else {
|
|
18
|
-
validateLink(errors, link, filePath, headings, allPages)
|
|
21
|
+
validateLink(errors, link, filePath, headings, allPages, outputDir)
|
|
19
22
|
}
|
|
20
23
|
}
|
|
21
24
|
}
|
|
@@ -58,7 +61,14 @@ export function logErrors(errors: ValidationErrors) {
|
|
|
58
61
|
/**
|
|
59
62
|
* Validate a link to another internal page that may or may not have a hash.
|
|
60
63
|
*/
|
|
61
|
-
function validateLink(
|
|
64
|
+
function validateLink(
|
|
65
|
+
errors: ValidationErrors,
|
|
66
|
+
link: string,
|
|
67
|
+
filePath: string,
|
|
68
|
+
headings: Headings,
|
|
69
|
+
pages: Pages,
|
|
70
|
+
outputDir: URL
|
|
71
|
+
) {
|
|
62
72
|
const sanitizedLink = link.replace(/^\//, '')
|
|
63
73
|
const segments = sanitizedLink.split('#')
|
|
64
74
|
|
|
@@ -67,7 +77,13 @@ function validateLink(errors: ValidationErrors, link: string, filePath: string,
|
|
|
67
77
|
|
|
68
78
|
if (path === undefined) {
|
|
69
79
|
throw new Error('Failed to validate a link with no path.')
|
|
70
|
-
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (isValidAsset(path, outputDir)) {
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (path.length > 0 && !path.endsWith('/')) {
|
|
71
87
|
path += '/'
|
|
72
88
|
}
|
|
73
89
|
|
|
@@ -100,6 +116,21 @@ function validateSelfAnchor(errors: ValidationErrors, hash: string, filePath: st
|
|
|
100
116
|
}
|
|
101
117
|
}
|
|
102
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Check if a link is a valid asset in the build output directory.
|
|
121
|
+
*/
|
|
122
|
+
function isValidAsset(path: string, outputDir: URL) {
|
|
123
|
+
const filePath = fileURLToPath(new URL(path, outputDir))
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
const stats = statSync(filePath)
|
|
127
|
+
|
|
128
|
+
return stats.isFile()
|
|
129
|
+
} catch {
|
|
130
|
+
return false
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
103
134
|
function addError(errors: ValidationErrors, filePath: string, link: string) {
|
|
104
135
|
const fileErrors = errors.get(filePath) ?? []
|
|
105
136
|
fileErrors.push(link)
|
package/package.json
CHANGED