starlight-links-validator 0.4.0 → 0.4.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.
Files changed (2) hide show
  1. package/libs/remark.ts +19 -4
  2. package/package.json +1 -1
package/libs/remark.ts CHANGED
@@ -2,7 +2,7 @@ import 'mdast-util-mdx-jsx'
2
2
 
3
3
  import nodePath from 'node:path'
4
4
 
5
- import { slug } from 'github-slugger'
5
+ 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'
@@ -19,12 +19,18 @@ const links: Links = new Map()
19
19
 
20
20
  export const remarkStarlightLinksValidator: Plugin<[], Root> = function () {
21
21
  return (tree, file) => {
22
+ const slugger = new GitHubSlugger()
22
23
  const filePath = normalizeFilePath(file.history[0])
23
24
 
24
25
  const fileHeadings: string[] = []
25
26
  const fileLinks: string[] = []
27
+ const fileDefinitions = new Map<string, string>()
26
28
 
27
- visit(tree, ['heading', 'html', 'link', 'mdxJsxFlowElement', 'mdxJsxTextElement'], (node) => {
29
+ visit(tree, 'definition', (node) => {
30
+ fileDefinitions.set(node.identifier, node.url)
31
+ })
32
+
33
+ visit(tree, ['heading', 'html', 'link', 'linkReference', 'mdxJsxFlowElement', 'mdxJsxTextElement'], (node) => {
28
34
  // https://github.com/syntax-tree/mdast#nodes
29
35
  // https://github.com/syntax-tree/mdast-util-mdx-jsx#nodes
30
36
  switch (node.type) {
@@ -35,7 +41,7 @@ export const remarkStarlightLinksValidator: Plugin<[], Root> = function () {
35
41
  break
36
42
  }
37
43
 
38
- fileHeadings.push(slug(content))
44
+ fileHeadings.push(slugger.slug(content))
39
45
 
40
46
  break
41
47
  }
@@ -46,6 +52,15 @@ export const remarkStarlightLinksValidator: Plugin<[], Root> = function () {
46
52
 
47
53
  break
48
54
  }
55
+ case 'linkReference': {
56
+ const definition = fileDefinitions.get(node.identifier)
57
+
58
+ if (definition && isInternalLink(definition)) {
59
+ fileLinks.push(definition)
60
+ }
61
+
62
+ break
63
+ }
49
64
  case 'mdxJsxFlowElement': {
50
65
  for (const attribute of node.attributes) {
51
66
  if (isMdxIdAttribute(attribute)) {
@@ -129,7 +144,7 @@ function normalizeFilePath(filePath?: string) {
129
144
  .replace(/\.\w+$/, '')
130
145
  .replace(/index$/, '')
131
146
  .replace(/\/?$/, '/')
132
- .split('/')
147
+ .split(/[/\\]/)
133
148
  .map((segment) => slug(segment))
134
149
  .join('/')
135
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-links-validator",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "license": "MIT",
5
5
  "description": "Astro integration for Starlight to validate internal links.",
6
6
  "author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",