返回 slidev
index.data.ts
根目录 / docs / features / index.data.ts
1 import { basename } from 'node:path'
2 import { createContentLoader } from 'vitepress'
3
4 const RE_FEATURE_NAME = /\/([\w-]+)($|#)/
5 const RE_HEADING1 = /^# (.*)$/m
6
7 export interface Feature {
8 name: string
9 title: string
10 link: string
11 description: string
12 depends: string[]
13 relates: string[]
14 derives: string[]
15 tags: string[]
16 since?: string
17 }
18
19 export default createContentLoader('features/*.md', {
20 includeSrc: true,
21 transform(data) {
22 const derivesMap: Record<string, string[]> = {}
23 for (const md of data) {
24 const name = basename(md.url, '.md')
25 if (name === 'index' || name === 'features')
26 continue
27 for (const depend of md.frontmatter.depends ?? []) {
28 const dependName = depend.match(RE_FEATURE_NAME)?.[1]
29 if (dependName) {
30 derivesMap[dependName] ??= []
31 derivesMap[dependName].push(`features/${name}`)
32 }
33 }
34 }
35
36 const result: Record<string, Feature> = {}
37 for (const md of data) {
38 const name = basename(md.url, '.md')
39 if (name === 'index' || name === 'features')
40 continue
41 const title = md.src?.match(RE_HEADING1)?.[1]?.trim() ?? name
42 const derives = md.frontmatter.derives ?? []
43 for (const d of derivesMap[name] ?? []) {
44 if (!derives.includes(d)) {
45 derives.push(d)
46 }
47 }
48 result[name] = {
49 name,
50 title,
51 link: `/features/${name}.html`,
52 description: md.frontmatter.description ?? '',
53 depends: md.frontmatter.depends ?? [],
54 relates: md.frontmatter.relates ?? [],
55 derives,
56 tags: md.frontmatter.tags ?? [],
57 since: md.frontmatter.since,
58 }
59 }
60 return result
61 },
62 })
63
64 export declare const data: Record<string, Feature>
65
65 lines TYPESCRIPT