feat: transform `:emoji:` in text to unicode emoji

This commit is contained in:
shiftinv 2024-02-01 17:55:07 +01:00
parent 2d91e4fe3e
commit 90755ad9da
3 changed files with 32 additions and 3 deletions

View File

@ -1,6 +1,17 @@
{
"version": "3",
"remote": {
"https://deno.land/std@0.192.0/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462",
"https://deno.land/std@0.192.0/_util/os.ts": "d932f56d41e4f6a6093d56044e29ce637f8dcc43c5a90af43504a889cf1775e3",
"https://deno.land/std@0.192.0/path/_constants.ts": "e49961f6f4f48039c0dfed3c3f93e963ca3d92791c9d478ac5b43183413136e0",
"https://deno.land/std@0.192.0/path/_interface.ts": "6471159dfbbc357e03882c2266d21ef9afdb1e4aa771b0545e90db58a0ba314b",
"https://deno.land/std@0.192.0/path/_util.ts": "d7abb1e0dea065f427b89156e28cdeb32b045870acdf865833ba808a73b576d0",
"https://deno.land/std@0.192.0/path/common.ts": "ee7505ab01fd22de3963b64e46cff31f40de34f9f8de1fff6a1bd2fe79380000",
"https://deno.land/std@0.192.0/path/glob.ts": "d479e0a695621c94d3fd7fe7abd4f9499caf32a8de13f25073451c6ef420a4e1",
"https://deno.land/std@0.192.0/path/mod.ts": "f065032a7189404fdac3ad1a1551a9ac84751d2f25c431e101787846c86c79ef",
"https://deno.land/std@0.192.0/path/posix.ts": "8b7c67ac338714b30c816079303d0285dd24af6b284f7ad63da5b27372a2c94d",
"https://deno.land/std@0.192.0/path/separator.ts": "0fb679739d0d1d7bf45b68dacfb4ec7563597a902edbaf3c59b50d5bcadd93b1",
"https://deno.land/std@0.192.0/path/win32.ts": "d186344e5583bcbf8b18af416d13d82b35a317116e6460a5a3953508c3de5bba",
"https://deno.land/std@0.211.0/assert/assert.ts": "bec068b2fccdd434c138a555b19a2c2393b71dfaada02b7d568a01541e67cdc5",
"https://deno.land/std@0.211.0/assert/assertion_error.ts": "9f689a101ee586c4ce92f52fa7ddd362e86434ffdf1f848e45987dc7689976b8",
"https://deno.land/std@0.211.0/async/delay.ts": "eab3187eee39ccc8cc76d411fb21fb1801250ddb1090e486d5aec2ace5403391",
@ -52,6 +63,8 @@
"https://deno.land/x/async@v2.1.0/state.ts": "a71692b72371239120b196d0c6a249631bab867dd499e85c422c2e5ec9695999",
"https://deno.land/x/async@v2.1.0/testutil.ts": "5ba512c6f14b7b319f320e149970ca8944115a35f4312b697ae427250d27fb6d",
"https://deno.land/x/async@v2.1.0/wait_group.ts": "e1b4f7f1210c9e8c65d3ee1d8cab8155b33327d3daba79b124ebb54b5ee6d15e",
"https://deno.land/x/github_emoji@v0.1.1/emojis.json": "32ec250b02b7afb313d289df6842e6e57d8001396ea34c30597dc6b1b609e8e5",
"https://deno.land/x/github_emoji@v0.1.1/mod.ts": "4ce30408db7ac4e807afd9fafc25b129a46c8ea63763c3f0ad0af5314c4d07a5",
"https://deno.land/x/http_errors@3.0.0/deps.ts": "7fcd181f9577e1803297e42cdb58ce219d9c7087391b1e9ccc12537e23b9d68c",
"https://deno.land/x/http_errors@3.0.0/mod.ts": "6549778cb6616cd16884dd1f46b28211fc8ac33f9d7141e92a8eaffb48391bc1",
"https://deno.land/x/redis@v0.32.1/backoff.ts": "33e4a6e245f8743fbae0ce583993a671a3ac2ecee433a3e7f0bd77b5dd541d84",

View File

@ -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";

View File

@ -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,
@ -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 [
@ -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);
}
}
}
}