From 3be13ce9e8145ee0ae4c626978f6b6c0deabf16e Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 29 Mar 2024 16:30:32 -0300 Subject: external-links: better domain matching --- src/util/external-links.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/util') diff --git a/src/util/external-links.js b/src/util/external-links.js index 8ab8dec..f63b924 100644 --- a/src/util/external-links.js +++ b/src/util/external-links.js @@ -443,7 +443,24 @@ export function getMatchingDescriptorsForExternalLink(url, descriptors, { } = {}) { const {domain, pathname, query} = urlParts(url); - const compareDomain = string => domain.includes(string); + const compareDomain = string => { + // A dot at the start of the descriptor's domain indicates + // we're looking to match a subdomain. + if (string.startsWith('.')) matchSubdomain: { + // "www" is never an acceptable subdomain for this purpose. + // Sorry to people whose usernames are www!! + if (domain.startsWith('www.')) { + break matchSubdomain; + } + + return domain.endsWith(string); + } + + // No dot means we're looking for an exact/full domain match. + // But let "www" pass here too, implicitly. + return domain === string || domain === 'www.' + string; + }; + const comparePathname = regex => regex.test(pathname.slice(1)); const compareQuery = regex => regex.test(query.slice(1)); -- cgit 1.3.0-6-gf8a5