From 742d34f15ee3401e65e132c6d333f5b2a253c16f Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 30 Jul 2024 23:45:44 +1200 Subject: [PATCH] Fork turndown for Discord-like link escaping --- m2d/converters/event-to-message.js | 2 +- m2d/converters/event-to-message.test.js | 39 +++++++++++++++++++++++++ package-lock.json | 19 ++++++------ package.json | 3 +- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/m2d/converters/event-to-message.js b/m2d/converters/event-to-message.js index 7b88adbf..a266f12a 100644 --- a/m2d/converters/event-to-message.js +++ b/m2d/converters/event-to-message.js @@ -4,7 +4,7 @@ const Ty = require("../../types") const DiscordTypes = require("discord-api-types/v10") const {Readable} = require("stream") const chunk = require("chunk-text") -const TurndownService = require("turndown") +const TurndownService = require("@cloudrac3r/turndown") const domino = require("domino") const assert = require("assert").strict const entities = require("entities") diff --git a/m2d/converters/event-to-message.test.js b/m2d/converters/event-to-message.test.js index c054abc8..7acad935 100644 --- a/m2d/converters/event-to-message.test.js +++ b/m2d/converters/event-to-message.test.js @@ -263,6 +263,45 @@ test("event2message: links in formatted body where the text & href are the same, ) }) +test("event2message: markdown in link text does not attempt to be escaped because that doesn't work", async t => { + t.deepEqual( + await eventToMessage({ + content: { + body: "hey mario sports mix [she/her], is it possible to listen on a unix socket?", + format: "org.matrix.custom.html", + formatted_body: "hey mario sports mix [she/her], is it possible to listen on a unix socket?", + "m.mentions": { + "user_ids": [ + "@cadence:cadence.moe" + ] + }, + msgtype: "m.text" + }, + 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: "hey [mario sports mix [she/her]](), is it possible to listen on a unix socket?", + avatar_url: undefined, + allowed_mentions: { + parse: ["users", "roles"] + } + }] + } + ) +}) + test("event2message: basic html is converted to markdown", async t => { t.deepEqual( await eventToMessage({ diff --git a/package-lock.json b/package-lock.json index 2d5284a1..f7d65534 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,9 +15,11 @@ "@cloudrac3r/html-template-tag": "^5.0.1", "@cloudrac3r/mixin-deep": "^3.0.0", "@cloudrac3r/pngjs": "^7.0.3", + "@cloudrac3r/turndown": "^7.1.4", "better-sqlite3": "^9.0.0", "chunk-text": "^2.0.1", "cloudstorm": "^0.10.8", + "domino": "^2.1.6", "entities": "^4.5.0", "get-stream": "^6.0.1", "heatsync": "^2.5.3", @@ -30,7 +32,6 @@ "snowtransfer": "^0.10.5", "stream-mime-type": "^1.0.2", "try-to-catch": "^3.0.1", - "turndown": "^7.1.2", "xxhash-wasm": "^1.0.2" }, "devDependencies": { @@ -213,6 +214,14 @@ "node": ">=8.0.0" } }, + "node_modules/@cloudrac3r/turndown": { + "version": "7.1.4", + "resolved": "https://registry.npmjs.org/@cloudrac3r/turndown/-/turndown-7.1.4.tgz", + "integrity": "sha512-bQAwcvcSqBTdEHPMt+IAZWIoDh+2eRuy9TgD0FUdxVurbvj3CUHTxLfzlmsO0UTi+GHpgYqDSsVdV7kYTNq5Qg==", + "dependencies": { + "domino": "^2.1.6" + } + }, "node_modules/@isaacs/cliui": { "version": "8.0.2", "resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz", @@ -3100,14 +3109,6 @@ "node": "*" } }, - "node_modules/turndown": { - "version": "7.1.2", - "resolved": "https://registry.npmjs.org/turndown/-/turndown-7.1.2.tgz", - "integrity": "sha512-ntI9R7fcUKjqBP6QU8rBK2Ehyt8LAzt3UBT9JR9tgo6GtuKvyUzpayWmeMKJw1DPdXzktvtIT8m2mVXz+bL/Qg==", - "dependencies": { - "domino": "^2.1.6" - } - }, "node_modules/type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", diff --git a/package.json b/package.json index 806785cd..13496736 100644 --- a/package.json +++ b/package.json @@ -21,9 +21,11 @@ "@cloudrac3r/html-template-tag": "^5.0.1", "@cloudrac3r/mixin-deep": "^3.0.0", "@cloudrac3r/pngjs": "^7.0.3", + "@cloudrac3r/turndown": "^7.1.4", "better-sqlite3": "^9.0.0", "chunk-text": "^2.0.1", "cloudstorm": "^0.10.8", + "domino": "^2.1.6", "entities": "^4.5.0", "get-stream": "^6.0.1", "heatsync": "^2.5.3", @@ -36,7 +38,6 @@ "snowtransfer": "^0.10.5", "stream-mime-type": "^1.0.2", "try-to-catch": "^3.0.1", - "turndown": "^7.1.2", "xxhash-wasm": "^1.0.2" }, "devDependencies": {