From ef2ea41d63b8990e533cd90c7894cca019295145 Mon Sep 17 00:00:00 2001 From: Salihu Date: Sat, 15 Nov 2025 16:42:38 +0100 Subject: [PATCH] serialize custom emojis like regular images --- app/editor/components/EmojiMenu.tsx | 1 - shared/editor/components/Styles.ts | 7 ++++++ shared/editor/nodes/Emoji.tsx | 20 +++------------- shared/editor/rules/emoji.ts | 36 +---------------------------- 4 files changed, 11 insertions(+), 53 deletions(-) diff --git a/app/editor/components/EmojiMenu.tsx b/app/editor/components/EmojiMenu.tsx index 71b3f50346..d6cd4f8967 100644 --- a/app/editor/components/EmojiMenu.tsx +++ b/app/editor/components/EmojiMenu.tsx @@ -45,7 +45,6 @@ const EmojiMenu = (props: Props) => { markup: shortcode, "data-name": !isInternalUrl(emoji) ? shortcode : item.name, "data-url": isInternalUrl(emoji) ? emoji : undefined, - type: isInternalUrl(emoji) ? "custom" : "emoji", }, }; }) diff --git a/shared/editor/components/Styles.ts b/shared/editor/components/Styles.ts index ac3353d045..3945deb6bf 100644 --- a/shared/editor/components/Styles.ts +++ b/shared/editor/components/Styles.ts @@ -828,6 +828,13 @@ h6:not(.placeholder)::before { margin-${props.rtl ? "right" : "left"}: -1em; } +.emoji.custom-emoji { + width: 1.2em; + height: 1.2em; + vertical-align: text-bottom; + display: inline-block; +} + .heading-anchor, .heading-fold { display: inline-block; diff --git a/shared/editor/nodes/Emoji.tsx b/shared/editor/nodes/Emoji.tsx index a9cfcf6493..43422ca2d0 100644 --- a/shared/editor/nodes/Emoji.tsx +++ b/shared/editor/nodes/Emoji.tsx @@ -33,7 +33,6 @@ export default class Emoji extends Extension { default: null, validate: (url: string) => !url || isInternalUrl(url), }, - type: { default: "emoji", validate: "string" }, }, inline: true, content: "text*", @@ -55,17 +54,14 @@ export default class Emoji extends Extension { toDOM: (node) => { const name = node.attrs["data-name"]; const url = node.attrs["data-url"]; - const type = node.attrs.type; - if (type === "custom" && !!url) { + if (url) { return [ "img", { class: `emoji custom-emoji ${name}`, "data-name": name, src: url, - style: - "width: 1.2em; height: 1.2em; vertical-align: text-bottom; display: inline-block;", }, ]; } else { @@ -117,7 +113,7 @@ export default class Emoji extends Extension { const escapedAlt = state.esc(alt, false); const escapedUrl = state.esc(url, false); - const markdown = `${prefix}![:${escapedAlt}:](${escapedUrl} "custom-emoji")`; + const markdown = `${prefix}![${escapedAlt}](${escapedUrl})`; state.write(markdown); } else if (name) { state.write(`:${name}:`); @@ -127,17 +123,7 @@ export default class Emoji extends Extension { parseMarkdown() { return { node: "emoji", - getAttrs: (tok: Token) => { - const url = tok.attrGet("data-url"); - if (url && isInternalUrl(url)) { - return { - "data-name": tok.markup.trim(), - "data-url": tok.attrGet("data-url"), - type: "custom", - }; - } - return { "data-name": tok.markup.trim() }; - }, + getAttrs: (tok: Token) => ({ "data-name": tok.markup.trim() }), }; } } diff --git a/shared/editor/rules/emoji.ts b/shared/editor/rules/emoji.ts index 75de9de095..35913f0dcc 100644 --- a/shared/editor/rules/emoji.ts +++ b/shared/editor/rules/emoji.ts @@ -1,4 +1,4 @@ -import MarkdownIt, { StateCore } from "markdown-it"; +import MarkdownIt from "markdown-it"; import { full as emojiPlugin } from "markdown-it-emoji"; import { nameToEmoji } from "../lib/emoji"; @@ -6,41 +6,7 @@ type Options = MarkdownIt.Options & { emoji: boolean; }; -function customEmojiRule(state: StateCore) { - const tokens = state.tokens; - - for (let i = 0; i < tokens.length; i++) { - const token = tokens[i]; - - if (token.type === "inline" && token.children) { - for (let j = 0; j < token.children.length; j++) { - const childToken = token.children[j]; - - if (childToken.type === "image") { - const title = childToken.attrGet("title"); - - if (title === "custom-emoji") { - const emojiName = childToken.content || ""; - const emojiUrl = childToken.attrGet("src") || ""; - - const emojiToken = new state.Token("emoji", "", 0); - emojiToken.markup = emojiName; - emojiToken.attrSet("data-name", emojiName); - emojiToken.attrSet("data-url", emojiUrl); - - token.children[j] = emojiToken; - } - } - } - } - } - - return false; -} - export default function emoji(md: MarkdownIt) { - md.core.ruler.after("inline", "custom_emoji", customEmojiRule); - // Ideally this would be an empty object, but due to a bug in markdown-it-emoji // passing an empty object results in newlines becoming emojis. Until this is // fixed at least one key is required. See: