starlight-links-validator 0.5.2 → 0.6.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/libs/remark.ts +20 -4
- package/libs/validation.ts +4 -2
- package/package.json +3 -1
package/libs/remark.ts
CHANGED
|
@@ -6,13 +6,14 @@ 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'
|
|
12
13
|
import type { Plugin } from 'unified'
|
|
13
14
|
import { visit } from 'unist-util-visit'
|
|
14
15
|
|
|
15
|
-
import { stripLeadingSlash } from './path'
|
|
16
|
+
import { ensureTrailingSlash, stripLeadingSlash } from './path'
|
|
16
17
|
|
|
17
18
|
// All the headings keyed by file path.
|
|
18
19
|
const headings: Headings = new Map()
|
|
@@ -23,6 +24,7 @@ export const remarkStarlightLinksValidator: Plugin<[base: string], Root> = funct
|
|
|
23
24
|
return (tree, file) => {
|
|
24
25
|
const slugger = new GitHubSlugger()
|
|
25
26
|
const filePath = normalizeFilePath(base, file.history[0])
|
|
27
|
+
const slug = file.data.astro?.frontmatter?.slug
|
|
26
28
|
|
|
27
29
|
const fileHeadings: string[] = []
|
|
28
30
|
const fileLinks: string[] = []
|
|
@@ -123,8 +125,8 @@ export const remarkStarlightLinksValidator: Plugin<[base: string], Root> = funct
|
|
|
123
125
|
}
|
|
124
126
|
})
|
|
125
127
|
|
|
126
|
-
headings.set(filePath, fileHeadings)
|
|
127
|
-
links.set(filePath, fileLinks)
|
|
128
|
+
headings.set(getFilePath(filePath, slug), fileHeadings)
|
|
129
|
+
links.set(getFilePath(filePath, slug), fileLinks)
|
|
128
130
|
}
|
|
129
131
|
}
|
|
130
132
|
|
|
@@ -133,7 +135,11 @@ export function getValidationData() {
|
|
|
133
135
|
}
|
|
134
136
|
|
|
135
137
|
function isInternalLink(link: string) {
|
|
136
|
-
return
|
|
138
|
+
return !isAbsoluteUrl(link)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function getFilePath(filePath: string, slug: string | undefined) {
|
|
142
|
+
return slug ? stripLeadingSlash(ensureTrailingSlash(slug)) : filePath
|
|
137
143
|
}
|
|
138
144
|
|
|
139
145
|
function normalizeFilePath(base: string, filePath?: string) {
|
|
@@ -169,3 +175,13 @@ interface MdxIdAttribute {
|
|
|
169
175
|
type: 'mdxJsxAttribute'
|
|
170
176
|
value: string
|
|
171
177
|
}
|
|
178
|
+
|
|
179
|
+
declare module 'vfile' {
|
|
180
|
+
interface DataMap {
|
|
181
|
+
astro?: {
|
|
182
|
+
frontmatter?: {
|
|
183
|
+
slug?: string
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
package/libs/validation.ts
CHANGED
|
@@ -32,7 +32,9 @@ export function validateLinks(
|
|
|
32
32
|
const { headings, links } = getValidationData()
|
|
33
33
|
const allPages: Pages = new Set(
|
|
34
34
|
pages.map((page) =>
|
|
35
|
-
ensureTrailingSlash(
|
|
35
|
+
ensureTrailingSlash(
|
|
36
|
+
base === '/' ? stripLeadingSlash(page.pathname) : posix.join(stripLeadingSlash(base), page.pathname),
|
|
37
|
+
),
|
|
36
38
|
),
|
|
37
39
|
)
|
|
38
40
|
|
|
@@ -113,7 +115,7 @@ function validateLink(context: ValidationContext) {
|
|
|
113
115
|
throw new Error('Failed to validate a link with no path.')
|
|
114
116
|
}
|
|
115
117
|
|
|
116
|
-
if (path.startsWith('.')) {
|
|
118
|
+
if (path.startsWith('.') || !link.startsWith('/')) {
|
|
117
119
|
if (options.errorOnRelativeLinks) {
|
|
118
120
|
addError(errors, filePath, link, ValidationErrorType.RelativeLink)
|
|
119
121
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "starlight-links-validator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
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"
|
|
@@ -26,6 +27,7 @@
|
|
|
26
27
|
"mdast-util-mdx-jsx": "3.0.0",
|
|
27
28
|
"typescript": "5.1.3",
|
|
28
29
|
"unified": "11.0.4",
|
|
30
|
+
"vfile": "6.0.1",
|
|
29
31
|
"vitest": "1.0.4"
|
|
30
32
|
},
|
|
31
33
|
"peerDependencies": {
|