« get me outta code hell

external-links: better domain matching - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-03-29 16:30:32 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-03-29 20:10:12 -0300
commit3be13ce9e8145ee0ae4c626978f6b6c0deabf16e (patch)
tree021aafeab8ff8e10b984cba733a8bc11dc1f7a02 /src
parentcbfea6a0e017c4a8a4c47df688072d95180313a2 (diff)
external-links: better domain matching
Diffstat (limited to 'src')
-rw-r--r--src/util/external-links.js19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/util/external-links.js b/src/util/external-links.js
index 8ab8deca..f63b9244 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));