fix m->d formatting of quotes and code
This commit is contained in:
		
							parent
							
								
									df651241a5
								
							
						
					
					
						commit
						39fb4465f6
					
				
					 2 changed files with 90 additions and 0 deletions
				
			
		| 
						 | 
					@ -33,6 +33,40 @@ turndownService.addRule("strikethrough", {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					turndownService.addRule("blockquote", {
 | 
				
			||||||
 | 
						filter: "blockquote",
 | 
				
			||||||
 | 
						replacement: function (content) {
 | 
				
			||||||
 | 
							content = content.replace(/^\n+|\n+$/g, "")
 | 
				
			||||||
 | 
							content = content.replace(/^/gm, "> ")
 | 
				
			||||||
 | 
							return content
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					turndownService.addRule("fencedCodeBlock", {
 | 
				
			||||||
 | 
						filter: function (node, options) {
 | 
				
			||||||
 | 
							return (
 | 
				
			||||||
 | 
								options.codeBlockStyle === "fenced" &&
 | 
				
			||||||
 | 
								node.nodeName === "PRE" &&
 | 
				
			||||||
 | 
								node.firstChild &&
 | 
				
			||||||
 | 
								node.firstChild.nodeName === "CODE"
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
						replacement: function (content, node, options) {
 | 
				
			||||||
 | 
							const className = node.firstChild.getAttribute("class") || ""
 | 
				
			||||||
 | 
							const language = (className.match(/language-(\S+)/) || [null, ""])[1]
 | 
				
			||||||
 | 
							const code = node.firstChild
 | 
				
			||||||
 | 
							const visibleCode = code.childNodes.map(c => c.nodeName === "BR" ? "\n" : c.textContent).join("").replace(/\n*$/g, "")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var fence = "```"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return (
 | 
				
			||||||
 | 
								fence + language + "\n" +
 | 
				
			||||||
 | 
								visibleCode +
 | 
				
			||||||
 | 
								"\n" + fence
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @param {Ty.Event.Outer<Ty.Event.M_Room_Message>} event
 | 
					 * @param {Ty.Event.Outer<Ty.Event.M_Room_Message>} event
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
| 
						 | 
					@ -61,9 +95,13 @@ function eventToMessage(event) {
 | 
				
			||||||
		// input = input.replace(/ /g, " ")
 | 
							// input = input.replace(/ /g, " ")
 | 
				
			||||||
		// There is also a corresponding test to uncomment, named "event2message: whitespace is retained"
 | 
							// There is also a corresponding test to uncomment, named "event2message: whitespace is retained"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Element adds a bunch of <br> before </blockquote> but doesn't render them. I can't figure out how this works, so let's just delete those.
 | 
				
			||||||
 | 
							input = input.replace(/(?:\n|<br ?\/?>\s*)*<\/blockquote>/g, "</blockquote>")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// The matrix spec hasn't decided whether \n counts as a newline or not, but I'm going to count it, because if it's in the data it's there for a reason.
 | 
							// The matrix spec hasn't decided whether \n counts as a newline or not, but I'm going to count it, because if it's in the data it's there for a reason.
 | 
				
			||||||
		// But I should not count it if it's between block elements.
 | 
							// But I should not count it if it's between block elements.
 | 
				
			||||||
		input = input.replace(/(<\/?([^ >]+)[^>]*>)?\n(<\/?([^ >]+)[^>]*>)?/g, (whole, beforeContext, beforeTag, afterContext, afterTag) => {
 | 
							input = input.replace(/(<\/?([^ >]+)[^>]*>)?\n(<\/?([^ >]+)[^>]*>)?/g, (whole, beforeContext, beforeTag, afterContext, afterTag) => {
 | 
				
			||||||
 | 
								// console.error(beforeContext, beforeTag, afterContext, afterTag)
 | 
				
			||||||
			if (typeof beforeTag !== "string" && typeof afterTag !== "string") {
 | 
								if (typeof beforeTag !== "string" && typeof afterTag !== "string") {
 | 
				
			||||||
				return "<br>"
 | 
									return "<br>"
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -271,6 +271,58 @@ test("event2message: code blocks work", t => {
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("event2message: code block contents are formatted correctly and not escaped", t => {
 | 
				
			||||||
 | 
						t.deepEqual(
 | 
				
			||||||
 | 
							eventToMessage({
 | 
				
			||||||
 | 
								"type": "m.room.message",
 | 
				
			||||||
 | 
								"sender": "@cadence:cadence.moe",
 | 
				
			||||||
 | 
								"content": {
 | 
				
			||||||
 | 
									"msgtype": "m.text",
 | 
				
			||||||
 | 
									"body": "wrong body",
 | 
				
			||||||
 | 
									"format": "org.matrix.custom.html",
 | 
				
			||||||
 | 
									"formatted_body": "<pre><code>input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,\n_input_ = input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,\n</code></pre>\n<p><code>input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,</code></p>\n"
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								"origin_server_ts": 1693031482275,
 | 
				
			||||||
 | 
								"unsigned": {
 | 
				
			||||||
 | 
									"age": 99,
 | 
				
			||||||
 | 
									"transaction_id": "m1693031482146.511"
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								"event_id": "$pGkWQuGVmrPNByrFELxhzI6MCBgJecr5I2J3z88Gc2s",
 | 
				
			||||||
 | 
								"room_id": "!BpMdOUkWWhFxmTrENV:cadence.moe"
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
 | 
							[{
 | 
				
			||||||
 | 
								username: "cadence",
 | 
				
			||||||
 | 
								content: "```\ninput = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,\n_input_ = input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,\n```\n\n`input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,`",
 | 
				
			||||||
 | 
								avatar_url: undefined
 | 
				
			||||||
 | 
							}]
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("event2message: quotes have an appropriate amount of whitespace", t => {
 | 
				
			||||||
 | 
						t.deepEqual(
 | 
				
			||||||
 | 
							eventToMessage({
 | 
				
			||||||
 | 
								content: {
 | 
				
			||||||
 | 
									msgtype: "m.text",
 | 
				
			||||||
 | 
									body: "wrong body",
 | 
				
			||||||
 | 
									format: "org.matrix.custom.html",
 | 
				
			||||||
 | 
									formatted_body: "<blockquote>Chancellor of Germany Angela Merkel, on March 17, 2017: they did not shake hands<br><br><br></blockquote><br>🤨"
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU",
 | 
				
			||||||
 | 
								origin_server_ts: 1688301929913,
 | 
				
			||||||
 | 
								room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe",
 | 
				
			||||||
 | 
								sender: "@cadence:cadence.moe",
 | 
				
			||||||
 | 
								type: "m.room.message",
 | 
				
			||||||
 | 
								unsigned: {
 | 
				
			||||||
 | 
									age: 405299
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
 | 
							[{
 | 
				
			||||||
 | 
								username: "cadence",
 | 
				
			||||||
 | 
								content: "> Chancellor of Germany Angela Merkel, on March 17, 2017: they did not shake hands\n🤨",
 | 
				
			||||||
 | 
								avatar_url: undefined
 | 
				
			||||||
 | 
							}]
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test("event2message: m.emote markdown syntax is escaped", t => {
 | 
					test("event2message: m.emote markdown syntax is escaped", t => {
 | 
				
			||||||
	t.deepEqual(
 | 
						t.deepEqual(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue