m->d: Reliably put < > around matrix.to links
This replaces the turndown brackets system with a regexp over body and formatted_body.
This commit is contained in:
parent
e236a25da2
commit
6738290d99
2 changed files with 60 additions and 10 deletions
|
@ -126,12 +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")
|
||||||
let brackets = ["", ""]
|
|
||||||
content = content.replace(/ @.*/, "")
|
content = content.replace(/ @.*/, "")
|
||||||
if (href.startsWith("https://matrix.to")) brackets = ["<", ">"]
|
if (href === content) return href
|
||||||
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 + "](" + href + ")"
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -709,6 +707,9 @@ async function eventToMessage(event, guild, di) {
|
||||||
// @ts-ignore bad type from turndown
|
// @ts-ignore bad type from turndown
|
||||||
content = turndownService.turndown(root)
|
content = turndownService.turndown(root)
|
||||||
|
|
||||||
|
// Put < > around any surviving matrix.to links to hide the URL previews
|
||||||
|
content = content.replace(/\bhttps?:\/\/matrix\.to\/[^ )]*/, "<$&>")
|
||||||
|
|
||||||
// It's designed for commonmark, we need to replace the space-space-newline with just newline
|
// It's designed for commonmark, we need to replace the space-space-newline with just newline
|
||||||
content = content.replace(/ \n/g, "\n")
|
content = content.replace(/ \n/g, "\n")
|
||||||
|
|
||||||
|
@ -725,7 +726,8 @@ async function eventToMessage(event, guild, di) {
|
||||||
content = `* ${displayName} ${content}`
|
content = `* ${displayName} ${content}`
|
||||||
}
|
}
|
||||||
|
|
||||||
content = await handleRoomOrMessageLinks(content, 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
|
||||||
|
|
||||||
const result = await checkWrittenMentions(content, guild, di)
|
const result = await checkWrittenMentions(content, guild, di)
|
||||||
if (result) {
|
if (result) {
|
||||||
|
|
|
@ -2731,7 +2731,7 @@ test("event2message: mentioning events falls back to original link when the chan
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("event2message: link to event in an unknown room", async t => {
|
test("event2message: link to event in an unknown room (href link)", async t => {
|
||||||
t.deepEqual(
|
t.deepEqual(
|
||||||
await eventToMessage({
|
await eventToMessage({
|
||||||
content: {
|
content: {
|
||||||
|
@ -2741,13 +2741,61 @@ test("event2message: link to event in an unknown room", async t => {
|
||||||
formatted_body: 'ah yeah, here\'s where the bug was reported: <a href="https://matrix.to/#/!QtykxKocfZaZOUrTwp:matrix.org/$1542477546853947KGhZL:matrix.org">https://matrix.to/#/!QtykxKocfZaZOUrTwp:matrix.org/$1542477546853947KGhZL:matrix.org</a>'
|
formatted_body: 'ah yeah, here\'s where the bug was reported: <a href="https://matrix.to/#/!QtykxKocfZaZOUrTwp:matrix.org/$1542477546853947KGhZL:matrix.org">https://matrix.to/#/!QtykxKocfZaZOUrTwp:matrix.org/$1542477546853947KGhZL:matrix.org</a>'
|
||||||
},
|
},
|
||||||
event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU",
|
event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU",
|
||||||
origin_server_ts: 1688301929913,
|
|
||||||
room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe",
|
room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe",
|
||||||
sender: "@cadence:cadence.moe",
|
sender: "@cadence:cadence.moe",
|
||||||
type: "m.room.message",
|
type: "m.room.message",
|
||||||
unsigned: {
|
}),
|
||||||
age: 405299
|
{
|
||||||
|
ensureJoined: [],
|
||||||
|
messagesToDelete: [],
|
||||||
|
messagesToEdit: [],
|
||||||
|
messagesToSend: [{
|
||||||
|
username: "cadence [they]",
|
||||||
|
content: "ah yeah, here's where the bug was reported: <https://matrix.to/#/!QtykxKocfZaZOUrTwp:matrix.org/$1542477546853947KGhZL:matrix.org>",
|
||||||
|
avatar_url: undefined
|
||||||
|
}]
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("event2message: link to event in an unknown room (bare link)", async t => {
|
||||||
|
t.deepEqual(
|
||||||
|
await eventToMessage({
|
||||||
|
content: {
|
||||||
|
body: "wrong body",
|
||||||
|
format: "org.matrix.custom.html",
|
||||||
|
formatted_body: "PK API failure, tho idk how you'd handle that https://matrix.to/#/!SeYQChwXBnZaQLoZfI:sleeping.town/$AAPZ56B2P7TfROYPTtuoJjgvXmaBM11NoNceM8GCJ7s",
|
||||||
|
msgtype: "m.text"
|
||||||
|
},
|
||||||
|
sender: "@cadence:cadence.moe",
|
||||||
|
type: "m.room.message",
|
||||||
|
event_id: "$-UwntiHseGfch1GMjTROIgDbgLGIOwMx0vJdTi-dmok",
|
||||||
|
room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe"
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
ensureJoined: [],
|
||||||
|
messagesToDelete: [],
|
||||||
|
messagesToEdit: [],
|
||||||
|
messagesToSend: [{
|
||||||
|
username: "cadence [they]",
|
||||||
|
content: "PK API failure, tho idk how you'd handle that <https://matrix.to/#/!SeYQChwXBnZaQLoZfI:sleeping.town/$AAPZ56B2P7TfROYPTtuoJjgvXmaBM11NoNceM8GCJ7s>",
|
||||||
|
avatar_url: undefined
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("event2message: link to event in an unknown room (plaintext)", async t => {
|
||||||
|
t.deepEqual(
|
||||||
|
await eventToMessage({
|
||||||
|
content: {
|
||||||
|
msgtype: "m.text",
|
||||||
|
body: "ah yeah, here's where the bug was reported: https://matrix.to/#/!QtykxKocfZaZOUrTwp:matrix.org/$1542477546853947KGhZL:matrix.org"
|
||||||
|
},
|
||||||
|
event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU",
|
||||||
|
room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe",
|
||||||
|
sender: "@cadence:cadence.moe",
|
||||||
|
type: "m.room.message",
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
ensureJoined: [],
|
ensureJoined: [],
|
||||||
|
|
Loading…
Reference in a new issue