docs/.vitepress/configs/markdown/emoji.ts
177 lines · 5509 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 alt: 'ic-round-looks-two',
110 ero: 'twemoji-no-one-under-eighteen',
111 acc: 'material-symbols-person-add',
112 rss: 'simple-icons-rss',
113 prx: 'material-symbols-directions-alt',
114 tf: 'mdi-airplane',
115 js: 'simple-icons-javascript',
116 css: 'mdi-format-paint',
117 more: 'mdi-arrow-right-bold',
118 help: 'material-symbols-help',
119 yes: 'twemoji-check-mark-button',
120 no: 'twemoji-cross-mark',
121 global: 'twemoji-globe-showing-asia-australia'
122}
123
124const defs: Record<string, string> = {}
125
126for (const elem of emojis) {
127 for (const key in elem.pack.icons) {
128 if (elem.prefix) defs[elem.prefix + key] = ''
129 else defs[key] = ''
130 }
131}
132
133for (const [alias, fullName] of Object.entries(aliases)) {
134 defs[alias] = defs[fullName] !== undefined ? '' : 'INVALID_ALIAS'
135}
136
137export { defs, aliases }
138
139export function emojiRender(md: MarkdownRenderer) {
140 md.renderer.rules.emoji = (tokens, idx) => {
141 const markup = tokens[idx].markup
142 if (aliases[markup]) {
143 return `<span class="i-${aliases[markup]}"></span>`
144 }
145
146 for (const emoji of emojis) {
147 if (markup.startsWith(emoji.prefix!)) {
148 return `<span class="i-${markup}"></span>`
149 }
150 }
151
152 return `<span class="i-twemoji-${markup}"></span>`
153 }
154}
155
156export function movePlugin(
157 plugins: { name: string }[],
158 pluginAName: string,
159 order: 'before' | 'after',
160 pluginBName: string
161) {
162 const pluginBIndex = plugins.findIndex((p) => p.name === pluginBName)
163 if (pluginBIndex === -1) return
164
165 const pluginAIndex = plugins.findIndex((p) => p.name === pluginAName)
166 if (pluginAIndex === -1) return
167
168 if (order === 'before' && pluginAIndex > pluginBIndex) {
169 const pluginA = plugins.splice(pluginAIndex, 1)[0]
170 plugins.splice(pluginBIndex, 0, pluginA)
171 }
172
173 if (order === 'after' && pluginAIndex < pluginBIndex) {
174 const pluginA = plugins.splice(pluginAIndex, 1)[0]
175 plugins.splice(pluginBIndex, 0, pluginA)
176 }
177}