starlight-links-validator 0.1.1 → 0.2.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 ADDED
@@ -0,0 +1,32 @@
1
+ import type { AstroIntegration } from 'astro'
2
+
3
+ import { remarkStarlightLinksValidator } from './libs/remark'
4
+ import { logErrors, validateLinks } from './libs/validation'
5
+
6
+ export default function starlightLinksValidatorIntegration(): AstroIntegration {
7
+ return {
8
+ name: 'starlight-links-validator',
9
+ hooks: {
10
+ 'astro:config:setup': ({ command, updateConfig }) => {
11
+ if (command !== 'build') {
12
+ return
13
+ }
14
+
15
+ updateConfig({
16
+ markdown: {
17
+ remarkPlugins: [remarkStarlightLinksValidator],
18
+ },
19
+ })
20
+ },
21
+ 'astro:build:done': ({ pages }) => {
22
+ const errors = validateLinks(pages)
23
+
24
+ logErrors(errors)
25
+
26
+ if (errors.size > 0) {
27
+ throw new Error('Links validation failed.')
28
+ }
29
+ },
30
+ },
31
+ }
32
+ }
package/libs/remark.ts ADDED
@@ -0,0 +1,149 @@
1
+ import 'mdast-util-mdx-jsx'
2
+
3
+ import nodePath from 'node:path'
4
+
5
+ import { slug } from 'github-slugger'
6
+ import type { Nodes } from 'hast'
7
+ import { fromHtml } from 'hast-util-from-html'
8
+ import { hasProperty } from 'hast-util-has-property'
9
+ import type { Root } from 'mdast'
10
+ import type { MdxJsxAttribute, MdxJsxExpressionAttribute } from 'mdast-util-mdx-jsx'
11
+ import { toString } from 'mdast-util-to-string'
12
+ import type { Plugin } from 'unified'
13
+ import { visit } from 'unist-util-visit'
14
+
15
+ // All the headings keyed by file path.
16
+ const headings: Headings = new Map()
17
+ // All the internal links keyed by file path.
18
+ const links: Links = new Map()
19
+
20
+ export const remarkStarlightLinksValidator: Plugin<[], Root> = function () {
21
+ return (tree, file) => {
22
+ const filePath = normalizeFilePath(file.history[0])
23
+
24
+ const fileHeadings: string[] = []
25
+ const fileLinks: string[] = []
26
+
27
+ visit(tree, ['heading', 'html', 'link', 'mdxJsxFlowElement', 'mdxJsxTextElement'], (node) => {
28
+ // https://github.com/syntax-tree/mdast#nodes
29
+ // https://github.com/syntax-tree/mdast-util-mdx-jsx#nodes
30
+ switch (node.type) {
31
+ case 'heading': {
32
+ const content = toString(node)
33
+
34
+ if (content.length === 0) {
35
+ break
36
+ }
37
+
38
+ fileHeadings.push(slug(content))
39
+
40
+ break
41
+ }
42
+ case 'link': {
43
+ if (isInternalLink(node.url)) {
44
+ fileLinks.push(node.url)
45
+ }
46
+
47
+ break
48
+ }
49
+ case 'mdxJsxFlowElement': {
50
+ for (const attribute of node.attributes) {
51
+ if (isMdxIdAttribute(attribute)) {
52
+ fileHeadings.push(attribute.value)
53
+ }
54
+ }
55
+
56
+ if (node.name !== 'a') {
57
+ break
58
+ }
59
+
60
+ for (const attribute of node.attributes) {
61
+ if (
62
+ attribute.type !== 'mdxJsxAttribute' ||
63
+ attribute.name !== 'href' ||
64
+ typeof attribute.value !== 'string'
65
+ ) {
66
+ continue
67
+ }
68
+
69
+ if (isInternalLink(attribute.value)) {
70
+ fileLinks.push(attribute.value)
71
+ }
72
+ }
73
+
74
+ break
75
+ }
76
+ case 'mdxJsxTextElement': {
77
+ for (const attribute of node.attributes) {
78
+ if (isMdxIdAttribute(attribute)) {
79
+ fileHeadings.push(attribute.value)
80
+ }
81
+ }
82
+
83
+ break
84
+ }
85
+ case 'html': {
86
+ const htmlTree = fromHtml(node.value, { fragment: true })
87
+
88
+ // @ts-expect-error - https://github.com/microsoft/TypeScript/issues/51188
89
+ visit(htmlTree, (htmlNode: Nodes) => {
90
+ if (hasProperty(htmlNode, 'id') && typeof htmlNode.properties.id === 'string') {
91
+ fileHeadings.push(htmlNode.properties.id)
92
+ }
93
+
94
+ if (
95
+ htmlNode.type === 'element' &&
96
+ htmlNode.tagName === 'a' &&
97
+ hasProperty(htmlNode, 'href') &&
98
+ typeof htmlNode.properties.href === 'string' &&
99
+ isInternalLink(htmlNode.properties.href)
100
+ ) {
101
+ fileLinks.push(htmlNode.properties.href)
102
+ }
103
+ })
104
+
105
+ break
106
+ }
107
+ }
108
+ })
109
+
110
+ headings.set(filePath, fileHeadings)
111
+ links.set(filePath, fileLinks)
112
+ }
113
+ }
114
+
115
+ export function getValidationData() {
116
+ return { headings, links }
117
+ }
118
+
119
+ function isInternalLink(link: string) {
120
+ return nodePath.isAbsolute(link) || link.startsWith('#')
121
+ }
122
+
123
+ function normalizeFilePath(filePath?: string) {
124
+ if (!filePath) {
125
+ throw new Error('Missing file path to validate links.')
126
+ }
127
+
128
+ return nodePath
129
+ .relative(nodePath.join(process.cwd(), 'src/content/docs'), filePath)
130
+ .replace(/\.\w+$/, '')
131
+ .replace(/index$/, '')
132
+ .replace(/\/?$/, '/')
133
+ .split('/')
134
+ .map((segment) => slug(segment))
135
+ .join('/')
136
+ }
137
+
138
+ function isMdxIdAttribute(attribute: MdxJsxAttribute | MdxJsxExpressionAttribute): attribute is MdxIdAttribute {
139
+ return attribute.type === 'mdxJsxAttribute' && attribute.name === 'id' && typeof attribute.value === 'string'
140
+ }
141
+
142
+ export type Headings = Map<string, string[]>
143
+ export type Links = Map<string, string[]>
144
+
145
+ interface MdxIdAttribute {
146
+ name: 'id'
147
+ type: 'mdxJsxAttribute'
148
+ value: string
149
+ }
@@ -0,0 +1,121 @@
1
+ import { bgGreen, black, bold, cyan, dim, red } from 'kleur/colors'
2
+
3
+ import { getValidationData, type Headings } from './remark'
4
+
5
+ export function validateLinks(pages: PageData[]): ValidationErrors {
6
+ process.stdout.write(`\n${bgGreen(black(` validating links `))}\n`)
7
+
8
+ const { headings, links } = getValidationData()
9
+ const allPages: Pages = new Set(pages.map((page) => page.pathname))
10
+
11
+ const errors: ValidationErrors = new Map()
12
+
13
+ for (const [filePath, fileLinks] of links) {
14
+ for (const link of fileLinks) {
15
+ if (link.startsWith('#')) {
16
+ validateSelfAnchor(errors, link, filePath, headings)
17
+ } else {
18
+ validateLink(errors, link, filePath, headings, allPages)
19
+ }
20
+ }
21
+ }
22
+
23
+ return errors
24
+ }
25
+
26
+ export function logErrors(errors: ValidationErrors) {
27
+ if (errors.size === 0) {
28
+ process.stdout.write(dim('All internal links are valid.\n\n'))
29
+ return
30
+ }
31
+
32
+ const errorCount = [...errors.values()].reduce((acc, links) => acc + links.length, 0)
33
+
34
+ process.stderr.write(
35
+ `${bold(
36
+ red(
37
+ `Found ${errorCount} invalid ${pluralize(errorCount, 'link')} in ${errors.size} ${pluralize(
38
+ errors.size,
39
+ 'file'
40
+ )}.`
41
+ )
42
+ )}\n\n`
43
+ )
44
+
45
+ for (const [file, links] of errors) {
46
+ process.stderr.write(`${red('▶')} ${file}\n`)
47
+
48
+ for (const [index, link] of links.entries()) {
49
+ process.stderr.write(` ${cyan(`${index < links.length - 1 ? '├' : '└'}─`)} ${link}\n`)
50
+ }
51
+
52
+ process.stdout.write(dim('\n'))
53
+ }
54
+
55
+ process.stdout.write(dim('\n'))
56
+ }
57
+
58
+ /**
59
+ * Validate a link to another internal page that may or may not have a hash.
60
+ */
61
+ function validateLink(errors: ValidationErrors, link: string, filePath: string, headings: Headings, pages: Pages) {
62
+ const sanitizedLink = link.replace(/^\//, '')
63
+ const segments = sanitizedLink.split('#')
64
+
65
+ let path = segments[0]
66
+ const hash = segments[1]
67
+
68
+ if (path === undefined) {
69
+ throw new Error('Failed to validate a link with no path.')
70
+ } else if (path.length > 0 && !path.endsWith('/')) {
71
+ path += '/'
72
+ }
73
+
74
+ const isValidPage = pages.has(path)
75
+ const fileHeadings = headings.get(path === '' ? '/' : path)
76
+
77
+ if (!isValidPage || !fileHeadings) {
78
+ addError(errors, filePath, link)
79
+ return
80
+ }
81
+
82
+ if (hash && !fileHeadings.includes(hash)) {
83
+ addError(errors, filePath, link)
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Validate a link to an anchor in the same page.
89
+ */
90
+ function validateSelfAnchor(errors: ValidationErrors, hash: string, filePath: string, headings: Headings) {
91
+ const sanitizedHash = hash.replace(/^#/, '')
92
+ const fileHeadings = headings.get(filePath)
93
+
94
+ if (!fileHeadings) {
95
+ throw new Error(`Failed to find headings for the file at '${filePath}'.`)
96
+ }
97
+
98
+ if (!fileHeadings.includes(sanitizedHash)) {
99
+ addError(errors, filePath, hash)
100
+ }
101
+ }
102
+
103
+ function addError(errors: ValidationErrors, filePath: string, link: string) {
104
+ const fileErrors = errors.get(filePath) ?? []
105
+ fileErrors.push(link)
106
+
107
+ errors.set(filePath, fileErrors)
108
+ }
109
+
110
+ function pluralize(count: number, singular: string) {
111
+ return count === 1 ? singular : `${singular}s`
112
+ }
113
+
114
+ // The invalid links keyed by file path.
115
+ type ValidationErrors = Map<string, string[]>
116
+
117
+ interface PageData {
118
+ pathname: string
119
+ }
120
+
121
+ type Pages = Set<PageData['pathname']>
package/package.json CHANGED
@@ -1,37 +1,28 @@
1
1
  {
2
2
  "name": "starlight-links-validator",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
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)",
7
7
  "type": "module",
8
- "main": "dist/index.cjs",
9
- "module": "dist/index.js",
10
- "types": "dist/index.d.ts",
11
8
  "exports": {
12
- "./package.json": "./package.json",
13
- ".": {
14
- "import": {
15
- "types": "./dist/index.d.ts",
16
- "default": "./dist/index.js"
17
- },
18
- "default": {
19
- "types": "./dist/index.d.cts",
20
- "default": "./dist/index.cjs"
21
- }
22
- }
9
+ ".": "./index.ts",
10
+ "./package.json": "./package.json"
23
11
  },
24
12
  "dependencies": {
25
13
  "github-slugger": "2.0.0",
14
+ "hast-util-from-html": "2.0.1",
15
+ "hast-util-has-property": "3.0.0",
26
16
  "kleur": "4.1.5",
27
17
  "mdast-util-to-string": "3.2.0",
28
18
  "unist-util-visit": "4.1.2"
29
19
  },
30
20
  "devDependencies": {
21
+ "@types/hast": "3.0.0",
31
22
  "@types/mdast": "3.0.11",
32
23
  "@types/node": "18.16.18",
24
+ "astro": "2.10.9",
33
25
  "mdast-util-mdx-jsx": "2.1.4",
34
- "tsup": "7.0.0",
35
26
  "typescript": "5.1.3",
36
27
  "unified": "10.1.2",
37
28
  "vitest": "0.32.2"
@@ -48,9 +39,6 @@
48
39
  "access": "public"
49
40
  },
50
41
  "sideEffects": false,
51
- "files": [
52
- "dist"
53
- ],
54
42
  "keywords": [
55
43
  "starlight",
56
44
  "links",
@@ -65,8 +53,6 @@
65
53
  },
66
54
  "bugs": "https://github.com/HiDeoo/starlight-links-validator/issues",
67
55
  "scripts": {
68
- "dev": "tsup --watch",
69
- "build": "tsup && cp dist/index.d.ts dist/index.d.cts",
70
56
  "test": "vitest",
71
57
  "lint": "prettier -c --cache . && eslint . --cache --max-warnings=0 && tsc --noEmit"
72
58
  }
package/dist/index.cjs DELETED
@@ -1,11 +0,0 @@
1
- "use strict";var z=Object.create;var d=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var A=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty;var S=(t,i)=>{for(var n in i)d(t,n,{get:i[n],enumerable:!0})},u=(t,i,n,r)=>{if(i&&typeof i=="object"||typeof i=="function")for(let e of M(i))!F.call(t,e)&&e!==n&&d(t,e,{get:()=>i[e],enumerable:!(r=H(i,e))||r.enumerable});return t};var D=(t,i,n)=>(n=t!=null?z(A(t)):{},u(i||!t||!t.__esModule?d(n,"default",{value:t,enumerable:!0}):n,t)),I=t=>u(d({},"__esModule",{value:!0}),t);var j={};S(j,{default:()=>V});module.exports=I(j);var G=require("mdast-util-mdx-jsx"),c=D(require("path"),1),h=require("github-slugger"),k=require("mdast-util-to-string"),w=require("unist-util-visit"),v=new Map,E=new Map,$=function(){return(t,i)=>{let n=J(i.history[0]),r=[],e=[];(0,w.visit)(t,["heading","link","mdxJsxFlowElement"],s=>{switch(s.type){case"heading":{let a=(0,k.toString)(s);if(a.length===0)break;r.push((0,h.slug)(a));break}case"link":{m(s.url)&&e.push(s.url);break}case"mdxJsxFlowElement":{if(s.name!=="a")break;for(let a of s.attributes)a.type!=="mdxJsxAttribute"||a.name!=="href"||typeof a.value!="string"||m(a.value)&&e.push(a.value);break}}}),v.set(n,r),E.set(n,e)}};function b(){return{headings:v,links:E}}function m(t){return c.default.isAbsolute(t)||t.startsWith("#")}function J(t){if(!t)throw new Error("Missing file path to validate links.");return c.default.relative(c.default.join(process.cwd(),"src/content/docs"),t).replace(/\.\w+$/,"").replace(/index$/,"").replace(/\/?$/,"/")}var o=require("kleur/colors");function L(t){process.stdout.write(`
2
- ${(0,o.bgGreen)((0,o.black)(" validating links "))}
3
- `);let{headings:i,links:n}=b(),r=new Set(t.map(s=>s.pathname)),e=new Map;for(let[s,a]of n)for(let l of a)l.startsWith("#")?R(e,l,s,i):W(e,l,s,i,r);return e}function y(t){if(t.size===0){process.stdout.write((0,o.dim)(`All internal links are valid.
4
-
5
- `));return}let i=[...t.values()].reduce((n,r)=>n+r.length,0);process.stderr.write(`${(0,o.bold)((0,o.red)(`Found ${i} invalid ${x(i,"link")} in ${t.size} ${x(t.size,"file")}.`))}
6
-
7
- `);for(let[n,r]of t){process.stderr.write(`${(0,o.red)("\u25B6")} ${n}
8
- `);for(let[e,s]of r.entries())process.stderr.write(` ${(0,o.cyan)(`${e<r.length-1?"\u251C":"\u2514"}\u2500`)} ${s}
9
- `);process.stdout.write((0,o.dim)(`
10
- `))}process.stdout.write((0,o.dim)(`
11
- `))}function W(t,i,n,r,e){let a=i.replace(/^\//,"").split("#"),l=a[0],f=a[1];if(l===void 0)throw new Error("Failed to validate a link with no path.");l.length>0&&!l.endsWith("/")&&(l+="/");let P=e.has(l),p=r.get(l===""?"/":l);if(!P||!p){g(t,n,i);return}f&&!p.includes(f)&&g(t,n,i)}function R(t,i,n,r){let e=i.replace(/^#/,""),s=r.get(n);if(!s)throw new Error(`Failed to find headings for the file at '${n}'.`);s.includes(e)||g(t,n,i)}function g(t,i,n){let r=t.get(i)??[];r.push(n),t.set(i,r)}function x(t,i){return t===1?i:`${i}s`}function V(){return{name:"starlight-links-validator",hooks:{"astro:config:setup":({command:t,updateConfig:i})=>{t==="build"&&i({markdown:{remarkPlugins:[$]}})},"astro:build:done":({pages:t})=>{let i=L(t);if(y(i),i.size>0)throw new Error("Links validation failed.")}}}}
package/dist/index.d.cts DELETED
@@ -1,5 +0,0 @@
1
- import { AstroIntegration } from 'astro';
2
-
3
- declare function starlightLinksValidatorIntegration(): AstroIntegration;
4
-
5
- export { starlightLinksValidatorIntegration as default };
package/dist/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import { AstroIntegration } from 'astro';
2
-
3
- declare function starlightLinksValidatorIntegration(): AstroIntegration;
4
-
5
- export { starlightLinksValidatorIntegration as default };
package/dist/index.js DELETED
@@ -1,11 +0,0 @@
1
- import"mdast-util-mdx-jsx";import l from"node:path";import{slug as x}from"github-slugger";import{toString as L}from"mdast-util-to-string";import{visit as y}from"unist-util-visit";var u=new Map,m=new Map,h=function(){return(t,i)=>{let n=V(i.history[0]),e=[],s=[];y(t,["heading","link","mdxJsxFlowElement"],r=>{switch(r.type){case"heading":{let o=L(r);if(o.length===0)break;e.push(x(o));break}case"link":{p(r.url)&&s.push(r.url);break}case"mdxJsxFlowElement":{if(r.name!=="a")break;for(let o of r.attributes)o.type!=="mdxJsxAttribute"||o.name!=="href"||typeof o.value!="string"||p(o.value)&&s.push(o.value);break}}}),u.set(n,e),m.set(n,s)}};function k(){return{headings:u,links:m}}function p(t){return l.isAbsolute(t)||t.startsWith("#")}function V(t){if(!t)throw new Error("Missing file path to validate links.");return l.relative(l.join(process.cwd(),"src/content/docs"),t).replace(/\.\w+$/,"").replace(/index$/,"").replace(/\/?$/,"/")}import{bgGreen as P,black as z,bold as H,cyan as M,dim as d,red as w}from"kleur/colors";function E(t){process.stdout.write(`
2
- ${P(z(" validating links "))}
3
- `);let{headings:i,links:n}=k(),e=new Set(t.map(r=>r.pathname)),s=new Map;for(let[r,o]of n)for(let a of o)a.startsWith("#")?F(s,a,r,i):A(s,a,r,i,e);return s}function $(t){if(t.size===0){process.stdout.write(d(`All internal links are valid.
4
-
5
- `));return}let i=[...t.values()].reduce((n,e)=>n+e.length,0);process.stderr.write(`${H(w(`Found ${i} invalid ${v(i,"link")} in ${t.size} ${v(t.size,"file")}.`))}
6
-
7
- `);for(let[n,e]of t){process.stderr.write(`${w("\u25B6")} ${n}
8
- `);for(let[s,r]of e.entries())process.stderr.write(` ${M(`${s<e.length-1?"\u251C":"\u2514"}\u2500`)} ${r}
9
- `);process.stdout.write(d(`
10
- `))}process.stdout.write(d(`
11
- `))}function A(t,i,n,e,s){let o=i.replace(/^\//,"").split("#"),a=o[0],g=o[1];if(a===void 0)throw new Error("Failed to validate a link with no path.");a.length>0&&!a.endsWith("/")&&(a+="/");let b=s.has(a),f=e.get(a===""?"/":a);if(!b||!f){c(t,n,i);return}g&&!f.includes(g)&&c(t,n,i)}function F(t,i,n,e){let s=i.replace(/^#/,""),r=e.get(n);if(!r)throw new Error(`Failed to find headings for the file at '${n}'.`);r.includes(s)||c(t,n,i)}function c(t,i,n){let e=t.get(i)??[];e.push(n),t.set(i,e)}function v(t,i){return t===1?i:`${i}s`}function S(){return{name:"starlight-links-validator",hooks:{"astro:config:setup":({command:t,updateConfig:i})=>{t==="build"&&i({markdown:{remarkPlugins:[h]}})},"astro:build:done":({pages:t})=>{let i=E(t);if($(i),i.size>0)throw new Error("Links validation failed.")}}}}export{S as default};