From c2ac2c78bb202f534ffac307b57fb3abbbceef4f Mon Sep 17 00:00:00 2001 From: Shea Duma Date: Fri, 24 Apr 2026 06:31:38 +0300 Subject: [PATCH] add stripping in plaintext --- src/app/components/editor/output.ts | 8 +++++--- src/app/components/message/MsgTypeRenderers.tsx | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/app/components/editor/output.ts b/src/app/components/editor/output.ts index 60791fd03..e15fef915 100644 --- a/src/app/components/editor/output.ts +++ b/src/app/components/editor/output.ts @@ -198,8 +198,7 @@ const elementToPlainText = (node: CustomElement, children: string): string => { }; const SPOILERINPUTREGEX = /\|\|.+?\|\|/g; -const HTTP_URL_PATTERN = `?`; -const URL_REG = new RegExp(HTTP_URL_PATTERN, 'g'); +const LINKINPUTREGEX = /?( |$)/g; /** * convert slate internal representation to a plain text string that can be sent to the server @@ -220,6 +219,9 @@ export const toPlainText = ( if (Text.isText(node)) { let { text } = node; text = text.replaceAll(SPOILERINPUTREGEX, '[Spoiler]'); + text = text.replace(LINKINPUTREGEX, '$1$2'); + // oxlint-disable-next-line no-console + console.log(text.match(LINKINPUTREGEX), text); if (stripNickname && nickNameReplacement) { nickNameReplacement?.keys().forEach((key) => { @@ -320,7 +322,7 @@ export const getMentions = (mx: MatrixClient, roomId: string, editor: Editor): M * @returns the mentions in a message {@link MentionsData} */ export const getLinks = (plaintext: string): string[] | undefined => { - const urlsMatch = plaintext.match(URL_REG); + const urlsMatch = plaintext.match(LINKINPUTREGEX); let urls = urlsMatch ? [...new Set(urlsMatch)] : undefined; urls = urls?.filter((url) => !(url.startsWith('<') && url.endsWith('>'))); diff --git a/src/app/components/message/MsgTypeRenderers.tsx b/src/app/components/message/MsgTypeRenderers.tsx index c6feea380..87ad01f3c 100644 --- a/src/app/components/message/MsgTypeRenderers.tsx +++ b/src/app/components/message/MsgTypeRenderers.tsx @@ -149,10 +149,11 @@ export function MText({ const urlsMatch = trimmedBody.match(URL_REG); let urls = urlsMatch ? [...new Set(urlsMatch)] : undefined; bundleContent = content['com.beeper.linkpreviews'] as BundleContent[]; - if(bundleContent?.length > 0){ + //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 = [];}; if ((content['com.beeper.per_message_profile'] as PerMessageProfileBeeperFormat)?.has_fallback) { // unwrap per-message profile fallback if present