m->d bridge known emojis in messages
This commit is contained in:
		
							parent
							
								
									b403f35674
								
							
						
					
					
						commit
						e44918b4c7
					
				
					 3 changed files with 78 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -117,6 +117,28 @@ turndownService.addRule("inlineLink", {
 | 
			
		|||
	}
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
turndownService.addRule("emoji", {
 | 
			
		||||
	filter: function (node, options) {
 | 
			
		||||
		if (node.nodeName !== "IMG" || !node.hasAttribute("data-mx-emoticon") || !node.getAttribute("src")) return false
 | 
			
		||||
		const row = select("emoji", ["emoji_id", "animated"], "WHERE mxc_url = ?").get(node.getAttribute("src"))
 | 
			
		||||
		if (!row) return false
 | 
			
		||||
		node.setAttribute("data-emoji-id", row.emoji_id)
 | 
			
		||||
		node.setAttribute("data-emoji-animated-char", row.animated ? "a" : "")
 | 
			
		||||
		return true
 | 
			
		||||
	},
 | 
			
		||||
 | 
			
		||||
	replacement: function (content, node) {
 | 
			
		||||
		/** @type {string} */
 | 
			
		||||
		const id = node.getAttribute("data-emoji-id")
 | 
			
		||||
		/** @type {string} */
 | 
			
		||||
		const animatedChar = node.getAttribute("data-emoji-animated-char")
 | 
			
		||||
		/** @type {string} */
 | 
			
		||||
		const title = node.getAttribute("title") || "__"
 | 
			
		||||
		const name = title.replace(/^:|:$/g, "")
 | 
			
		||||
		return `<${animatedChar}:${name}:${id}>`
 | 
			
		||||
	}
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
turndownService.addRule("fencedCodeBlock", {
 | 
			
		||||
	filter: function (node, options) {
 | 
			
		||||
		return (
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1692,3 +1692,55 @@ test("event2message: stickers work", async t => {
 | 
			
		|||
		}
 | 
			
		||||
	)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("event2message: static emojis work", async t => {
 | 
			
		||||
	t.deepEqual(
 | 
			
		||||
		await eventToMessage({
 | 
			
		||||
			type: "m.room.message",
 | 
			
		||||
			sender: "@cadence:cadence.moe",
 | 
			
		||||
			content: {
 | 
			
		||||
				msgtype: "m.text",
 | 
			
		||||
				body: ":hippo:",
 | 
			
		||||
				format: "org.matrix.custom.html",
 | 
			
		||||
				formatted_body: '<img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/qWmbXeRspZRLPcjseyLmeyXC\" title=\":hippo:\" alt=\":hippo:\">'
 | 
			
		||||
			},
 | 
			
		||||
			event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU",
 | 
			
		||||
			room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe"
 | 
			
		||||
		}),
 | 
			
		||||
		{
 | 
			
		||||
			messagesToDelete: [],
 | 
			
		||||
			messagesToEdit: [],
 | 
			
		||||
			messagesToSend: [{
 | 
			
		||||
				username: "cadence [they]",
 | 
			
		||||
				content: "<:hippo:230201364309868544>",
 | 
			
		||||
				avatar_url: undefined
 | 
			
		||||
			}]
 | 
			
		||||
		}
 | 
			
		||||
	)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("event2message: animated emojis work", async t => {
 | 
			
		||||
	t.deepEqual(
 | 
			
		||||
		await eventToMessage({
 | 
			
		||||
			type: "m.room.message",
 | 
			
		||||
			sender: "@cadence:cadence.moe",
 | 
			
		||||
			content: {
 | 
			
		||||
				msgtype: "m.text",
 | 
			
		||||
				body: ":hippo:",
 | 
			
		||||
				format: "org.matrix.custom.html",
 | 
			
		||||
				formatted_body: '<img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/WbYqNlACRuicynBfdnPYtmvc\" title=\":hipposcope:\" alt=\":hipposcope:\">'
 | 
			
		||||
			},
 | 
			
		||||
			event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU",
 | 
			
		||||
			room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe"
 | 
			
		||||
		}),
 | 
			
		||||
		{
 | 
			
		||||
			messagesToDelete: [],
 | 
			
		||||
			messagesToEdit: [],
 | 
			
		||||
			messagesToSend: [{
 | 
			
		||||
				username: "cadence [they]",
 | 
			
		||||
				content: "<a:hipposcope:393635038903926784>",
 | 
			
		||||
				avatar_url: undefined
 | 
			
		||||
			}]
 | 
			
		||||
		}
 | 
			
		||||
	)
 | 
			
		||||
})
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue