1
0
Fork 0

Fix "...\nhttps://..." corrupting the link

This commit is contained in:
Cadence Ember 2024-05-25 22:46:28 +12:00
parent 1f5865b0d8
commit 07a133eba9
2 changed files with 34 additions and 5 deletions

View file

@ -54,16 +54,17 @@ const turndownService = new TurndownService({
*/ */
// @ts-ignore bad type from turndown // @ts-ignore bad type from turndown
turndownService.escape = function (string) { turndownService.escape = function (string) {
const escapedWords = string.split(" ").map(word => { return string.replace(/\s+|\S+/g, part => { // match chunks of spaces or non-spaces
if (word.match(/^https?:\/\//)) { if (part.match(/\s/)) return part // don't process spaces
return word
if (part.match(/^https?:\/\//)) {
return part
} else { } else {
return markdownEscapes.reduce(function (accumulator, escape) { return markdownEscapes.reduce(function (accumulator, escape) {
return accumulator.replace(escape[0], escape[1]) return accumulator.replace(escape[0], escape[1])
}, word) }, part)
} }
}) })
return escapedWords.join(" ")
} }
turndownService.remove("mx-reply") turndownService.remove("mx-reply")

View file

@ -205,6 +205,34 @@ test("event2message: links in plaintext body are not broken", async t => {
) )
}) })
test("event2message: links in plaintext body are not broken when preceded by a newline", async t => {
t.deepEqual(
await eventToMessage({
type: "m.room.message",
sender: "@cadence:cadence.moe",
content: {
msgtype: "m.text",
body: "java redstoners will be like \"I hate bedrock edition redstone!!\" meanwhile java edition:\nhttps://youtu.be/g_ORb7bN3CM"
},
event_id: "$b1c5gJZfh1gq3zz6UkhI1whJ61JVvgvvzbdSPEYnTbY",
room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe"
}),
{
ensureJoined: [],
messagesToDelete: [],
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "java redstoners will be like \"I hate bedrock edition redstone!!\" meanwhile java edition:\nhttps://youtu.be/g_ORb7bN3CM",
avatar_url: undefined,
allowed_mentions: {
parse: ["users", "roles"]
}
}]
}
)
})
test("event2message: links in formatted body where the text & href are the same, just post the link once", async t => { test("event2message: links in formatted body where the text & href are the same, just post the link once", async t => {
t.deepEqual( t.deepEqual(
await eventToMessage({ await eventToMessage({