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

b6014706f31a15765278ed70d472aa2950771ca5
wotaku/docs/.vitepress/configs/markdown/emoji.ts history · blame · raw

179 lines · 5555 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  cc: 'mdi-closed-caption',
123  hs: 'mdi-hulu'
124}
125
126const defs: Record<string, string> = {}
127
128for (const elem of emojis) {
129  for (const key in elem.pack.icons) {
130    if (elem.prefix) defs[elem.prefix + key] = ''
131    else defs[key] = ''
132  }
133}
134
135for (const [alias, fullName] of Object.entries(aliases)) {
136  defs[alias] = defs[fullName] !== undefined ? '' : 'INVALID_ALIAS'
137}
138
139export { defs, aliases }
140
141export function emojiRender(md: MarkdownRenderer) {
142  md.renderer.rules.emoji = (tokens, idx) => {
143    const markup = tokens[idx].markup
144    if (aliases[markup]) {
145      return `<span class="i-${aliases[markup]}"></span>`
146    }
147
148    for (const emoji of emojis) {
149      if (markup.startsWith(emoji.prefix!)) {
150        return `<span class="i-${markup}"></span>`
151      }
152    }
153
154    return `<span class="i-twemoji-${markup}"></span>`
155  }
156}
157
158export function movePlugin(
159  plugins: { name: string }[],
160  pluginAName: string,
161  order: 'before' | 'after',
162  pluginBName: string
163) {
164  const pluginBIndex = plugins.findIndex((p) => p.name === pluginBName)
165  if (pluginBIndex === -1) return
166
167  const pluginAIndex = plugins.findIndex((p) => p.name === pluginAName)
168  if (pluginAIndex === -1) return
169
170  if (order === 'before' && pluginAIndex > pluginBIndex) {
171    const pluginA = plugins.splice(pluginAIndex, 1)[0]
172    plugins.splice(pluginBIndex, 0, pluginA)
173  }
174
175  if (order === 'after' && pluginAIndex < pluginBIndex) {
176    const pluginA = plugins.splice(pluginAIndex, 1)[0]
177    plugins.splice(pluginBIndex, 0, pluginA)
178  }
179}