forked from cutls/OpenSticker
/
createCss.ts
53 lines (52 loc) · 1.78 KB
/
createCss.ts
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
import { IStickerOutPut } from './interfaces/json5.ts'
const decoder = new TextDecoder('utf-8')
const mastodon = decoder.decode(await Deno.readFile(`./template/mastodon.sticker`))
const append = {
mastodon: mastodon
}
const sticker = decoder.decode(await Deno.readFile(`./template/sticker.sticker`))
const twoLetters = {
mastodon: 'md',
pleroma: 'pl',
misskey: 'mi',
misskeylegacy: 'ml',
pixelfed: 'pf',
}
export default function (raw: {updated: string, data: IStickerOutPut[]}, type: 'mastodon') {
const obj = raw.data
let output = ''
for (let i = 0; i < obj.length; i++) {
const target = obj[i]
let { domain, name, bgColor, fontColor, favicon } = target
const type = target.type as 'mastodon' | 'pleroma' | 'misskey' | 'misskeylegacy' | 'pixelfed'
if (!name) name = domain
const tl = twoLetters[type]
let bgColorCSS = `var(--${tl})`
let bgColorTCSS = `var(--${tl}t)`
if (bgColor) {
bgColorCSS = ''
bgColorTCSS = ''
for (let j = 0; j < bgColor.length; j++) {
const bg = bgColor[j]
bgColorCSS = bgColorCSS + bg + ','
bgColorTCSS = bgColorTCSS + bg + ' 84%,'
}
bgColorCSS = `linear-gradient(90deg, ${bgColorCSS} transparent)`
bgColorTCSS = `linear-gradient(90deg, ${bgColorTCSS} transparent)`
}
let isDefault = false
if (favicon == `https://s.0px.io/a/${tl}`) isDefault = true
let faviconCSS = `no-repeat url('${favicon}')`
if (isDefault) faviconCSS = `var(--${tl}f)`
if (!fontColor) fontColor = `var(--${tl}c)`
const str = sticker
.replace(/{{domain}}/g, domain)
.replace(/{{name}}/g, name)
.replace(/{{bg}}/g, bgColorCSS)
.replace(/{{bgt}}/g, bgColorTCSS)
.replace(/{{favicon}}/g, faviconCSS)
.replace(/{{color}}/g, fontColor)
output = output + str
}
return append[type].replace(/{{updated}}/g, raw.updated) + output
}