mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
Improve emoji naming and custom emoji UX (#878)
Signed-off-by: Wes <wesbillman@users.noreply.github.com> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co>
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import assert from "node:assert/strict";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
|
||||
import {
|
||||
customEmojiFromEvent,
|
||||
customEmojiFromTags,
|
||||
normalizeShortcode,
|
||||
suggestShortcodeFromFilename,
|
||||
} from "./customEmoji.ts";
|
||||
|
||||
function ev(tags) {
|
||||
@@ -83,6 +85,22 @@ test("normalizeShortcode rejects invalid chars and empties", () => {
|
||||
assert.equal(normalizeShortcode(""), null);
|
||||
});
|
||||
|
||||
test("suggestShortcodeFromFilename derives a valid name from common filenames", () => {
|
||||
assert.equal(
|
||||
suggestShortcodeFromFilename("Party Parrot.gif"),
|
||||
"party_parrot",
|
||||
);
|
||||
assert.equal(
|
||||
suggestShortcodeFromFilename("ship-it.final.PNG"),
|
||||
"ship-it_final",
|
||||
);
|
||||
assert.equal(
|
||||
suggestShortcodeFromFilename(path.join("tmp", "Narf! Zort.webp")),
|
||||
"narf_zort",
|
||||
);
|
||||
assert.equal(suggestShortcodeFromFilename("---.png"), null);
|
||||
});
|
||||
|
||||
test("customEmojiFromTags normalizes shortcodes (case-fold)", () => {
|
||||
const out = customEmojiFromTags([["emoji", "ShipIt", "https://relay/s.png"]]);
|
||||
assert.deepEqual(out, [{ shortcode: "shipit", url: "https://relay/s.png" }]);
|
||||
|
||||
@@ -54,6 +54,24 @@ export function normalizeShortcode(raw: string): string | null {
|
||||
return SHORTCODE_RE.test(lower) ? lower : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Suggest a valid custom-emoji shortcode from an uploaded filename.
|
||||
* Mirrors Slack's file-first flow: strip the extension, lowercase, and collapse
|
||||
* runs of invalid characters into a single underscore.
|
||||
*/
|
||||
export function suggestShortcodeFromFilename(filename: string): string | null {
|
||||
const basename = filename
|
||||
.trim()
|
||||
.replace(/^.*[/\\]/, "")
|
||||
.replace(/\.[^.]*$/, "");
|
||||
const suggested = basename
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9_-]+/g, "_")
|
||||
.replace(/_+/g, "_")
|
||||
.replace(/^[_-]+|[_-]+$/g, "");
|
||||
return normalizeShortcode(suggested);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse NIP-30 `["emoji", shortcode, url]` tags from a single event into a
|
||||
* custom-emoji list. Shortcodes are normalized; malformed/duplicate entries
|
||||
|
||||
@@ -761,7 +761,7 @@ export type BlobDescriptor = {
|
||||
thumb?: string;
|
||||
duration?: number;
|
||||
image?: string;
|
||||
/** Original filename for generic (non-media) file attachments. */
|
||||
/** Original filename captured client-side. */
|
||||
filename?: string;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user