m2d emoji roundtripping #110
1 changed files with 53 additions and 1 deletions
implement roundtripping for m->d emotes
commit
519aedebe6
|
|
@ -136,6 +136,58 @@ const embedTitleParser = markdown.markdownEngine.parserFor({
|
||||||
autolink: undefined,
|
autolink: undefined,
|
||||||
link: undefined
|
link: undefined
|
||||||
})
|
})
|
||||||
|
const contentHtmlOutput = markdown.markdownEngine.htmlFor(markdown.markdownEngine.ruleOutput({
|
||||||
|
...markdown.rules,
|
||||||
|
// special override for the link node that converts any m->d bridged emojis back to proper matrix emotes on the way back
|
||||||
|
link: Object.assign({}, markdown.markdownEngine.defaultRules.link, {
|
||||||
|
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[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?`)) {
|
||||||
|
let parsedUrl
|
||||||
|
try {
|
||||||
|
parsedUrl = new URL(node.target)
|
||||||
|
} catch {
|
||||||
|
// failed to parse url, fall back
|
||||||
|
return markdown.markdownEngine.defaultRules.link.html(node, output, state)
|
||||||
|
}
|
||||||
|
if (!parsedUrl.searchParams.has("e")) {
|
||||||
|
// 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 htmlEmojis = []
|
||||||
|
for (const mxc of parsedUrl.searchParams.getAll("e")) {
|
||||||
|
const name = emojiNames.shift()
|
||||||
|
htmlEmojis.push(
|
||||||
|
name == null || name.length === 0
|
||||||
|
? tag`<img data-mx-emoticon src="mxc://${mxc}" height="32">`
|
||||||
|
: tag`<img data-mx-emoticon src="mxc://${mxc}" alt="${name}" title="${name}" height="32">`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return htmlEmojis.join(" ")
|
||||||
|
// stricter link label regex for single emotes - no spaces/colons allowed
|
||||||
|
} else if (node.target.startsWith(singleEmoteUrlPrefix) && /^:[a-zA-Z0-9_]*:$/.test(node.content[0].content)) {
|
||||||
|
const name = node.content[0].content.slice(1, -1)
|
||||||
|
return name.length === 0
|
||||||
|
? tag`<img data-mx-emoticon src="mxc://${node.target.slice(singleEmoteUrlPrefix.length)}" height="32">`
|
||||||
|
: tag`<img data-mx-emoticon src="mxc://${node.target.slice(singleEmoteUrlPrefix.length)}" alt="${name}" title="${name}" height="32">`
|
||||||
|
} else {
|
||||||
|
// maybe this is not an emote after all lol
|
||||||
|
return markdown.markdownEngine.defaultRules.link.html(node, output, state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}, 'html'))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {{room?: boolean, user_ids?: string[]}} mentions
|
* @param {{room?: boolean, user_ids?: string[]}} mentions
|
||||||
|
|
@ -894,7 +946,7 @@ async function messageToEvent(message, guild, options = {}, di) {
|
||||||
return `<:${name}:${id}>`
|
return `<:${name}:${id}>`
|
||||||
})
|
})
|
||||||
|
|
||||||
const {body, html} = await transformContent(content, {isTheMessageContent: true})
|
const {body, html} = await transformContent(content, {isTheMessageContent: true}, markdown.parser, contentHtmlOutput)
|
||||||
await addTextEvent(body, html, msgtype)
|
await addTextEvent(body, html, msgtype)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue