-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added emoji support to Satori when generating OG images
- Loading branch information
Showing
2 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Modified version of https://unpkg.com/[email protected]/dist/twemoji.esm.js. | ||
* Ported from https://github.com/vercel/satori/blob/48aea6f812365959c2888a25261c72ce17992c6d/playground/utils/twemoji.ts. | ||
*/ | ||
|
||
/*! Copyright Twitter Inc. and other contributors. Licensed under MIT */ | ||
|
||
const U200D = String.fromCharCode(8205) | ||
const UFE0Fg = /\uFE0F/g | ||
|
||
export function getIconCode(char: string) { | ||
return toCodePoint(char.indexOf(U200D) < 0 ? char.replace(UFE0Fg, "") : char) | ||
} | ||
|
||
function toCodePoint(unicodeSurrogates: string) { | ||
const r = [] | ||
let c = 0, | ||
p = 0, | ||
i = 0 | ||
|
||
while (i < unicodeSurrogates.length) { | ||
c = unicodeSurrogates.charCodeAt(i++) | ||
if (p) { | ||
r.push((65536 + ((p - 55296) << 10) + (c - 56320)).toString(16)) | ||
p = 0 | ||
} else if (55296 <= c && c <= 56319) { | ||
p = c | ||
} else { | ||
r.push(c.toString(16)) | ||
} | ||
} | ||
return r.join("-") | ||
} | ||
|
||
export const apis = { | ||
twemoji: (code: string) => | ||
"https://cdnjs.cloudflare.com/ajax/libs/twemoji/15.1.0/svg/" + code.toLowerCase() + ".svg", | ||
openmoji: "https://cdn.jsdelivr.net/npm/@svgmoji/[email protected]/svg/", | ||
blobmoji: "https://cdn.jsdelivr.net/npm/@svgmoji/[email protected]/svg/", | ||
noto: "https://cdn.jsdelivr.net/gh/svgmoji/svgmoji/packages/svgmoji__noto/svg/", | ||
fluent: (code: string) => | ||
"https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/" + | ||
code.toLowerCase() + | ||
"_color.svg", | ||
fluentFlat: (code: string) => | ||
"https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/" + | ||
code.toLowerCase() + | ||
"_flat.svg", | ||
} | ||
|
||
const emojiCache: Record<string, Promise<any>> = {} | ||
|
||
export function loadEmoji(type: keyof typeof apis, code: string) { | ||
const key = type + ":" + code | ||
if (key in emojiCache) return emojiCache[key] | ||
|
||
if (!type || !apis[type]) { | ||
type = "twemoji" | ||
} | ||
|
||
const api = apis[type] | ||
if (typeof api === "function") { | ||
return (emojiCache[key] = fetch(api(code)).then((r) => r.text())) | ||
} | ||
return (emojiCache[key] = fetch(`${api}${code.toUpperCase()}.svg`).then((r) => r.text())) | ||
} |