docs/.vitepress/theme/components/extrepo/helpers.ts
102 lines · 2598 bytes
14 symbols in this file
1import {
2 LANG_FLAG_FALLBACK,
3 LANG_FLAG_ICONS
4} from '../../../configs/markdown/langFlags'
5import type { Rating } from './types'
6
7const flagCache = new Map<string, string>()
8const labelCache = new Map<string, string>()
9const stripCache = new Map<string, string>()
10const contentTypeCache = new Map<string, string>()
11
12const langDisplayNames = new Intl.DisplayNames(['en'], { type: 'language' })
13
14export function langFlagClass(lang: string = 'all'): string {
15 const key = lang.toLowerCase()
16 let hit = flagCache.get(key)
17 if (hit === undefined) {
18 hit = `i-twemoji-${
19 LANG_FLAG_ICONS[key] ?? LANG_FLAG_ICONS[key.split('-')[0]] ??
20 LANG_FLAG_FALLBACK
21 }`
22 flagCache.set(key, hit)
23 }
24 return hit
25}
26
27export function langLabel(lang: string = 'all'): string {
28 const key = lang.toLowerCase()
29 let hit = labelCache.get(key)
30 if (hit === undefined) {
31 if (key === 'all') hit = 'Multi-language'
32 else {
33 try {
34 hit = langDisplayNames.of(lang) ?? lang
35 } catch {
36 hit = lang
37 }
38 }
39 labelCache.set(key, hit)
40 }
41 return hit
42}
43
44export function stripHtml(html: string): string {
45 let hit = stripCache.get(html)
46 if (hit === undefined) {
47 hit = html.replace(/<[^>]*>/g, '').trim()
48 if (stripCache.size > 2000) stripCache.clear()
49 stripCache.set(html, hit)
50 }
51 return hit
52}
53
54const ESCAPE_MAP: Record<string, string> = {
55 '&': '&',
56 '<': '<',
57 '>': '>',
58 '"': '"'
59}
60
61export function escapeHtml(text: string): string {
62 return text.replace(/[&<>"]/g, c => ESCAPE_MAP[c])
63}
64
65export function ratingIcon(rating: Rating): string {
66 return rating === 'nsfw'
67 ? 'i-twemoji-no-one-under-eighteen'
68 : 'i-twemoji-warning'
69}
70
71export function ratingTitle(rating: Rating): string {
72 if (rating === 'nsfw') return 'NSFW'
73 if (rating === 'suggestive') return 'Mature'
74 return 'Mixed'
75}
76
77const CONTENT_TYPE_LABELS: Record<string, string> = {
78 plugin: 'Plugin',
79 'custom-source': 'Custom Source',
80 'onlinestream-provider': 'Online Streaming',
81 'manga-provider': 'Manga Provider',
82 'anime-torrent-provider': 'Anime Torrent',
83 movie: 'Movie',
84 series: 'Series',
85 anime: 'Anime',
86 tv: 'TV',
87 channel: 'Channel',
88 other: 'Other'
89}
90
91export function contentTypeLabel(type: string): string {
92 let hit = contentTypeCache.get(type)
93 if (hit === undefined) {
94 const mapped = CONTENT_TYPE_LABELS[type.toLowerCase()]
95 hit = mapped ??
96 type.toLowerCase().split('_').map(word =>
97 word.charAt(0).toUpperCase() + word.slice(1)
98 ).join(' ')
99 contentTypeCache.set(type, hit)
100 }
101 return hit
102}