Skip to content

Commit 90755ad

Browse files
committed
feat: transform :emoji: in text to unicode emoji
1 parent 2d91e4f commit 90755ad

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

deno.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * as log from "https://deno.land/[email protected]/log/mod.ts";
22
export * as hex from "https://deno.land/[email protected]/encoding/hex.ts";
33

44
export * as httpErrors from "https://deno.land/x/[email protected]/mod.ts";
5+
export * as githubEmoji from "https://deno.land/x/[email protected]/mod.ts";
56

67
export * as redis from "https://deno.land/x/[email protected]/mod.ts";
78
export { Mutex } from "https://deno.land/x/[email protected]/mod.ts";

lib/formatter.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { githubEmoji } from "../deps.ts";
2+
13
// Empirically determined GitHub embed description limit in the Discord API.
24
// Anything above this will be ellipsized :/
35
const EMBED_LIMIT = 500;
46

5-
function maybeTransformText(s: string): string {
6-
// If length exceeds limit, add backticks since they might otherwise be removed
7+
// If length exceeds limit, add backticks since they might otherwise be removed
8+
function ellipsizeText(s: string): string {
79
const suffix = "…\n```";
810
const maxLen = EMBED_LIMIT - suffix.length;
911
// n.b. this heuristic is by no means perfect; it might add backticks when not necessary,
@@ -14,6 +16,15 @@ function maybeTransformText(s: string): string {
1416
return s;
1517
}
1618

19+
function transformEmojis(s: string): string {
20+
return githubEmoji.emojify(s);
21+
}
22+
23+
const TRANSFORMS: ((s: string) => string)[] = [
24+
transformEmojis,
25+
ellipsizeText,
26+
];
27+
1728
export default function fixupEmbeds(data: Record<string, any>): void {
1829
for (
1930
const field of [
@@ -31,6 +42,10 @@ export default function fixupEmbeds(data: Record<string, any>): void {
3142
"answer",
3243
]
3344
) {
34-
if (data[field]?.body) data[field].body = maybeTransformText(data[field].body);
45+
if (data[field]?.body) {
46+
for (const transform of TRANSFORMS) {
47+
data[field].body = transform(data[field].body);
48+
}
49+
}
3550
}
3651
}

0 commit comments

Comments
 (0)