docs/.vitepress/configs/markdown/emoji.ts
236 lines · 7859 bytes
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 type { IconifyJSON } from '@iconify-json/octicon'
9import type { MarkdownRenderer } from 'vitepress'
10import { iconTooltips } from './tooltip'
11
12// Icons that need to be used should be imported here
13import { icons as akar } from '@iconify-json/akar-icons'
14import { icons as ic } from '@iconify-json/ic'
15import { icons as iconoir } from '@iconify-json/iconoir'
16import { icons as logos } from '@iconify-json/logos'
17import { icons as lucide } from '@iconify-json/lucide'
18import { icons as materials } from '@iconify-json/material-symbols'
19import { icons as mdi } from '@iconify-json/mdi'
20import { icons as mingcute } from '@iconify-json/mingcute'
21import { icons as octicon } from '@iconify-json/octicon'
22import { icons as ri } from '@iconify-json/ri'
23import { icons as simple } from '@iconify-json/simple-icons'
24import { icons as twemoji } from '@iconify-json/twemoji'
25import { icons as uil } from '@iconify-json/uil'
26
27// 1. Install emoji pack with `pnpm add -D @iconify-json/<icon>`
28// 2. Import them like I did above
29// 3. Add it to this emojis array, like I did for octicon, and add a prefix
30const emojis: { pack: IconifyJSON; prefix?: string }[] = [
31 // Default emojis (twemoji)
32 { pack: twemoji },
33 // octicon emojis, prefixed with "octicon-"
34 { pack: octicon, prefix: 'octicon-' },
35 { pack: logos, prefix: 'logos-' },
36 { pack: ic, prefix: 'ic-' },
37 { pack: mingcute, prefix: 'mingcute-' },
38 { pack: mdi, prefix: 'mdi-' },
39 { pack: materials, prefix: 'material-symbols-' },
40 { pack: simple, prefix: 'simple-icons-' },
41 { pack: lucide, prefix: 'lucide-' },
42 { pack: uil, prefix: 'uil-' },
43 { pack: ri, prefix: 'ri-' },
44 { pack: akar, prefix: 'akar-icons-' },
45 { pack: iconoir, prefix: 'iconoir-' }
46]
47
48// Add aliases here...
49const aliases: Record<string, string> = {
50 // System
51 and: 'simple-icons-android',
52 ios: 'simple-icons-apple',
53 win: 'simple-icons-windows',
54 app: 'ri-finder-fill',
55 lin: 'simple-icons-linux',
56 bsd: 'simple-icons-freebsd',
57 cmd: 'material-symbols-terminal-rounded',
58 ff: 'simple-icons-firefoxbrowser',
59 cr: 'simple-icons-googlechrome',
60 web: 'mdi-web',
61
62 // Type
63 strm: 'material-symbols-play-circle-outline',
64 ddl: 'material-symbols-cloud-download-outline-rounded',
65 mag: 'lucide-magnet',
66 lcl: 'iconoir-floppy-disk',
67 batch: 'material-symbols-package-2-outline',
68 sp: `lucide-file`,
69 dp: `lucide-book-open`,
70 ls: `lucide-scroll`,
71
72 // Tracking
73 sync: `material-symbols-sync`,
74 ie: `material-symbols-arrows-up-down-circle-outline-rounded`,
75
76 // Price
77 paid: 'ic-round-attach-money',
78 fm: 'ic-round-add-shopping-cart',
79 free: 'ic-round-money-off-csred',
80 sub: 'ic-round-currency-exchange',
81 coin: 'akar-icons-coin',
82
83 // Source
84 src: 'mdi-package-variant',
85 cs: 'mdi-package-variant-closed-remove',
86
87 // Platform
88 d: 'simple-icons-discord',
89 f: 'simple-icons-4chan',
90 x: 'simple-icons-twitter',
91 tg: 'simple-icons-telegram',
92 mal: 'simple-icons-myanimelist',
93 al: 'simple-icons-anilist',
94 k: 'simple-icons-kitsu',
95 simkl: 'simple-icons-simkl',
96
97 // Storage
98 sgd: 'simple-icons-googledrive',
99 sm: 'simple-icons-mega',
100 smf: 'simple-icons-mediafire',
101 host: 'mdi-snail',
102
103 // Flag
104 fjp: 'twemoji-flag-japan',
105 fkr: 'twemoji-flag-south-korea',
106 fcn: 'twemoji-flag-china',
107
108 // Code
109 py: 'simple-icons-python',
110 js: 'simple-icons-javascript',
111 css: 'mdi-language-css3',
112 dock: 'simple-icons-docker',
113
114 // Others
115 s: 'twemoji-glowing-star',
116 e: 'mdi-puzzle',
117 n: 'material-symbols-science',
118 alt: 'ic-round-looks-two',
119 ero: 'twemoji-no-one-under-eighteen',
120 acc: 'material-symbols-person-add',
121 rss: 'simple-icons-rss',
122 prx: 'material-symbols-directions-alt',
123 tf: 'mdi-airplane',
124 more: 'mdi-arrow-right-bold',
125 help: 'material-symbols-help',
126 yes: 'twemoji-check-mark-button',
127 no: 'twemoji-cross-mark',
128 global: 'twemoji-globe-showing-asia-australia',
129 up: 'material-symbols-upload',
130 ms: 'material-symbols-file-copy-rounded',
131 ps: 'material-symbols-file-copy-outline-rounded',
132 ss: 'material-symbols-file-copy-off-rounded',
133 cc: 'mdi-closed-caption',
134 hs: 'mdi-hulu'
135}
136
137// Tooltip labels for icon aliases (desktop hover)
138const aliasLabels: Record<string, string> = {
139 s: 'Favorite', src: 'Source Code', cs: 'Closed Source',
140 e: 'Extension / Plugin', prx: 'Proxies', ero: 'R18',
141 acc: 'Needs account', help: 'Help / Docs', more: 'Related',
142 and: 'Android', ios: 'iOS / iPadOS', app: 'MacOS',
143 win: 'Windows', lin: 'Linux', bsd: 'BSD',
144 cmd: 'CLI / TUI', ff: 'Firefox', cr: 'Chromium', web: 'Web',
145 strm: 'Stream', ddl: 'Online / DDL', mag: 'Torrent / p2p',
146 lcl: 'Local file', batch: 'Batch Download',
147 sp: 'Single Page Mode', dp: 'Double Page Mode', ls: 'Long-Strip Mode',
148 paid: 'Paid', sub: 'Subscription', free: 'Has free content',
149 coin: 'Coin / Point', fm: 'Freemium',
150 d: 'Discord', f: '4chan', x: 'Twitter', tg: 'Telegram',
151 mal: 'MyAnimeList', al: 'AniList', k: 'Kitsu', simkl: 'Simkl',
152 sgd: 'Google Drive', sm: 'MEGA', smf: 'MediaFire', host: 'Other Host',
153 fjp: 'Japan', fkr: 'Korea', fcn: 'China',
154 js: 'JavaScript', css: 'CSS', py: 'Python', dock: 'Docker',
155 n: 'Nightly build', alt: 'Alternative', rss: 'RSS Feed', tf: 'Testflight',
156 yes: 'Yes / Available', no: 'No / Unavailable',
157 global: 'Global', cc: 'Subtitles / CC', hs: 'Hardsubs',
158 up: 'More Info', ms: 'Feedback',
159 sync: 'Sync', ie: 'Import / Export'
160}
161
162// Custom icons using UnoCSS inline collection
163const customIconAliases: Record<string, string> = {
164 'custom-circle': 'inline-circle'
165 // Add more custom icon aliases here
166}
167
168const defs: Record<string, string> = {}
169
170// Add pack icons to defs
171for (const elem of emojis) {
172 for (const key in elem.pack.icons) {
173 if (elem.prefix) defs[elem.prefix + key] = ''
174 else defs[key] = ''
175 }
176}
177
178// Add custom icon aliases to defs
179for (const [alias, _] of Object.entries(customIconAliases)) {
180 defs[alias] = ''
181}
182
183for (const [alias, fullName] of Object.entries(aliases)) {
184 defs[alias] = defs[fullName] !== undefined ? '' : 'INVALID_ALIAS'
185}
186
187export { aliases, defs }
188
189export function emojiRender(md: MarkdownRenderer) {
190 md.renderer.rules.emoji = (tokens, idx) => {
191 const markup = tokens[idx].markup
192 const label = aliasLabels[markup] || iconTooltips[markup]
193
194 const tip = label?.replace(/"/g, '"')
195 const wrap = (cls: string) => tip
196 ? `<span class="icon-tip" data-tip="${tip}" tabindex="-1"><span class="${cls}"></span></span>`
197 : `<span class="${cls}"></span>`
198
199 // Check for custom icon aliases first
200 if (customIconAliases[markup]) return wrap(`i-${customIconAliases[markup]}`)
201
202 // Check for aliases
203 if (aliases[markup]) return wrap(`i-${aliases[markup]}`)
204
205 // Check for prefixed icons
206 for (const emoji of emojis) {
207 if (markup.startsWith(emoji.prefix!)) return wrap(`i-${markup}`)
208 }
209
210 // Default to twemoji
211 return wrap(`i-twemoji-${markup}`)
212 }
213}
214
215export function movePlugin(
216 plugins: { name: string }[],
217 pluginAName: string,
218 order: 'before' | 'after',
219 pluginBName: string
220) {
221 const pluginBIndex = plugins.findIndex((p) => p.name === pluginBName)
222 if (pluginBIndex === -1) return
223
224 const pluginAIndex = plugins.findIndex((p) => p.name === pluginAName)
225 if (pluginAIndex === -1) return
226
227 if (order === 'before' && pluginAIndex > pluginBIndex) {
228 const pluginA = plugins.splice(pluginAIndex, 1)[0]
229 plugins.splice(pluginBIndex, 0, pluginA)
230 }
231
232 if (order === 'after' && pluginAIndex < pluginBIndex) {
233 const pluginA = plugins.splice(pluginAIndex, 1)[0]
234 plugins.splice(pluginBIndex, 0, pluginA)
235 }
236}