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

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

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