Socialify

Folder ..

Viewing renderCard.ts
64 lines (54 loc) • 1.7 KB

 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { SatoriOptions } from 'satori'
import { Font } from './types/configType'
import QueryType from './types/queryType'
import { mergeConfig } from './configHelper'
import { getRepoDetails } from './github/repoQuery'
import getTwemojiMap from './twemoji'

export async function getFont(
  font: Font,
  weight: SatoriOptions['fonts'][0]['weight']
): Promise<SatoriOptions['fonts'][0]> {
  const fontSlug = font.replace(/\s/g, '-').toLowerCase()
  const cdnUrl = `https://cdn.jsdelivr.net/npm/@fontsource/${fontSlug}/files/${fontSlug}-all-${weight}-normal.woff`

  return {
    name: font,
    data: await fetch(cdnUrl).then((response) => {
      if (response.ok) {
        return response.arrayBuffer()
      }
      throw new Error('Failed to fetch font')
    }),
    weight,
    style: 'normal'
  }
}

export function getFonts(font: Font) {
  return Promise.all([
    getFont(Font.jost, 400),
    getFont(font, 200),
    getFont(font, 400),
    getFont(font, 500)
  ])
}

export async function getEmojiSVG(code: string) {
  return (
    await fetch(`https://twemoji.maxcdn.com/v/13.1.0/svg/${code}.svg`)
  ).text()
}

export async function getGraphemeImages(description: string = '') {
  const emojiCodes = getTwemojiMap(description)
  const emojis = await Promise.all(Object.values(emojiCodes).map(getEmojiSVG))
  const graphemeImages = Object.fromEntries(
    Object.entries(emojiCodes).map(([key], index) => [
      key,
      `data:image/svg+xml;base64,` + btoa(emojis[index])
    ])
  )

  return graphemeImages
}

export async function getCardConfig(query: QueryType) {
  const { repository } = await getRepoDetails(query._owner, query._name)

  const config = mergeConfig(repository, query)

  if (!config) throw Error('Configuration failed to generate')

  return config
}