Compare commits

...

2 commits

Author SHA1 Message Date
141ae835df add test for anchor tags 2024-02-14 21:34:31 +13:00
Wonder Collective
7a9e1168fe fix anchor tags where href and content are the same 2024-02-14 21:15:58 +13:00
2 changed files with 29 additions and 1 deletions

View file

@ -129,6 +129,7 @@ turndownService.addRule("inlineLink", {
let brackets = ["", ""] let brackets = ["", ""]
content = content.replace(/ @.*/, "") content = content.replace(/ @.*/, "")
if (href.startsWith("https://matrix.to")) brackets = ["<", ">"] if (href.startsWith("https://matrix.to")) brackets = ["<", ">"]
if (href === content) return brackets[0] + href + brackets[1]
if (href.startsWith("https://matrix.to/#/@") && content[0] !== "@") content = "@" + content if (href.startsWith("https://matrix.to/#/@") && content[0] !== "@") content = "@" + content
return "[" + content + "](" + brackets[0] + href + brackets[1] + ")" return "[" + content + "](" + brackets[0] + href + brackets[1] + ")"
} }

View file

@ -172,6 +172,33 @@ test("event2message: links in plaintext body are not broken", async t => {
) )
}) })
test("event2message: links in formatted body where the text & href are the same, just post the link once", async t => {
t.deepEqual(
await eventToMessage({
sender: "@cadence:cadence.moe",
type: "m.room.message",
content: {
body: "https://privatebin.net/?9111cb16f28da21b#62CKkEr6WvXZ1gQv2M6agazsA7tGYX8ZP8drETYujYZr",
format: "org.matrix.custom.html",
formatted_body: "<a href=\"https://privatebin.net/?9111cb16f28da21b#62CKkEr6WvXZ1gQv2M6agazsA7tGYX8ZP8drETYujYZr\">https://privatebin.net/?9111cb16f28da21b#62CKkEr6WvXZ1gQv2M6agazsA7tGYX8ZP8drETYujYZr</a>",
msgtype: "m.text"
},
room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe",
event_id: "$p3AOv1eReiSH0g06_8AZ0WH0qSeaGdqwHhiNx_hz-bs",
}),
{
ensureJoined: [],
messagesToDelete: [],
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "https://privatebin.net/?9111cb16f28da21b#62CKkEr6WvXZ1gQv2M6agazsA7tGYX8ZP8drETYujYZr",
avatar_url: undefined
}]
}
)
})
test("event2message: basic html is converted to markdown", async t => { test("event2message: basic html is converted to markdown", async t => {
t.deepEqual( t.deepEqual(
await eventToMessage({ await eventToMessage({
@ -2664,7 +2691,7 @@ test("event2message: link to event in an unknown room", async t => {
messagesToEdit: [], messagesToEdit: [],
messagesToSend: [{ messagesToSend: [{
username: "cadence [they]", username: "cadence [they]",
content: "ah yeah, here's where the bug was reported: [https://matrix.to/#/!QtykxKocfZaZOUrTwp:matrix.org/$1542477546853947KGhZL:matrix.org](<https://matrix.to/#/!QtykxKocfZaZOUrTwp:matrix.org/$1542477546853947KGhZL:matrix.org>)", content: "ah yeah, here's where the bug was reported: <https://matrix.to/#/!QtykxKocfZaZOUrTwp:matrix.org/$1542477546853947KGhZL:matrix.org>",
avatar_url: undefined avatar_url: undefined
}] }]
} }