Skip to content

Commit

Permalink
move dumb loop to regex replace
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <[email protected]>
  • Loading branch information
metal0 authored Dec 29, 2023
1 parent 089d7a7 commit d4c591b
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions backend/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,6 @@ const plainLinkRegex = /((?!https?:\/\/)\S)+\.\S+/; // anything.anything, withou
// Both of the above, with precedence on the first one
const urlRegex = new RegExp(`(${realLinkRegex.source}|${plainLinkRegex.source})`, "g");
const protocolRegex = /^[a-z]+:\/\//;
const hostnameTldRegex = /^[a-z]$/;

interface MatchedURL extends URL {
input: string;
Expand All @@ -651,10 +650,7 @@ export function getUrlsInString(str: string, onlyUnique = false): MatchedURL[] {
let hostname = matchUrl.hostname.toLowerCase();

if (hostname.length > 3) {
while (!hostnameTldRegex.test(hostname.at(-1)!)) {
if (!hostname.length) break;
hostname = hostname.slice(0, -1);
}
hostname = hostname.replace(/[^a-z]+$/, "");
}

const hostnameParts = hostname.split(".");
Expand Down

0 comments on commit d4c591b

Please sign in to comment.