docs/.vitepress/configs/markdown/emoji.ts
176 lines · 5480 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'
31
32// 1. Install emoji pack with `pnpm add -g @iconify-json/<icon>`
33// 2. Import them like I did above
34// 3. Add it to this emojis array, like I did for octicon, and add a prefix
35const emojis: { pack: IconifyJSON; prefix?: string }[] = [
36 // Default emojis (twemoji)
37 { pack: twemoji },
38 // octicon emojis, prefixed with "octicon-"
39 { pack: octicon, prefix: 'octicon-' },
40 { pack: logos, prefix: 'logos-' },
41 { pack: ic, prefix: 'ic-' },
42 { pack: mingcute, prefix: 'mingcute-' },
43 { pack: mdi, prefix: 'mdi-' },
44 { pack: materials, prefix: 'material-symbols-' },
45 { pack: simple, prefix: 'simple-icons-' },
46 { pack: lucide, prefix: 'lucide-' },
47 { pack: uil, prefix: 'uil-' },
48 { pack: iconoir, prefix: 'iconoir-' }
49]
50
51// Add aliases here...
52const aliases: Record<string, string> = {
53 // System
54 and: 'simple-icons-android',
55 win: 'simple-icons-windows',
56 app: 'simple-icons-apple',
57 lin: 'simple-icons-linux',
58 bsd: 'simple-icons-freebsd',
59 cmd: 'material-symbols-terminal-rounded',
60 ff: 'simple-icons-firefoxbrowser',
61 cr: 'simple-icons-googlechrome',
62 web: 'mdi-web',
63
64 // Type
65 strm: 'material-symbols-play-circle-outline',
66 ddl: 'material-symbols-cloud-download-outline-rounded',
67 mag: 'lucide-magnet',
68 lcl: 'iconoir-floppy-disk',
69 batch: 'material-symbols-package-2-outline',
70
71 // Price
72 paid: 'material-symbols-credit-card-outline',
73 fm: 'material-symbols-add-shopping-cart-rounded',
74
75 // Source
76 gh: 'simple-icons-github',
77 gl: 'simple-icons-gitlab',
78 cb: 'simple-icons-codeberg',
79 kde: 'simple-icons-kde',
80 gn: 'simple-icons-gnome',
81 gt: 'simple-icons-gitea',
82 cs: 'material-symbols-lock-outline',
83
84 // Platform
85 d: 'simple-icons-discord',
86 f: 'simple-icons-4chan',
87 x: 'simple-icons-twitter',
88 tg: 'simple-icons-telegram',
89 mal: 'simple-icons-myanimelist',
90 al: 'simple-icons-anilist',
91 k: 'simple-icons-kitsu',
92 simkl: 'simple-icons-simkl',
93
94 // Storage
95 sgd: 'simple-icons-googledrive',
96 sm: 'simple-icons-mega',
97 smf: 'simple-icons-mediafire',
98 host: 'mdi-snail',
99
100 // Flag
101 fjp: 'twemoji-flag-japan',
102 fkr: 'twemoji-flag-south-korea',
103 fcn: 'twemoji-flag-china',
104
105 // Others
106 s: 'twemoji-glowing-star',
107 e: 'mdi-puzzle',
108 n: 'material-symbols-science',
109 ero: 'twemoji-no-one-under-eighteen',
110 acc: 'material-symbols-person-add',
111 rss: 'simple-icons-rss',
112 prx: 'material-symbols-directions-alt',
113 tf: 'mdi-airplane',
114 js: 'simple-icons-javascript',
115 css: 'mdi-format-paint',
116 more: 'mdi-arrow-right-bold',
117 help: 'material-symbols-help',
118 yes: 'twemoji-check-mark-button',
119 no: 'twemoji-cross-mark',
120 global: 'twemoji-globe-showing-asia-australia'
121}
122
123const defs: Record<string, string> = {}
124
125for (const elem of emojis) {
126 for (const key in elem.pack.icons) {
127 if (elem.prefix) defs[elem.prefix + key] = ''
128 else defs[key] = ''
129 }
130}
131
132for (const [alias, fullName] of Object.entries(aliases)) {
133 defs[alias] = defs[fullName] !== undefined ? '' : 'INVALID_ALIAS'
134}
135
136export { defs, aliases }
137
138export function emojiRender(md: MarkdownRenderer) {
139 md.renderer.rules.emoji = (tokens, idx) => {
140 const markup = tokens[idx].markup
141 if (aliases[markup]) {
142 return `<span class="i-${aliases[markup]}"></span>`
143 }
144
145 for (const emoji of emojis) {
146 if (markup.startsWith(emoji.prefix!)) {
147 return `<span class="i-${markup}"></span>`
148 }
149 }
150
151 return `<span class="i-twemoji-${markup}"></span>`
152 }
153}
154
155export function movePlugin(
156 plugins: { name: string }[],
157 pluginAName: string,
158 order: 'before' | 'after',
159 pluginBName: string
160) {
161 const pluginBIndex = plugins.findIndex((p) => p.name === pluginBName)
162 if (pluginBIndex === -1) return
163
164 const pluginAIndex = plugins.findIndex((p) => p.name === pluginAName)
165 if (pluginAIndex === -1) return
166
167 if (order === 'before' && pluginAIndex > pluginBIndex) {
168 const pluginA = plugins.splice(pluginAIndex, 1)[0]
169 plugins.splice(pluginBIndex, 0, pluginA)
170 }
171
172 if (order === 'after' && pluginAIndex < pluginBIndex) {
173 const pluginA = plugins.splice(pluginAIndex, 1)[0]
174 plugins.splice(pluginBIndex, 0, pluginA)
175 }
176}