Add handling for custom URLs

This commit is contained in:
nachtjasmin 2023-07-14 14:32:48 +02:00
parent 6abbe93219
commit 777724c9eb
No known key found for this signature in database
2 changed files with 23 additions and 0 deletions

View file

@ -135,6 +135,24 @@ function sanitizePronouns(str) {
// Remove all custom emojis with the :shortcode: format.
str = str.replace(/:[\w_]+:/gi, "");
// We still might have URLs in our text, for example, if people redirect some domain to pronouns.page.
// We filter them out, because they would not be clickable anyways and provide no benefit.
str = str
.split(" ")
.filter((x) => {
// Let's try to build an URL and if it looks like one, filter it out.
try {
const u = new URL(x);
return !u.protocol.startsWith("http");
} catch {
return true;
}
})
.join(" ");
// Remove trailing characters that are used as separators.
str = str.replace(/[-| /]+$/, "");
// Finally, remove leading and trailing whitespace.
str = str.trim();

View file

@ -59,6 +59,11 @@ const valueExtractionTests = [
[`<a href="https://de.pronouns.page/:Katze"></a>`, "Katze"], // custom pronouns
[`<a href="https://de.pronouns.page/@benaryorg"></a>`, "Katze"], // custom pronouns in profile
[`:theythem:`, null], // emojis shortcodes used for pronouns
[
// This is an actual example from a Mastodon field, with example.com redirecting to pronouns.page.
`dey/denen, es/ihm - <a href="https://example.com" rel="nofollow noopener noreferrer" target="_blank"><span class="invisible">https://</span><span class="">example.com</span><span class="invisible"></span></a>`,
"dey/denen, es/ihm",
],
];
for (const [input, expects] of valueExtractionTests) {
valueExtractionSuite(input, async () => {