docs/.vitepress/utils/mirrors.ts

ba4f23d3bab9e2bb3b8efdb22317c8cc8f353ec5
wotaku/docs/.vitepress/utils/mirrors.ts history · blame · raw

94 lines · 2735 bytes

7 symbols in this file
 1/**
 2 *  All Rights Reserved
 3 *
 4 *  Copyright (c) 2025 taskylizard
 5 *
 6 *  All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
 7 */
 8import matter from 'gray-matter'
 9import { readdirSync, readFileSync } from 'node:fs'
10import { basename, join, resolve } from 'pathe'
11
12export interface MirrorData {
13  id: string
14  title: string
15  src: string
16  mirrors: string[]
17  note: string
18  noteType: string
19  frontmatter: Record<string, string>
20  content: string
21}
22
23const NOTE_TYPE_KEYS = ['danger', 'warning', 'tip', 'info'] as const
24
25const URL_RE = /https?:\/\/[^\s<>"')\]]+/g
26
27export function extractUrls(text: string): string[] {
28  const out: string[] = []
29  const seen = new Set<string>()
30  for (const m of text.matchAll(URL_RE)) {
31    const url = m[0].replace(/[.,;:!?]+$/, '')
32    if (!seen.has(url)) {
33      seen.add(url)
34      out.push(url)
35    }
36  }
37  return out
38}
39
40let mirrorsCache: MirrorData[] | null = null
41
42export function loadMirrors(): MirrorData[] {
43  if (mirrorsCache) return mirrorsCache
44
45  const mirrorsDir = resolve(process.cwd(), 'docs/.vitepress/mirrors')
46  const mirrors: MirrorData[] = []
47
48  try {
49    const files = readdirSync(mirrorsDir).filter((file) => file.endsWith('.md'))
50
51    for (const file of files) {
52      const filePath = join(mirrorsDir, file)
53      const fileContent = readFileSync(filePath, 'utf-8')
54      const { data, content } = matter(fileContent)
55
56      const id = basename(file, '.md')
57      const src = typeof data.src === 'string' ? data.src.trim() : ''
58      const title = typeof data.title === 'string' && data.title.trim()
59        ? data.title.trim()
60        : id
61      const noteEntry = NOTE_TYPE_KEYS.map((t) => ({
62        type: t,
63        value: (data as Record<string, unknown>)[t]
64      })).find((e) => typeof e.value === 'string' && e.value.trim())
65      const note = noteEntry ? (noteEntry.value as string).trim() : ''
66      const noteType = noteEntry ? noteEntry.type : 'warning'
67      const cleanContent = content.trim()
68      const mirrorsList = extractUrls(cleanContent).filter((u) => u !== src)
69
70      mirrors.push({
71        id,
72        title,
73        src,
74        mirrors: mirrorsList,
75        note,
76        noteType,
77        frontmatter: data as Record<string, string>,
78        content: cleanContent
79      })
80    }
81  } catch (error) {
82    console.warn('Failed to load mirrors:', error)
83  }
84
85  mirrorsCache = mirrors
86  return mirrors
87}
88
89export function getMirror(id: string): MirrorData | undefined {
90  const mirrors = loadMirrors()
91  return mirrors.find((mirror) => mirror.id === id)
92}
93
94export const mirrors = loadMirrors()