forked from vuejs/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.data.ts
98 lines (89 loc) · 2.31 KB
/
api.data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// api.data.ts
// a file ending with data.(j|t)s will be evaluated in Node.js
import fs from 'fs'
import path from 'path'
import type { MultiSidebarConfig } from '@vue/theme/src/vitepress/config'
import { sidebar } from '../../.vitepress/config'
interface APIHeader {
anchor: string
text: string
}
export interface APIGroup {
text: string
anchor: string
items: {
text: string
link: string
headers: APIHeader[]
}[]
}
// declare resolved data type
export declare const data: APIGroup[]
export default {
// declare files that should trigger HMR
watch: './*.md',
// read from fs and generate the data
load(): APIGroup[] {
return (sidebar as MultiSidebarConfig)['/api/'].map((group) => ({
text: group.text,
anchor: slugify(group.text),
items: group.items.map((item) => ({
...item,
headers: parsePageHeaders(item.link)
}))
}))
}
}
const headersCache = new Map<
string,
{
headers: APIHeader[]
timestamp: number
}
>()
function parsePageHeaders(link: string) {
const fullPath = path.join(__dirname, '../', link) + '.md'
const timestamp = fs.statSync(fullPath).mtimeMs
const cached = headersCache.get(fullPath)
if (cached && timestamp === cached.timestamp) {
return cached.headers
}
const src = fs.readFileSync(fullPath, 'utf-8')
const h2s = src.match(/^## [^\n]+/gm)
let headers: APIHeader[] = []
if (h2s) {
const anchorRE = /\{#([^}]+)\}/
headers = h2s.map((h) => {
const text = h
.slice(2)
.replace(/<sup class=.*/, '')
.replace(/\\</g, '<')
.replace(/`([^`]+)`/g, '$1')
.replace(anchorRE, '') // hidden anchor tag
.trim()
const anchor = h.match(anchorRE)?.[1] ?? slugify(text)
return { text, anchor }
})
}
headersCache.set(fullPath, {
timestamp,
headers
})
return headers
}
// same as vitepress' slugify logic
function slugify(text: string): string {
return (
text
// Replace special characters
.replace(/[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'<>,.?/]+/g, '-')
// Remove continuous separators
.replace(/-{2,}/g, '-')
// Remove prefixing and trailing separators
.replace(/^-+|-+$/g, '')
// ensure it doesn't start with a number (#121)
.replace(/^(\d)/, '_$1')
// lowercase
.toLowerCase()
)
}