diff --git a/m2d/converters/event-to-message.js b/m2d/converters/event-to-message.js index 7893799..f4382a3 100644 --- a/m2d/converters/event-to-message.js +++ b/m2d/converters/event-to-message.js @@ -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} event */ @@ -61,9 +95,13 @@ function eventToMessage(event) { // input = input.replace(/ /g, " ") // There is also a corresponding test to uncomment, named "event2message: whitespace is retained" + // Element adds a bunch of
before but doesn't render them. I can't figure out how this works, so let's just delete those. + input = input.replace(/(?:\n|
\s*)*<\/blockquote>/g, "") + // 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. input = input.replace(/(<\/?([^ >]+)[^>]*>)?\n(<\/?([^ >]+)[^>]*>)?/g, (whole, beforeContext, beforeTag, afterContext, afterTag) => { + // console.error(beforeContext, beforeTag, afterContext, afterTag) if (typeof beforeTag !== "string" && typeof afterTag !== "string") { return "
" } diff --git a/m2d/converters/event-to-message.test.js b/m2d/converters/event-to-message.test.js index 7aefdb1..afa40de 100644 --- a/m2d/converters/event-to-message.test.js +++ b/m2d/converters/event-to-message.test.js @@ -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": "
input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,\n_input_ = input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,\n
\n

input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,

\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: "
Chancellor of Germany Angela Merkel, on March 17, 2017: they did not shake hands



🤨" + }, + 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 => { t.deepEqual(