about summary refs log tree commit diff
path: root/bskyogcard/src/util.ts
blob: 2b86ded06674a57c2cade40666ac94fc4454abd7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import twemoji from 'twemoji'

import {renderLogger} from './logger.js'

const U200D = String.fromCharCode(0x200d)
const UFE0F_REGEXP = /\uFE0F/g

export async function loadEmojiAsSvg(chars: string) {
  const cached = emojiCache.get(chars)
  if (cached) return cached
  const iconCode = twemoji.convert.toCodePoint(
    chars.indexOf(U200D) < 0 ? chars.replace(UFE0F_REGEXP, '') : chars,
  )
  const res = await fetch(getEmojiUrl(iconCode))
  const body = await res.arrayBuffer()
  if (!res.ok) {
    renderLogger.warn(
      {status: res.status, err: Buffer.from(body).toString()},
      'could not fetch emoji',
    )
    return
  }
  const svg =
    'data:image/svg+xml;base64,' + Buffer.from(body).toString('base64')
  emojiCache.set(chars, svg)
  return svg
}

const emojiCache = new Map<string, string>()

function getEmojiUrl(code: string) {
  return (
    'https://cdnjs.cloudflare.com/ajax/libs/twemoji/14.0.2/svg/' +
    code.toLowerCase() +
    '.svg'
  )
}