Socialify

Folder ..

Viewing helpers.ts
174 lines (158 loc) • 3.9 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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { Pattern, Theme } from './types/configType'
import {
  siGithub,
  siC,
  siCsharp,
  siCplusplus,
  siCoffeescript,
  siCss3,
  siGo,
  siApachegroovy,
  siHtml5,
  siOpenjdk,
  siJavascript,
  siJupyter,
  siPhp,
  siPython,
  siRuby,
  siRust,
  siScala,
  siSwift,
  siTypescript,
  siSvelte,
  siHaskell,
  siKotlin,
  siDocker,
  siGnubash,
  siVuedotjs,
  siNginx,
  siDart,
  siLua,
  siDm,
  siPerl,
  siOcaml,
  siClojure,
  siPowershell,
  siErlang,
  siJulia,
  siWebassembly,
  siPuppet,
  siElixir
} from 'simple-icons/icons'
import {
  signal,
  charlieBrown,
  formalInvitation,
  plus,
  circuitBoard,
  overlappingHexagons,
  brickWall,
  floatingCogs,
  diagonalStripes
} from 'hero-patterns'

const LANGUAGE_ICON_MAPPING: { [key: string]: any } = {
  GitHub: siGithub,
  C: siC,
  'C#': siCsharp,
  'C++': siCplusplus,
  CoffeeScript: siCoffeescript,
  CSS: siCss3,
  Go: siGo,
  Groovy: siApachegroovy,
  HTML: siHtml5,
  Java: siOpenjdk,
  JavaScript: siJavascript,
  'Jupyter Notebook': siJupyter,
  PHP: siPhp,
  Python: siPython,
  Ruby: siRuby,
  Rust: siRust,
  Scala: siScala,
  Swift: siSwift,
  TypeScript: siTypescript,
  Svelte: siSvelte,
  Haskell: siHaskell,
  Kotlin: siKotlin,
  Dockerfile: siDocker,
  Shell: siGnubash,
  Vue: siVuedotjs,
  Nginx: siNginx,
  Dart: siDart,
  Lua: siLua,
  DM: siDm,
  Perl: siPerl,
  OCaml: siOcaml,
  Clojure: siClojure,
  PowerShell: siPowershell,
  Erlang: siErlang,
  Julia: siJulia,
  WebAssembly: siWebassembly,
  Puppet: siPuppet,
  Elixir: siElixir
}

const getSimpleIconsImageURI = function (language: string, theme: Theme) {
  const icon = LANGUAGE_ICON_MAPPING[language]
  if (!icon) return undefined

  const iconColor =
    language === 'GitHub' && theme === Theme.dark ? '#fff' : `#${icon.hex}`

  const iconSvg = icon.svg.replace('<svg ', `<svg fill="${iconColor}" `)

  return `data:image/svg+xml,${encodeURIComponent(iconSvg)}`
}

const getHeroPattern = (pattern: Pattern, theme: Theme) => {
  const PATTERN_FUNCTIONS_MAPPING: { [key: string]: any } = {
    [Pattern.signal]: signal,
    [Pattern.charlieBrown]: charlieBrown,
    [Pattern.formalInvitation]: formalInvitation,
    [Pattern.plus]: plus,
    [Pattern.circuitBoard]: circuitBoard,
    [Pattern.overlappingHexagons]: overlappingHexagons,
    [Pattern.brickWall]: brickWall,
    [Pattern.floatingCogs]: floatingCogs,
    [Pattern.diagonalStripes]: diagonalStripes,
    [Pattern.solid]: null
  }
  const patternFunction = PATTERN_FUNCTIONS_MAPPING[pattern]
  const themedBackgroundColor = theme === Theme.dark ? '#000' : '#fff'

  if (!patternFunction) {
    return {
      backgroundColor: themedBackgroundColor
    }
  }

  const darkThemeArgs = ['#eaeaea', 0.2]
  const lightThemeArgs = ['#eaeaea', 0.6]
  let patternImageUrl = patternFunction.apply(
    null,
    theme === Theme.dark ? darkThemeArgs : lightThemeArgs
  )

  const width = patternImageUrl.match(/width%3D%22(\d+)%22/)?.[1]
  const height = patternImageUrl.match(/height%3D%22(\d+)%22/)?.[1]

  // Satori has issues with quotes around data uris, therefore we are stripping the quotes
  patternImageUrl = patternImageUrl
    .replace(/^url\('/, 'url(')
    .replace(/'\)$/, ')')

  return {
    backgroundColor: themedBackgroundColor,
    backgroundImage: patternImageUrl,
    backgroundSize: `${width}px ${height}px`,
    backgroundRepeat: 'repeat'
  }
}

let webpSupport: boolean | undefined
const checkWebpSupport = (): boolean => {
  if (webpSupport !== undefined) {
    return webpSupport
  }

  webpSupport = (() => {
    try {
      const canvas = document.createElement('canvas')
      return canvas.toDataURL('image/webp').indexOf('data:image/webp') === 0
    } catch (e) {
      return false
    }
  })()

  return webpSupport
}

const HOST_PREFIX = process.env.VERCEL_URL
  ? `https://${process.env.VERCEL_URL}`
  : process.env.PROJECT_URL || ''

export { getSimpleIconsImageURI, getHeroPattern, checkWebpSupport, HOST_PREFIX }