From 7499b17d88f1dc20d6431fe712bc6bdaf9a63bee Mon Sep 17 00:00:00 2001 From: Shea Duma Date: Sun, 26 Apr 2026 03:27:36 +0300 Subject: [PATCH] almost working --- src/app/components/editor/output.ts | 6 ++- .../components/message/MsgTypeRenderers.tsx | 42 +++++++++++++++---- src/app/features/room/RoomInput.tsx | 1 - .../features/room/message/MessageEditor.tsx | 33 +++++++++++++-- 4 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/app/components/editor/output.ts b/src/app/components/editor/output.ts index 8207f00d3..a03e81e68 100644 --- a/src/app/components/editor/output.ts +++ b/src/app/components/editor/output.ts @@ -199,7 +199,7 @@ const elementToPlainText = (node: CustomElement, children: string): string => { const SPOILERINPUTREGEX = /\|\|.+?\|\|/g; //very loose link check with the empty text at the end to make sure it doesnt overextend -const LINKINPUTREGEX = /(https?:\/\/[A-Za-z0-9-._~:/?#[\]()@!$&'*+,;%=]+)/g; +export const LINKINPUTREGEX = /\(?(https?:\/\/[A-Za-z0-9-._~:/?#[\]()@!$&'*+,;%=]+)\)?/g; const SPOILEREDLINKINPUTREGEX = /<(https?:\/\/[A-Za-z0-9-._~:/?#[\]()@!$&'*+,;%=]+)>/g; /** @@ -324,7 +324,9 @@ export const getLinks = (serialized: Descendant | Descendant[]): string[] | unde // truncate the spoilered ones of their <> and then remove the items that are present in both lists const urlsMatch = text.match(LINKINPUTREGEX); let urls = urlsMatch ? [...new Set(urlsMatch)] : undefined; - + urls = urls?.map((url) => + url.startsWith('(') && url.endsWith(')') ? url.substring(1, url.length - 1) : url + ); const spoileredUrlsMatch = text.match(SPOILEREDLINKINPUTREGEX); let spoileredUrls = spoileredUrlsMatch ? [...new Set(spoileredUrlsMatch)] : undefined; spoileredUrls = spoileredUrls?.map((spoileredUrl) => spoileredUrl.slice(1, -1)); diff --git a/src/app/components/message/MsgTypeRenderers.tsx b/src/app/components/message/MsgTypeRenderers.tsx index 29da77024..e75dd534b 100644 --- a/src/app/components/message/MsgTypeRenderers.tsx +++ b/src/app/components/message/MsgTypeRenderers.tsx @@ -2,7 +2,7 @@ import type { CSSProperties, ReactNode } from 'react'; import { useMemo } from 'react'; import { Box, Chip, Icon, Icons, Text, toRem } from 'folds'; import type { IContent, IPreviewUrlResponse } from '$types/matrix-sdk'; -import { JUMBO_EMOJI_REG, URL_REG } from '$utils/regex'; +import { JUMBO_EMOJI_REG } from '$utils/regex'; import { trimReplyFromBody } from '$utils/room'; import type { IAudioContent, @@ -36,8 +36,9 @@ import { } from './content'; import { MessageTextBody } from './layout'; import { unwrapForwardedContent } from './modals/MessageForward'; +import { LINKINPUTREGEX } from '$components/editor'; -interface BundleContent extends IPreviewUrlResponse { +export interface BundleContent extends IPreviewUrlResponse { matched_url: string; } @@ -146,8 +147,11 @@ export function MText({ if (!body && !customBody) return ; let bundleContent: BundleContent[] | undefined; - const urlsMatch = trimmedBody.match(URL_REG); + const urlsMatch = trimmedBody.match(LINKINPUTREGEX); let urls = urlsMatch ? [...new Set(urlsMatch)] : undefined; + urls = urls?.map((url) => + url.startsWith('(') && url.endsWith(')') ? url.substring(1, url.length - 1) : url + ); bundleContent = content['com.beeper.linkpreviews'] as BundleContent[]; //small "fix" for if someone sends malformed objects (ie not arrays of objects) try { @@ -240,10 +244,20 @@ export function MEmote({ const isJumbo = JUMBO_EMOJI_REG.test(trimmedBody); let bundleContent: BundleContent[] | undefined; - const urlsMatch = trimmedBody.match(URL_REG); - const urls = urlsMatch ? [...new Set(urlsMatch)] : undefined; + const urlsMatch = trimmedBody.match(LINKINPUTREGEX); + let urls = urlsMatch ? [...new Set(urlsMatch)] : undefined; + urls = urls?.map((url) => + url.startsWith('(') && url.endsWith(')') ? url.substring(1, url.length - 1) : url + ); bundleContent = content['com.beeper.linkpreviews'] as BundleContent[]; - bundleContent = bundleContent?.filter((bundle) => !!urls?.includes(bundle.matched_url)); + //small "fix" for if someone sends malformed objects (ie not arrays of objects) + try { + bundleContent = bundleContent?.filter((bundle) => !!urls?.includes(bundle.matched_url)); + if (renderUrlsPreview && bundleContent) + urls = bundleContent.map((bundle) => bundle.matched_url); + } catch { + urls = []; + } return ( <> @@ -292,10 +306,20 @@ export function MNotice({ const isJumbo = JUMBO_EMOJI_REG.test(trimmedBody); let bundleContent: BundleContent[] | undefined; - const urlsMatch = trimmedBody.match(URL_REG); - const urls = urlsMatch ? [...new Set(urlsMatch)] : undefined; + const urlsMatch = trimmedBody.match(LINKINPUTREGEX); + let urls = urlsMatch ? [...new Set(urlsMatch)] : undefined; + urls = urls?.map((url) => + url.startsWith('(') && url.endsWith(')') ? url.substring(1, url.length - 1) : url + ); bundleContent = content['com.beeper.linkpreviews'] as BundleContent[]; - bundleContent = bundleContent?.filter((bundle) => !!urls?.includes(bundle.matched_url)); + //small "fix" for if someone sends malformed objects (ie not arrays of objects) + try { + bundleContent = bundleContent?.filter((bundle) => !!urls?.includes(bundle.matched_url)); + if (renderUrlsPreview && bundleContent) + urls = bundleContent.map((bundle) => bundle.matched_url); + } catch { + urls = []; + } return ( <> diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 8f98b229a..db21b3544 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -807,7 +807,6 @@ export const RoomInput = forwardRef( const links = getLinks(serializedChildren); content['com.beeper.linkpreviews'] = []; links?.forEach((link) => content['com.beeper.linkpreviews'].push({ matched_url: link })); - if (replyDraft || !customHtmlEqualsPlainText(formattedBody, body)) { content.format = 'org.matrix.custom.html'; diff --git a/src/app/features/room/message/MessageEditor.tsx b/src/app/features/room/message/MessageEditor.tsx index 7322a99dd..ecc711bf9 100644 --- a/src/app/features/room/message/MessageEditor.tsx +++ b/src/app/features/room/message/MessageEditor.tsx @@ -36,6 +36,8 @@ import { useEditor, getMentions, ANYWHERE_AUTOCOMPLETE_PREFIXES, + getLinks, + LINKINPUTREGEX, } from '$components/editor'; import { useSetting } from '$state/hooks/settings'; import { CaptionPosition, settingsAtom } from '$state/settings'; @@ -56,6 +58,7 @@ import { useMediaAuthentication } from '$hooks/useMediaAuthentication'; import type { Opts as LinkifyOpts } from 'linkifyjs'; import type { GetContentCallback } from '$types/matrix/room'; import { sanitizeText } from '$utils/sanitize'; +import type { BundleContent } from '$components/message'; type MessageEditorProps = { roomId: string; @@ -116,9 +119,31 @@ export const MessageEditor = as<'div', MessageEditorProps>( ); } + const bundleContent = content['com.beeper.linkpreviews'] as BundleContent[]; + const markHiddenLinks = (original: string, isHTML?: boolean) => { + if (!bundleContent) return original; + const splitBody = original.split(isHTML ? /(?=^.+<)|(?=)|^<.+$/g : /(?=[ |\n|(|)])/g); + let newBody = ''; + splitBody.map((s) => { + if (s.length < 5) { + newBody += s; + return; + } + const strippedS = s.substring(1); + const isHidden = + (bundleContent?.length === 0 || + bundleContent.filter((b) => b.matched_url !== strippedS).length > 0) && + strippedS.match(LINKINPUTREGEX) !== null; + newBody += `${isHidden ? (isHTML && `<${s[0]}`) || `${s[0]}<` : s[0]}${strippedS}${isHidden ? (isHTML && '>') || '>' : ''}`; + }); + // oxlint-disable-next-line no-console + console.log(bundleContent, original, newBody, splitBody); + return newBody; + }; + return [ - typeof body === 'string' ? body : undefined, - typeof customHtml === 'string' ? customHtml : undefined, + typeof body === 'string' ? markHiddenLinks(body) : undefined, + typeof customHtml === 'string' ? markHiddenLinks(customHtml, true) : undefined, mMentions, ]; }, [room, mEvent]); @@ -212,7 +237,7 @@ export const MessageEditor = as<'div', MessageEditorProps>( newContent['m.mentions'] = mMentions; contentBody['m.mentions'] = mMentions; - // const links = getLinks(serialized); + const links = getLinks(editor.children); if (!customHtmlEqualsPlainText(customHtml, plainText)) { newContent.format = 'org.matrix.custom.html'; @@ -249,7 +274,7 @@ export const MessageEditor = as<'div', MessageEditorProps>( } } content['com.beeper.linkpreviews'] = []; - // links?.forEach((link) => content['com.beeper.linkpreviews'].push({ matched_url: link })); + links?.forEach((link) => content['com.beeper.linkpreviews'].push({ matched_url: link })); return mx.sendMessage(roomId, content as RoomMessageEventContent); }, [mx, editor, roomId, mEvent, isMarkdown, getPrevBodyAndFormattedBody, room])