docs/.vitepress/configs/markdown/emoji.ts
192 lines · 5918 bytes
1/**
2 * Copyright (c) 2024 taskylizard
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import type { MarkdownRenderer } from 'vitepress'
17import type { IconifyJSON } from '@iconify-json/octicon'
18
19// Icons that need to be used should be imported here
20import { icons as twemoji } from '@iconify-json/twemoji'
21import { icons as octicon } from '@iconify-json/octicon'
22import { icons as logos } from '@iconify-json/logos'
23import { icons as ic } from '@iconify-json/ic'
24import { icons as mingcute } from '@iconify-json/mingcute'
25import { icons as mdi } from '@iconify-json/mdi'
26import { icons as materials } from '@iconify-json/material-symbols'
27import { icons as simple } from '@iconify-json/simple-icons'
28import { icons as lucide } from '@iconify-json/lucide'
29import { icons as iconoir } from '@iconify-json/iconoir'
30import { icons as uil } from '@iconify-json/uil'
31import { icons as ri } from '@iconify-json/ri'
32import { icons as akar } from '@iconify-json/akar-icons'
33
34// 1. Install emoji pack with `pnpm add -g @iconify-json/<icon>`
35// 2. Import them like I did above
36// 3. Add it to this emojis array, like I did for octicon, and add a prefix
37const emojis: { pack: IconifyJSON; prefix?: string }[] = [
38 // Default emojis (twemoji)
39 { pack: twemoji },
40 // octicon emojis, prefixed with "octicon-"
41 { pack: octicon, prefix: 'octicon-' },
42 { pack: logos, prefix: 'logos-' },
43 { pack: ic, prefix: 'ic-' },
44 { pack: mingcute, prefix: 'mingcute-' },
45 { pack: mdi, prefix: 'mdi-' },
46 { pack: materials, prefix: 'material-symbols-' },
47 { pack: simple, prefix: 'simple-icons-' },
48 { pack: lucide, prefix: 'lucide-' },
49 { pack: uil, prefix: 'uil-' },
50 { pack: ri, prefix: 'ri-' },
51 { pack: akar, prefix: 'akar-icons-' },
52 { pack: iconoir, prefix: 'iconoir-' }
53]
54
55// Add aliases here...
56const aliases: Record<string, string> = {
57 // System
58 and: 'simple-icons-android',
59 win: 'simple-icons-windows',
60 app: 'simple-icons-apple',
61 lin: 'simple-icons-linux',
62 bsd: 'simple-icons-freebsd',
63 cmd: 'material-symbols-terminal-rounded',
64 ff: 'simple-icons-firefoxbrowser',
65 cr: 'simple-icons-googlechrome',
66 web: 'mdi-web',
67
68 // Type
69 strm: 'material-symbols-play-circle-outline',
70 ddl: 'material-symbols-cloud-download-outline-rounded',
71 mag: 'lucide-magnet',
72 lcl: 'iconoir-floppy-disk',
73 batch: 'material-symbols-package-2-outline',
74 sp: `ri-file-4-line`,
75 dp: `ri-book-open-line`,
76 ls: `ri-file-paper-2-line`,
77
78 // Price
79 paid: 'ic-round-attach-money',
80 fm: 'ic-round-add-shopping-cart',
81 sub: 'ic-round-currency-exchange',
82 coin: 'akar-icons-coin',
83
84 // Source
85 gh: 'simple-icons-github',
86 gl: 'simple-icons-gitlab',
87 cb: 'simple-icons-codeberg',
88 kde: 'simple-icons-kde',
89 gn: 'simple-icons-gnome',
90 gt: 'simple-icons-gitea',
91 cs: 'material-symbols-lock-outline',
92
93 // Platform
94 d: 'simple-icons-discord',
95 f: 'simple-icons-4chan',
96 x: 'simple-icons-twitter',
97 tg: 'simple-icons-telegram',
98 mal: 'simple-icons-myanimelist',
99 al: 'simple-icons-anilist',
100 k: 'simple-icons-kitsu',
101 simkl: 'simple-icons-simkl',
102
103 // Storage
104 sgd: 'simple-icons-googledrive',
105 sm: 'simple-icons-mega',
106 smf: 'simple-icons-mediafire',
107 host: 'mdi-snail',
108
109 // Flag
110 fjp: 'twemoji-flag-japan',
111 fkr: 'twemoji-flag-south-korea',
112 fcn: 'twemoji-flag-china',
113
114 // Code
115 py: 'simple-icons-python',
116 js: 'simple-icons-javascript',
117 css: 'simple-icons-css',
118 dock: 'simple-icons-docker',
119
120 // Others
121 s: 'twemoji-glowing-star',
122 e: 'mdi-puzzle',
123 n: 'material-symbols-science',
124 alt: 'ic-round-looks-two',
125 ero: 'twemoji-no-one-under-eighteen',
126 acc: 'material-symbols-person-add',
127 rss: 'simple-icons-rss',
128 prx: 'material-symbols-directions-alt',
129 tf: 'mdi-airplane',
130 more: 'mdi-arrow-right-bold',
131 help: 'material-symbols-help',
132 yes: 'twemoji-check-mark-button',
133 no: 'twemoji-cross-mark',
134 global: 'twemoji-globe-showing-asia-australia',
135 cc: 'mdi-closed-caption',
136 hs: 'mdi-hulu'
137}
138
139const defs: Record<string, string> = {}
140
141for (const elem of emojis) {
142 for (const key in elem.pack.icons) {
143 if (elem.prefix) defs[elem.prefix + key] = ''
144 else defs[key] = ''
145 }
146}
147
148for (const [alias, fullName] of Object.entries(aliases)) {
149 defs[alias] = defs[fullName] !== undefined ? '' : 'INVALID_ALIAS'
150}
151
152export { defs, aliases }
153
154export function emojiRender(md: MarkdownRenderer) {
155 md.renderer.rules.emoji = (tokens, idx) => {
156 const markup = tokens[idx].markup
157 if (aliases[markup]) {
158 return `<span class="i-${aliases[markup]}"></span>`
159 }
160
161 for (const emoji of emojis) {
162 if (markup.startsWith(emoji.prefix!)) {
163 return `<span class="i-${markup}"></span>`
164 }
165 }
166
167 return `<span class="i-twemoji-${markup}"></span>`
168 }
169}
170
171export function movePlugin(
172 plugins: { name: string }[],
173 pluginAName: string,
174 order: 'before' | 'after',
175 pluginBName: string
176) {
177 const pluginBIndex = plugins.findIndex((p) => p.name === pluginBName)
178 if (pluginBIndex === -1) return
179
180 const pluginAIndex = plugins.findIndex((p) => p.name === pluginAName)
181 if (pluginAIndex === -1) return
182
183 if (order === 'before' && pluginAIndex > pluginBIndex) {
184 const pluginA = plugins.splice(pluginAIndex, 1)[0]
185 plugins.splice(pluginBIndex, 0, pluginA)
186 }
187
188 if (order === 'after' && pluginAIndex < pluginBIndex) {
189 const pluginA = plugins.splice(pluginAIndex, 1)[0]
190 plugins.splice(pluginBIndex, 0, pluginA)
191 }
192}