| 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 |
| 7 | ? link |
| 8 | : link.slice(0, idx) |
| 9 | } |
| 10 | |
| 11 | function getGuideTitle(id: string) { |
| 12 | return Guides.find(g => g.link.endsWith(`/${id}`))?.text ?? Advanced.find(g => g.link.endsWith(id))?.text ?? id |
| 13 | } |
| 14 | |
| 15 | export function resolveLink(link: string): { |
| 16 | kind: 'external' | 'features' | 'guide' |
| 17 | url: string |
| 18 | title?: string |
| 19 | tags?: string[] |
| 20 | descripton?: string |
| 21 | } { |
| 22 | const [kind, nameWithHash] = link.split('/') |
| 23 | const name = removeHash(nameWithHash) |
| 24 | switch (kind) { |
| 25 | case 'http:': |
| 26 | case 'https:': |
| 27 | case 'mailto:': |
| 28 | return { kind: 'external', url: link } |
| 29 | case 'features': { |
| 30 | const feature = features[name] |
| 31 | if (!feature) |
| 32 | throw new Error(`Feature "${name}" not found.`) |
| 33 | return { |
| 34 | kind: 'features', |
| 35 | title: `✨ ${feature.title}`, |
| 36 | tags: feature.tags, |
| 37 | descripton: feature.description, |
| 38 | url: `/features/${nameWithHash}`, |
| 39 | } |
| 40 | } |
| 41 | case 'guide': { |
| 42 | return { |
| 43 | kind: 'guide', |
| 44 | title: `📖 ${getGuideTitle(name)}`, |
| 45 | tags: ['guide'], |
| 46 | descripton: 'Click to read this guide', |
| 47 | url: `/guide/${nameWithHash}`, |
| 48 | } |
| 49 | } |
| 50 | default: |
| 51 | throw new Error(`Invalid link: ${link}`) |
| 52 | } |
| 53 | } |
| 54 |