mirror of
https://github.com/block/buzz.git
synced 2026-10-31 14:31:02 +00:00
Signed-off-by: Wes <wesbillman@users.noreply.github.com> Signed-off-by: tlongwell-block <109685178+tlongwell-block@users.noreply.github.com> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co> Co-authored-by: tlongwell-block <109685178+tlongwell-block@users.noreply.github.com>
102 lines
3.5 KiB
JavaScript
102 lines
3.5 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import remarkCustomEmoji from "./remarkCustomEmoji.ts";
|
|
|
|
const EMOJI = [
|
|
{ shortcode: "party_parrot", url: "https://relay/blob/parrot.gif" },
|
|
{ shortcode: "party", url: "https://relay/blob/party.png" },
|
|
];
|
|
|
|
function runPlugin(tree, emoji = EMOJI) {
|
|
remarkCustomEmoji({ customEmoji: emoji })(tree);
|
|
return tree;
|
|
}
|
|
|
|
function paragraph(...children) {
|
|
return { type: "root", children: [{ type: "paragraph", children }] };
|
|
}
|
|
function text(value) {
|
|
return { type: "text", value };
|
|
}
|
|
function kids(tree) {
|
|
return tree.children[0].children;
|
|
}
|
|
|
|
test("known shortcode becomes an emoji node with src/alt", () => {
|
|
const k = kids(runPlugin(paragraph(text(":party_parrot:"))));
|
|
assert.equal(k.length, 1);
|
|
assert.equal(k[0].type, "emoji");
|
|
assert.equal(k[0].data.hName, "emoji");
|
|
assert.equal(k[0].data.hProperties.src, "https://relay/blob/parrot.gif");
|
|
assert.equal(k[0].data.hProperties.alt, ":party_parrot:");
|
|
assert.equal(k[0].data.hProperties["data-shortcode"], "party_parrot");
|
|
});
|
|
|
|
test("unknown shortcode stays plain text", () => {
|
|
const k = kids(runPlugin(paragraph(text(":thinking:"))));
|
|
assert.equal(k.length, 1);
|
|
assert.equal(k[0].type, "text");
|
|
assert.equal(k[0].value, ":thinking:");
|
|
});
|
|
|
|
test("mid-sentence shortcode splits surrounding text", () => {
|
|
const k = kids(runPlugin(paragraph(text("hi :party_parrot: there"))));
|
|
assert.equal(k.length, 3);
|
|
assert.equal(k[0].value, "hi ");
|
|
assert.equal(k[1].type, "emoji");
|
|
assert.equal(k[2].value, " there");
|
|
});
|
|
|
|
test("two custom emoji in one text node both replaced", () => {
|
|
const k = kids(runPlugin(paragraph(text(":party: and :party_parrot:"))));
|
|
const emoji = k.filter((c) => c.type === "emoji");
|
|
assert.equal(emoji.length, 2);
|
|
assert.equal(emoji[0].data.hProperties["data-shortcode"], "party");
|
|
assert.equal(emoji[1].data.hProperties["data-shortcode"], "party_parrot");
|
|
});
|
|
|
|
test("longest-first: :party_parrot: is not shadowed by :party:", () => {
|
|
const k = kids(runPlugin(paragraph(text(":party_parrot:"))));
|
|
assert.equal(k.length, 1);
|
|
assert.equal(k[0].data.hProperties["data-shortcode"], "party_parrot");
|
|
});
|
|
|
|
test("shortcodes inside inline code are left untouched", () => {
|
|
const tree = {
|
|
type: "root",
|
|
children: [
|
|
{
|
|
type: "paragraph",
|
|
children: [{ type: "inlineCode", value: ":party:" }],
|
|
},
|
|
],
|
|
};
|
|
remarkCustomEmoji({ customEmoji: EMOJI })(tree);
|
|
const codeNode = tree.children[0].children[0];
|
|
assert.equal(codeNode.type, "inlineCode");
|
|
assert.equal(codeNode.value, ":party:");
|
|
});
|
|
|
|
test("empty emoji map is a no-op (no matching, text preserved)", () => {
|
|
const k = kids(runPlugin(paragraph(text(":party:")), []));
|
|
assert.equal(k.length, 1);
|
|
assert.equal(k[0].type, "text");
|
|
assert.equal(k[0].value, ":party:");
|
|
});
|
|
|
|
test("plain text without shortcodes is unchanged", () => {
|
|
const k = kids(runPlugin(paragraph(text("just a normal sentence"))));
|
|
assert.equal(k.length, 1);
|
|
assert.equal(k[0].type, "text");
|
|
assert.equal(k[0].value, "just a normal sentence");
|
|
});
|
|
|
|
test("mixed-case :Party_Parrot: renders via the lowercase set key", () => {
|
|
const k = kids(runPlugin(paragraph(text("yo :Party_Parrot: yo"))));
|
|
const emoji = k.find((c) => c.type === "emoji");
|
|
assert.ok(emoji, "expected an emoji node for mixed-case shortcode");
|
|
assert.equal(emoji.data.hProperties["data-shortcode"], "party_parrot");
|
|
assert.equal(emoji.data.hProperties.src, "https://relay/blob/parrot.gif");
|
|
});
|