From 7afce11de0f3ba5432372a773cfb3e8d135b76fe Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 29 Mar 2024 19:31:06 -0300 Subject: external-links: always require handle/details extractions to match --- src/util/external-links.js | 55 +++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 20 deletions(-) (limited to 'src/util/external-links.js') diff --git a/src/util/external-links.js b/src/util/external-links.js index 6d993ee0..46d6e0e9 100644 --- a/src/util/external-links.js +++ b/src/util/external-links.js @@ -197,14 +197,13 @@ export const externalLinkSpec = [ match: { context: 'flash', domain: 'homestuck.com', - pathname: /^story\/[0-9]+\/?$/, }, platform: 'homestuck', detail: { substring: 'page', - page: {pathname: /[0-9]+/}, + page: {pathname: /^story\/([0-9]+)\/?$/,}, }, icon: 'globe', @@ -281,10 +280,7 @@ export const externalLinkSpec = [ }, { - match: { - domain: 'bsky.app', - pathname: /^profile\/[^/]+\/?/, - }, + match: {domain: 'bsky.app'}, platform: 'bluesky', handle: {pathname: /^profile\/([^/]+?)(?:\.bsky\.social)?\/?$/}, @@ -329,10 +325,7 @@ export const externalLinkSpec = [ }, { - match: { - domain: 'itch.io', - pathname: /^profile\/.+\/?$/, - }, + match: {domain: 'itch.io'}, platform: 'itch', handle: {pathname: /^profile\/(.+)\/?$/}, @@ -341,10 +334,7 @@ export const externalLinkSpec = [ }, { - match: { - domain: 'ko-fi.com', - pathname: /^.+\/?/, - }, + match: {domain: 'ko-fi.com'}, platform: 'kofi', handle: {pathname: /^(.+)\/?/}, @@ -353,10 +343,7 @@ export const externalLinkSpec = [ }, { - match: { - domain: 'mspaintadventures.fandom.com', - pathname: /^wiki\/.+\/?$/, - }, + match: {domain: 'mspaintadventures.fandom.com'}, platform: 'fandom.mspaintadventures', @@ -556,6 +543,9 @@ export function getMatchingDescriptorsForExternalLink(url, descriptors, { const comparePathname = regex => regex.test(pathname.slice(1)); const compareQuery = regex => regex.test(query.slice(1)); + const compareExtractSpec = extract => + extractPartFromExternalLink(url, extract, {mode: 'test'}); + const contextArray = (Array.isArray(context) ? context @@ -589,12 +579,29 @@ export function getMatchingDescriptorsForExternalLink(url, descriptors, { ? compareQuery(match.query) : match.queries ? match.quieries.some(compareQuery) + : true)) + + .filter(({handle}) => + (handle + ? compareExtractSpec(handle) + : true)) + + .filter(({detail}) => + (typeof detail === 'object' + ? Object.entries(detail) + .filter(([key]) => key !== 'substring') + .map(([_key, value]) => value) + .every(compareExtractSpec) : true)); return [...matchingDescriptors, fallbackDescriptor]; } -export function extractPartFromExternalLink(url, extract) { +export function extractPartFromExternalLink(url, extract, { + // Set to 'test' to just see if this would extract anything. + // This disables running custom transformations. + mode = 'extract', +} = {}) { const {domain, pathname, query} = urlParts(url); let regexen = []; @@ -670,15 +677,23 @@ export function extractPartFromExternalLink(url, extract) { })) { const match = test.match(regex); if (match) { - value = prefix + (match[1] ?? match[0]); + value = match[1] ?? match[0]; break; } } + if (mode === 'test') { + return !!value; + } + if (!value) { return null; } + if (prefix) { + value = prefix + value; + } + for (const fn of transform) { value = fn(value); } -- cgit 1.3.0-6-gf8a5