From 83bdc82049b47e41ba7d26c34b244b6bab75b1a3 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Sun, 5 Nov 2023 16:13:00 +0800 Subject: [PATCH] Add more unfurling - Fix regex - Handle trunks.social and Phanpy links too --- src/components/status.jsx | 20 +++++++++++++++++--- src/utils/isMastodonLinkMaybe.jsx | 6 ++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/components/status.jsx b/src/components/status.jsx index 20f94705..4da9ef05 100644 --- a/src/components/status.jsx +++ b/src/components/status.jsx @@ -2158,10 +2158,24 @@ function _unfurlMastodonLink(instance, url) { let remoteInstanceFetch; let theURL = url; - if (/\/\/elk\.[^\/]+\/[^.]+\.[^.]+/i.test(theURL)) { - // E.g. https://elk.zone/domain.com/@stest/123 -> https://domain.com/@stest/123 + + // https://elk.zone/domain.com/@stest/123 -> https://domain.com/@stest/123 + if (/\/\/elk\.[^\/]+\/[^\/]+\.[^\/]+/i.test(theURL)) { theURL = theURL.replace(/elk\.[^\/]+\//i, ''); } + + // https://trunks.social/status/domain.com/@stest/123 -> https://domain.com/@stest/123 + if (/\/\/trunks\.[^\/]+\/status\/[^\/]+\.[^\/]+/i.test(theURL)) { + theURL = theURL.replace(/trunks\.[^\/]+\/status\//i, ''); + } + + // https://phanpy.social/#/domain.com/s/123 -> https://domain.com/statuses/123 + if (/\/#\/[^\/]+\.[^\/]+\/s\/.+/i.test(theURL)) { + const urlAfterHash = theURL.split('/#/')[1]; + const finalURL = urlAfterHash.replace(/\/s\//i, '/@fakeUsername/'); + theURL = `https://${finalURL}`; + } + const urlObj = new URL(theURL); const domain = urlObj.hostname; const path = urlObj.pathname; @@ -2189,7 +2203,7 @@ function _unfurlMastodonLink(instance, url) { const { masto } = api({ instance }); const mastoSearchFetch = masto.v2.search .fetch({ - q: url, + q: theURL, type: 'statuses', resolve: true, limit: 1, diff --git a/src/utils/isMastodonLinkMaybe.jsx b/src/utils/isMastodonLinkMaybe.jsx index 7e029d2a..8fcfb306 100644 --- a/src/utils/isMastodonLinkMaybe.jsx +++ b/src/utils/isMastodonLinkMaybe.jsx @@ -1,9 +1,11 @@ export default function isMastodonLinkMaybe(url) { - const { pathname } = new URL(url); + const { pathname, hash } = new URL(url); return ( /^\/.*\/\d+$/i.test(pathname) || /^\/@[^/]+\/(statuses|posts)\/\w+\/?$/i.test(pathname) || // GoToSocial, Takahe /^\/notes\/[a-z0-9]+$/i.test(pathname) || // Misskey, Calckey - /^\/(notice|objects)\/[a-z0-9-]+$/i.test(pathname) // Pleroma + /^\/notes\/[a-z0-9]+$/i.test(pathname) || // Misskey, Calckey + /^\/(notice|objects)\/[a-z0-9-]+$/i.test(pathname) || // Pleroma + /#\/[^\/]+\.[^\/]+\/s\/.+/i.test(hash) // Phanpy 🫣 ); }