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