From 2a080b737cbb1f949314e177359bdf92fc5b91ea Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 20 Aug 2024 03:15:33 +1200 Subject: [PATCH] Parsing HTML with regular expressions (GONE WRONG) --- m2d/converters/event-to-message.js | 6 ++--- m2d/converters/event-to-message.test.js | 34 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/m2d/converters/event-to-message.js b/m2d/converters/event-to-message.js index 2a32dde..2984e8e 100644 --- a/m2d/converters/event-to-message.js +++ b/m2d/converters/event-to-message.js @@ -329,7 +329,7 @@ async function uploadEndOfMessageSpriteSheet(content, attachments, pendingFiles, */ async function handleRoomOrMessageLinks(input, di) { let offset = 0 - for (const match of [...input.matchAll(/("?https:\/\/matrix.to\/#\/(![^"/, ?)]+)(?:\/(\$[^"/ ?)]+))?(?:\?[^",:!? )]*)?)(">|[, )]|$)/g)]) { + for (const match of [...input.matchAll(/("?https:\/\/matrix.to\/#\/(![^"/, ?)]+)(?:\/(\$[^"/ ?)]+))?(?:\?[^",:!? )]*?)?)(">|[,<\n )]|$)/g)]) { assert(typeof match.index === "number") const [_, attributeValue, roomID, eventID, endMarker] = match let result @@ -726,7 +726,7 @@ async function eventToMessage(event, guild, di) { content = turndownService.turndown(root) // Put < > around any surviving matrix.to links to hide the URL previews - content = content.replace(/\bhttps?:\/\/matrix\.to\/[^ )]*/g, "<$&>") + content = content.replace(/\bhttps?:\/\/matrix\.to\/[^<>\n )]*/g, "<$&>") // It's designed for commonmark, we need to replace the space-space-newline with just newline content = content.replace(/ \n/g, "\n") @@ -745,7 +745,7 @@ async function eventToMessage(event, guild, di) { } content = await handleRoomOrMessageLinks(content, di) // Replace matrix.to links with discord.com equivalents where possible - content = content.replace(/\bhttps?:\/\/matrix\.to\/[^ )]*/, "<$&>") // Put < > around any surviving matrix.to links to hide the URL previews + content = content.replace(/\bhttps?:\/\/matrix\.to\/[^<>\n )]*/, "<$&>") // Put < > around any surviving matrix.to links to hide the URL previews const result = await checkWrittenMentions(content, event.sender, event.room_id, guild, di) if (result) { diff --git a/m2d/converters/event-to-message.test.js b/m2d/converters/event-to-message.test.js index f997b44..b404e59 100644 --- a/m2d/converters/event-to-message.test.js +++ b/m2d/converters/event-to-message.test.js @@ -3025,6 +3025,40 @@ test("event2message: mentioning known bridged events works (formatted body)", as ) }) +test("event2message: mentioning known bridged events followed by line break and user mention works (partially formatted body)", async t => { + t.deepEqual( + await eventToMessage({ + content: { + msgtype: "m.text", + body: "wrong body", + format: "org.matrix.custom.html", + formatted_body: `https://matrix.to/#/!CzvdIdUQXgUjDVKxeU:cadence.moe/$zXSlyI78DQqQwwfPUSzZ1b-nXzbUrCDljJgnGDdoI10?via=cadence.moeextremity` + }, + event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", + origin_server_ts: 1688301929913, + room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", + sender: "@cadence:cadence.moe", + type: "m.room.message", + unsigned: { + age: 405299 + } + }), + { + ensureJoined: [], + messagesToDelete: [], + messagesToEdit: [], + messagesToSend: [{ + username: "cadence [they]", + content: "https://discord.com/channels/497159726455455754/497161350934560778/1141619794500649020<@114147806469554185>", + avatar_url: undefined, + allowed_mentions: { + parse: ["users", "roles"] + } + }] + } + ) +}) + test("event2message: mentioning unknown bridged events can approximate with timestamps", async t => { let called = 0 t.deepEqual(