返回 slidev
utils.ts
根目录 / docs / .vitepress / utils.ts
1 import { data as features } from '../features/index.data.js'
2 import { Advanced, Guides } from './pages'
3
4 function removeHash(link: string) {
5 const idx = link.lastIndexOf('#')
6 return idx < 0 ? link : link.slice(0, idx)
7 }
8
9 function getGuideTitle(id: string) {
10 return Guides.find(g => g.link.endsWith(`/${id}`))?.text ?? Advanced.find(g => g.link.endsWith(id))?.text ?? id
11 }
12
13 export function resolveLink(link: string): {
14 kind: 'external' | 'features' | 'guide'
15 url: string
16 title?: string
17 tags?: string[]
18 descripton?: string
19 } {
20 const [kind, nameWithHash] = link.split('/')
21 const name = removeHash(nameWithHash)
22 switch (kind) {
23 case 'http:':
24 case 'https:':
25 case 'mailto:':
26 return { kind: 'external', url: link }
27 case 'features': {
28 const feature = features[name]
29 if (!feature)
30 throw new Error(`Feature "${name}" not found.`)
31 return {
32 kind: 'features',
33 title: `✨ ${feature.title}`,
34 tags: feature.tags,
35 descripton: feature.description,
36 url: `/features/${nameWithHash}`,
37 }
38 }
39 case 'guide': {
40 return {
41 kind: 'guide',
42 title: `📖 ${getGuideTitle(name)}`,
43 tags: ['guide'],
44 descripton: 'Click to read this guide',
45 url: `/guide/${nameWithHash}`,
46 }
47 }
48 default:
49 throw new Error(`Invalid link: ${link}`)
50 }
51 }
52
52 lines TYPESCRIPT