1
0
Fork 0

add test for anchor tags

This commit is contained in:
Cadence Ember 2024-02-14 21:34:31 +13:00
parent 7a9e1168fe
commit 141ae835df
2 changed files with 29 additions and 2 deletions

View file

@ -126,10 +126,10 @@ turndownService.addRule("inlineLink", {
if (node.getAttribute("data-message-id")) return `https://discord.com/channels/${node.getAttribute("data-guild-id")}/${node.getAttribute("data-channel-id")}/${node.getAttribute("data-message-id")}` if (node.getAttribute("data-message-id")) return `https://discord.com/channels/${node.getAttribute("data-guild-id")}/${node.getAttribute("data-channel-id")}/${node.getAttribute("data-message-id")}`
if (node.getAttribute("data-channel-id")) return `<#${node.getAttribute("data-channel-id")}>` if (node.getAttribute("data-channel-id")) return `<#${node.getAttribute("data-channel-id")}>`
const href = node.getAttribute("href") const href = node.getAttribute("href")
if (href === content) return href
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
}] }]
} }