m2d emoji roundtripping #110

Open
mschae wants to merge 3 commits from mschae/out-of-your-element:m2d-emote-roundtrip into main
2 changed files with 12 additions and 12 deletions
Showing only changes of commit 7aa1f5870c - Show all commits

sprite sheet emote roundtripping without visible names

mini-bomba 2026-11-15 00:59:00 +01:00
No known key found for this signature in database
GPG key ID: 30F8B138B4794886

View file

@ -143,16 +143,14 @@ const contentHtmlOutput = markdown.markdownEngine.htmlFor(markdown.markdownEngin
html: function(node, output, state) { html: function(node, output, state) {
// first, check if this looks like a m->d bridged emoji // first, check if this looks like a m->d bridged emoji
if ( if (
!node.target.startsWith(`${reg.ooye.bridge_origin}/download/`) node.content.length != 1
|| node.content.length != 1
|| node.content[0].type !== "text" || node.content[0].type !== "text"
|| !/^:[a-zA-Z0-9_: ]*:$/.test(node.content[0].content)
) { ) {
// not a m->d bridged emote, run the standard link output code // not a m->d bridged emote, run the standard link output code
return markdown.markdownEngine.defaultRules.link.html(node, output, state) return markdown.markdownEngine.defaultRules.link.html(node, output, state)
} }
const singleEmoteUrlPrefix = `${reg.ooye.bridge_origin}/download/matrix/` const singleEmoteUrlPrefix = `${reg.ooye.bridge_origin}/download/matrix/`
if (node.target.startsWith(`${reg.ooye.bridge_origin}/download/sheet?`)) { if (node.target.startsWith(`${reg.ooye.bridge_origin}/download/sheet?`) && node.content[0].content === "\u2800") {
let parsedUrl let parsedUrl
try { try {
parsedUrl = new URL(node.target) parsedUrl = new URL(node.target)
@ -164,7 +162,7 @@ const contentHtmlOutput = markdown.markdownEngine.htmlFor(markdown.markdownEngin
// no emotes, fall back // no emotes, fall back
return markdown.markdownEngine.defaultRules.link.html(node, output, state) return markdown.markdownEngine.defaultRules.link.html(node, output, state)
} }
const emojiNames = Array.from(node.content[0].content.matchAll(/:([^\s:]*):/g).map((m) => m[1])) const emojiNames = parsedUrl.searchParams.getAll("n")
const htmlEmojis = [] const htmlEmojis = []
for (const mxc of parsedUrl.searchParams.getAll("e")) { for (const mxc of parsedUrl.searchParams.getAll("e")) {
const name = emojiNames.shift() const name = emojiNames.shift()

View file

@ -361,18 +361,19 @@ function getUserOrProxyOwnerMention(mxid) {
function linkEndOfMessageSpriteSheet(content) { function linkEndOfMessageSpriteSheet(content) {
if (!/<!:[a-zA-Z0-9_]*:>/.test(content)) return content // No unknown emojis, nothing to do if (!/<!:[a-zA-Z0-9_]*:>/.test(content)) return content // No unknown emojis, nothing to do
// Remove known and unknown emojis from the end of the message // Remove known and unknown emojis from the end of the message
const r = /<[a!]?(:[a-zA-Z0-9_]*:)[0-9]*>\s*$/ const r = /<[a!]?:([a-zA-Z0-9_]*):[0-9]*>\s*$/
const emoteNames = [] const emojiNames = []
let match let match
while (match = content.match(r)) { while (match = content.match(r)) {
emoteNames.unshift(match[1]) emojiNames.unshift(match[1])
content = content.replace(r, "") content = content.replace(r, "")
} }
// Use a markdown link to hide the URL. If this is the only thing in the message, Discord will hide it entirely, same as lone URLs. Good for us. // Use a markdown link to hide the URL. If this is the only thing in the message, Discord will hide it entirely, same as lone URLs. Good for us.
content = content.trimEnd() content = content.trimEnd()
content += ` [${emoteNames.join(" ")}](` content += " [\u2800](" // U+2800 Braille Pattern Blank is invisible on all known platforms but is digitally not a whitespace character
content += `${reg.ooye.bridge_origin}/download/sheet?`
const afterLink = ")" const afterLink = ")"
// Make emojis URL params // Make emojis URL params
@ -381,15 +382,16 @@ function linkEndOfMessageSpriteSheet(content) {
// We can do up to 2000 chars max. (In this maximal case it will get chunked to a separate message.) Ignore additional emojis. // We can do up to 2000 chars max. (In this maximal case it will get chunked to a separate message.) Ignore additional emojis.
const withoutMxc = mxUtils.makeMxcPublic(mxc) const withoutMxc = mxUtils.makeMxcPublic(mxc)
assert(withoutMxc) assert(withoutMxc)
const emojisLength = params.toString().length + encodeURIComponent(withoutMxc).length + 2 const emojiName = emojiNames.shift() ?? ""
const emojisLength = params.toString().length + encodeURIComponent(withoutMxc).length + encodeURIComponent(emojiName).length + 4
if (content.length + emojisLength + afterLink.length > 2000) { if (content.length + emojisLength + afterLink.length > 2000) {
break break
} }
params.append("e", withoutMxc) params.append("e", withoutMxc)
params.append("n", emojiName)
} }
const url = `${reg.ooye.bridge_origin}/download/sheet?${params.toString()}` return content + params.toString() + afterLink
return content + url + afterLink
} }
/** /**