-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathremark-custom-emojis.js
47 lines (36 loc) · 1.12 KB
/
remark-custom-emojis.js
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
import { findAndReplace } from "mdast-util-find-and-replace";
import { get as getEmoji } from "node-emoji";
import { WebClient } from "@slack/web-api";
const token = process.env.SLACK_TOKEN;
const web = new WebClient(token);
console.log("Fetching custom emojis from Slack...");
const customEmojis = await web.emoji
.list()
.then((res) => {
const { emoji = {} } = res;
const map = new Map();
const getSrc = (url) => (url?.startsWith("alias:") ? getSrc(emoji[url.split(":")[1]]) : url);
Object.entries(emoji).forEach(([name, url]) => {
const src = getSrc(url);
if (src) {
map.set(`:${name}:`, {
type: "html",
value: `<img src='${src.replace(
/'/gu,
"%27",
)}' style='height: 1em;display:inline;' title='${name.replace(/'/gu, "'")}'/>`,
});
}
});
return map;
})
.catch((e) => {
console.error(e);
return new Map();
});
const plugin = () => {
const replaceEmoji = (match) => customEmojis.get(match) ?? getEmoji(match) ?? false;
const transformer = (tree) => findAndReplace(tree, [[/:\+1:|:-1:|:[\w-]+:/g, replaceEmoji]]);
return transformer;
};
export default plugin;