Skip to content

Commit

Permalink
fix: avoid embedding angle-bracketed links
Browse files Browse the repository at this point in the history
Closes podaboutlist#82

Warning: completely untested
  • Loading branch information
TheGiddyLimit authored May 7, 2024
1 parent 9b51800 commit a958660
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,39 @@ client.on(Events.MessageCreate, (message) => {
}

let reply = "";
let isSpoiler = false;

for (const [identifier, replacer] of replacementsEntries) {
// no "g" flag because we only need to know if there's one or zero matches
const regex = RegExp(identifier);

if (regex.test(message.content)) {
// bit ugly but easiest way to get rid of || at the end of spoilered links
// plus, what's the worst thing that could happen? what kind of URL has
// "|" in it? 👈 me settin myself up lol
const result = replacer(message.content.replaceAll("|", ""));

if (result) {
reply += result + "\n";
}
}

// split around "no embed" angle-brackets
message.content.split(/(<[^> ]+>)/g)
.forEach(part => {
// if a part starts and ends with angle brackets, it's not to be embedded; ignore it
if (part.startsWith("<") && part.endsWith(">")) return;

if (!regex.test(part)) return;

// if any part contains spoilers, we should spoiler the reply
if (part.includes("||")) isSpoiler = true;

// bit ugly but easiest way to get rid of || at the end of spoilered links
// plus, what's the worst thing that could happen? what kind of URL has
// "|" in it? 👈 me settin myself up lol
const result = replacer(part.replaceAll("|", ""));

if (result) {
reply += result + "\n";
}
});
}

if (reply === "") {
return;
}

if (message.content.includes("||")) {
if (isSpoiler) {
// Spoiler the message with some padding so the vertical bars don't mess
// up the end of the URLs
reply = "||" + reply.replace(/\n$/g, "") + " ||";
Expand Down

0 comments on commit a958660

Please sign in to comment.