docs/.vitepress/configs/markdown/emoji.ts
187 lines · 5804 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
75 // Price
76 paid: 'material-symbols-credit-card-outline',
77 fm: 'material-symbols-add-shopping-cart-rounded',
78
79 // Source
80 gh: 'simple-icons-github',
81 gl: 'simple-icons-gitlab',
82 cb: 'simple-icons-codeberg',
83 kde: 'simple-icons-kde',
84 gn: 'simple-icons-gnome',
85 gt: 'simple-icons-gitea',
86 cs: 'material-symbols-lock-outline',
87
88 // Platform
89 d: 'simple-icons-discord',
90 f: 'simple-icons-4chan',
91 x: 'simple-icons-twitter',
92 tg: 'simple-icons-telegram',
93 mal: 'simple-icons-myanimelist',
94 al: 'simple-icons-anilist',
95 k: 'simple-icons-kitsu',
96 simkl: 'simple-icons-simkl',
97
98 // Storage
99 sgd: 'simple-icons-googledrive',
100 sm: 'simple-icons-mega',
101 smf: 'simple-icons-mediafire',
102 host: 'mdi-snail',
103
104 // Flag
105 fjp: 'twemoji-flag-japan',
106 fkr: 'twemoji-flag-south-korea',
107 fcn: 'twemoji-flag-china',
108
109 // Code
110 py: 'simple-icons-python',
111 js: 'simple-icons-javascript',
112 css: 'simple-icons-css',
113 dock: 'simple-icons-docker',
114
115 // Others
116 s: 'twemoji-glowing-star',
117 e: 'mdi-puzzle',
118 n: 'material-symbols-science',
119 alt: 'ic-round-looks-two',
120 ero: 'twemoji-no-one-under-eighteen',
121 acc: 'material-symbols-person-add',
122 rss: 'simple-icons-rss',
123 prx: 'material-symbols-directions-alt',
124 tf: 'mdi-airplane',
125 more: 'mdi-arrow-right-bold',
126 help: 'material-symbols-help',
127 yes: 'twemoji-check-mark-button',
128 no: 'twemoji-cross-mark',
129 global: 'twemoji-globe-showing-asia-australia',
130 cc: 'mdi-closed-caption',
131 hs: 'mdi-hulu'
132}
133
134const defs: Record<string, string> = {}
135
136for (const elem of emojis) {
137 for (const key in elem.pack.icons) {
138 if (elem.prefix) defs[elem.prefix + key] = ''
139 else defs[key] = ''
140 }
141}
142
143for (const [alias, fullName] of Object.entries(aliases)) {
144 defs[alias] = defs[fullName] !== undefined ? '' : 'INVALID_ALIAS'
145}
146
147export { defs, aliases }
148
149export function emojiRender(md: MarkdownRenderer) {
150 md.renderer.rules.emoji = (tokens, idx) => {
151 const markup = tokens[idx].markup
152 if (aliases[markup]) {
153 return `<span class="i-${aliases[markup]}"></span>`
154 }
155
156 for (const emoji of emojis) {
157 if (markup.startsWith(emoji.prefix!)) {
158 return `<span class="i-${markup}"></span>`
159 }
160 }
161
162 return `<span class="i-twemoji-${markup}"></span>`
163 }
164}
165
166export function movePlugin(
167 plugins: { name: string }[],
168 pluginAName: string,
169 order: 'before' | 'after',
170 pluginBName: string
171) {
172 const pluginBIndex = plugins.findIndex((p) => p.name === pluginBName)
173 if (pluginBIndex === -1) return
174
175 const pluginAIndex = plugins.findIndex((p) => p.name === pluginAName)
176 if (pluginAIndex === -1) return
177
178 if (order === 'before' && pluginAIndex > pluginBIndex) {
179 const pluginA = plugins.splice(pluginAIndex, 1)[0]
180 plugins.splice(pluginBIndex, 0, pluginA)
181 }
182
183 if (order === 'after' && pluginAIndex < pluginBIndex) {
184 const pluginA = plugins.splice(pluginAIndex, 1)[0]
185 plugins.splice(pluginBIndex, 0, pluginA)
186 }
187}