tags.data.ts (1013B)
1 import { createContentLoader } from 'vitepress' 2 3 export interface TaggedPage { 4 title: string 5 url: string 6 } 7 8 export interface TagsData { 9 [tag: string]: TaggedPage[] 10 } 11 12 declare const data: TagsData 13 export { data } 14 15 function stripTitleIcon(title: string): string { 16 if (!title) return title 17 return title.replace(/<svg[\s\S]*?<\/svg>\s*/i, '').trim() 18 } 19 20 export default createContentLoader('**/*.md', { 21 includeSrc: false, 22 render: false, 23 excerpt: false, 24 transform(raw) { 25 const byTag: TagsData = {} 26 27 for (const page of raw) { 28 const tags = page.frontmatter?.tags 29 if (!Array.isArray(tags) || tags.length === 0) continue 30 31 const title = stripTitleIcon(page.frontmatter?.title || page.url) 32 33 for (const tag of tags) { 34 if (!tag) continue 35 if (!byTag[tag]) byTag[tag] = [] 36 byTag[tag].push({ title, url: page.url }) 37 } 38 } 39 40 for (const tag in byTag) { 41 byTag[tag].sort((a, b) => a.title.localeCompare(b.title)) 42 } 43 44 return byTag 45 } 46 })