docs/.vitepress/configs/markdown/emoji.ts

a34183d4cfd38b7799fb222fb400a8dacd7812f9
wotaku/docs/.vitepress/configs/markdown/emoji.ts

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