docs/.vitepress/configs/markdown/emoji.ts
210 lines · 6362 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'
10
11// Icons that need to be used should be imported here
12import { icons as akar } from '@iconify-json/akar-icons'
13import { icons as ic } from '@iconify-json/ic'
14import { icons as iconoir } from '@iconify-json/iconoir'
15import { icons as logos } from '@iconify-json/logos'
16import { icons as lucide } from '@iconify-json/lucide'
17import { icons as materials } from '@iconify-json/material-symbols'
18import { icons as mdi } from '@iconify-json/mdi'
19import { icons as mingcute } from '@iconify-json/mingcute'
20import { icons as octicon } from '@iconify-json/octicon'
21import { icons as ri } from '@iconify-json/ri'
22import { icons as simple } from '@iconify-json/simple-icons'
23import { icons as twemoji } from '@iconify-json/twemoji'
24import { icons as uil } from '@iconify-json/uil'
25
26// 1. Install emoji pack with `pnpm add -D @iconify-json/<icon>`
27// 2. Import them like I did above
28// 3. Add it to this emojis array, like I did for octicon, and add a prefix
29const emojis: { pack: IconifyJSON; prefix?: string }[] = [
30 // Default emojis (twemoji)
31 { pack: twemoji },
32 // octicon emojis, prefixed with "octicon-"
33 { pack: octicon, prefix: 'octicon-' },
34 { pack: logos, prefix: 'logos-' },
35 { pack: ic, prefix: 'ic-' },
36 { pack: mingcute, prefix: 'mingcute-' },
37 { pack: mdi, prefix: 'mdi-' },
38 { pack: materials, prefix: 'material-symbols-' },
39 { pack: simple, prefix: 'simple-icons-' },
40 { pack: lucide, prefix: 'lucide-' },
41 { pack: uil, prefix: 'uil-' },
42 { pack: ri, prefix: 'ri-' },
43 { pack: akar, prefix: 'akar-icons-' },
44 { pack: iconoir, prefix: 'iconoir-' }
45]
46
47// Add aliases here...
48const aliases: Record<string, string> = {
49 // System
50 and: 'simple-icons-android',
51 ios: 'simple-icons-apple',
52 win: 'simple-icons-windows',
53 app: 'ri-finder-fill',
54 lin: 'simple-icons-linux',
55 bsd: 'simple-icons-freebsd',
56 cmd: 'material-symbols-terminal-rounded',
57 ff: 'simple-icons-firefoxbrowser',
58 cr: 'simple-icons-googlechrome',
59 web: 'mdi-web',
60
61 // Type
62 strm: 'material-symbols-play-circle-outline',
63 ddl: 'material-symbols-cloud-download-outline-rounded',
64 mag: 'lucide-magnet',
65 lcl: 'iconoir-floppy-disk',
66 batch: 'material-symbols-package-2-outline',
67 sp: `lucide-file`,
68 dp: `lucide-book-open`,
69 ls: `lucide-scroll`,
70
71 // Tracking
72 sync: `material-symbols-sync`,
73 ie: `material-symbols-arrows-up-down-circle-outline-rounded`,
74
75 // Price
76 paid: 'ic-round-attach-money',
77 fm: 'ic-round-add-shopping-cart',
78 free: 'ic-round-money-off-csred',
79 sub: 'ic-round-currency-exchange',
80 coin: 'akar-icons-coin',
81
82 // Source
83 src: 'mdi-package-variant',
84 cs: 'mdi-package-variant-closed-remove',
85
86 // Platform
87 d: 'simple-icons-discord',
88 f: 'simple-icons-4chan',
89 x: 'simple-icons-twitter',
90 tg: 'simple-icons-telegram',
91 mal: 'simple-icons-myanimelist',
92 al: 'simple-icons-anilist',
93 k: 'simple-icons-kitsu',
94 simkl: 'simple-icons-simkl',
95
96 // Storage
97 sgd: 'simple-icons-googledrive',
98 sm: 'simple-icons-mega',
99 smf: 'simple-icons-mediafire',
100 host: 'mdi-snail',
101
102 // Flag
103 fjp: 'twemoji-flag-japan',
104 fkr: 'twemoji-flag-south-korea',
105 fcn: 'twemoji-flag-china',
106
107 // Code
108 py: 'simple-icons-python',
109 js: 'simple-icons-javascript',
110 css: 'mdi-language-css3',
111 dock: 'simple-icons-docker',
112
113 // Others
114 s: 'twemoji-glowing-star',
115 e: 'mdi-puzzle',
116 n: 'material-symbols-science',
117 alt: 'ic-round-looks-two',
118 ero: 'twemoji-no-one-under-eighteen',
119 acc: 'material-symbols-person-add',
120 rss: 'simple-icons-rss',
121 prx: 'material-symbols-directions-alt',
122 tf: 'mdi-airplane',
123 more: 'mdi-arrow-right-bold',
124 help: 'material-symbols-help',
125 yes: 'twemoji-check-mark-button',
126 no: 'twemoji-cross-mark',
127 global: 'twemoji-globe-showing-asia-australia',
128 up: 'material-symbols-upload',
129 ms: 'material-symbols-file-copy-rounded',
130 ps: 'material-symbols-file-copy-outline-rounded',
131 ss: 'material-symbols-file-copy-off-rounded',
132 cc: 'mdi-closed-caption',
133 hs: 'mdi-hulu'
134}
135
136// Custom icons using UnoCSS inline collection
137const customIconAliases: Record<string, string> = {
138 'custom-circle': 'inline-circle'
139 // Add more custom icon aliases here
140}
141
142const defs: Record<string, string> = {}
143
144// Add pack icons to defs
145for (const elem of emojis) {
146 for (const key in elem.pack.icons) {
147 if (elem.prefix) defs[elem.prefix + key] = ''
148 else defs[key] = ''
149 }
150}
151
152// Add custom icon aliases to defs
153for (const [alias, _] of Object.entries(customIconAliases)) {
154 defs[alias] = ''
155}
156
157for (const [alias, fullName] of Object.entries(aliases)) {
158 defs[alias] = defs[fullName] !== undefined ? '' : 'INVALID_ALIAS'
159}
160
161export { aliases, defs }
162
163export function emojiRender(md: MarkdownRenderer) {
164 md.renderer.rules.emoji = (tokens, idx) => {
165 const markup = tokens[idx].markup
166
167 // Check for custom icon aliases first
168 if (customIconAliases[markup]) {
169 return `<span class="i-${customIconAliases[markup]}"></span>`
170 }
171
172 // Check for aliases
173 if (aliases[markup]) {
174 return `<span class="i-${aliases[markup]}"></span>`
175 }
176
177 // Check for prefixed icons
178 for (const emoji of emojis) {
179 if (markup.startsWith(emoji.prefix!)) {
180 return `<span class="i-${markup}"></span>`
181 }
182 }
183
184 // Default to twemoji
185 return `<span class="i-twemoji-${markup}"></span>`
186 }
187}
188
189export function movePlugin(
190 plugins: { name: string }[],
191 pluginAName: string,
192 order: 'before' | 'after',
193 pluginBName: string
194) {
195 const pluginBIndex = plugins.findIndex((p) => p.name === pluginBName)
196 if (pluginBIndex === -1) return
197
198 const pluginAIndex = plugins.findIndex((p) => p.name === pluginAName)
199 if (pluginAIndex === -1) return
200
201 if (order === 'before' && pluginAIndex > pluginBIndex) {
202 const pluginA = plugins.splice(pluginAIndex, 1)[0]
203 plugins.splice(pluginBIndex, 0, pluginA)
204 }
205
206 if (order === 'after' && pluginAIndex < pluginBIndex) {
207 const pluginA = plugins.splice(pluginAIndex, 1)[0]
208 plugins.splice(pluginBIndex, 0, pluginA)
209 }
210}