m2d emoji roundtripping #110
2 changed files with 12 additions and 12 deletions
sprite sheet emote roundtripping without visible names
commit
7aa1f5870c
|
|
@ -143,16 +143,14 @@ const contentHtmlOutput = markdown.markdownEngine.htmlFor(markdown.markdownEngin
|
|||
html: function(node, output, state) {
|
||||
// first, check if this looks like a m->d bridged emoji
|
||||
if (
|
||||
!node.target.startsWith(`${reg.ooye.bridge_origin}/download/`)
|
||||
|| node.content.length != 1
|
||||
node.content.length != 1
|
||||
|| 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
|
||||
return markdown.markdownEngine.defaultRules.link.html(node, output, state)
|
||||
}
|
||||
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
|
||||
try {
|
||||
parsedUrl = new URL(node.target)
|
||||
|
|
@ -164,7 +162,7 @@ const contentHtmlOutput = markdown.markdownEngine.htmlFor(markdown.markdownEngin
|
|||
// no emotes, fall back
|
||||
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 = []
|
||||
for (const mxc of parsedUrl.searchParams.getAll("e")) {
|
||||
const name = emojiNames.shift()
|
||||
|
|
|
|||
|
|
@ -361,18 +361,19 @@ function getUserOrProxyOwnerMention(mxid) {
|
|||
function linkEndOfMessageSpriteSheet(content) {
|
||||
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
|
||||
const r = /<[a!]?(:[a-zA-Z0-9_]*:)[0-9]*>\s*$/
|
||||
const emoteNames = []
|
||||
const r = /<[a!]?:([a-zA-Z0-9_]*):[0-9]*>\s*$/
|
||||
const emojiNames = []
|
||||
|
||||
let match
|
||||
while (match = content.match(r)) {
|
||||
emoteNames.unshift(match[1])
|
||||
emojiNames.unshift(match[1])
|
||||
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.
|
||||
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 = ")"
|
||||
|
||||
// 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.
|
||||
const withoutMxc = mxUtils.makeMxcPublic(mxc)
|
||||
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) {
|
||||
break
|
||||
}
|
||||
params.append("e", withoutMxc)
|
||||
params.append("n", emojiName)
|
||||
}
|
||||
|
||||
const url = `${reg.ooye.bridge_origin}/download/sheet?${params.toString()}`
|
||||
return content + url + afterLink
|
||||
return content + params.toString() + afterLink
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue