Skip to content

Commit

Permalink
feat: transform :emoji: in text to unicode emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftinv committed Feb 1, 2024
1 parent 2d91e4f commit 90755ad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
13 changes: 13 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * as log from "https://deno.land/std@0.214.0/log/mod.ts";
export * as hex from "https://deno.land/std@0.214.0/encoding/hex.ts";

export * as httpErrors from "https://deno.land/x/http_errors@3.0.0/mod.ts";
export * as githubEmoji from "https://deno.land/x/github_emoji@v0.1.1/mod.ts";

export * as redis from "https://deno.land/x/redis@v0.32.1/mod.ts";
export { Mutex } from "https://deno.land/x/async@v2.1.0/mod.ts";
Expand Down
21 changes: 18 additions & 3 deletions lib/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { githubEmoji } from "../deps.ts";

// Empirically determined GitHub embed description limit in the Discord API.
// Anything above this will be ellipsized :/
const EMBED_LIMIT = 500;

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

function transformEmojis(s: string): string {
return githubEmoji.emojify(s);
}

const TRANSFORMS: ((s: string) => string)[] = [
transformEmojis,
ellipsizeText,
];

export default function fixupEmbeds(data: Record<string, any>): void {
for (
const field of [
Expand All @@ -31,6 +42,10 @@ export default function fixupEmbeds(data: Record<string, any>): void {
"answer",
]
) {
if (data[field]?.body) data[field].body = maybeTransformText(data[field].body);
if (data[field]?.body) {
for (const transform of TRANSFORMS) {
data[field].body = transform(data[field].body);
}
}
}
}

0 comments on commit 90755ad

Please sign in to comment.