diff --git a/src/app/components/editor/output.ts b/src/app/components/editor/output.ts index a03e81e68..a240d947c 100644 --- a/src/app/components/editor/output.ts +++ b/src/app/components/editor/output.ts @@ -201,6 +201,8 @@ const SPOILERINPUTREGEX = /\|\|.+?\|\|/g; //very loose link check with the empty text at the end to make sure it doesnt overextend export const LINKINPUTREGEX = /\(?(https?:\/\/[A-Za-z0-9-._~:/?#[\]()@!$&'*+,;%=]+)\)?/g; const SPOILEREDLINKINPUTREGEX = /<(https?:\/\/[A-Za-z0-9-._~:/?#[\]()@!$&'*+,;%=]+)>/g; +const MASKEDSPOILEREDLINKINPUTREGEX = + /<\[.+\]\((https?:\/\/[A-Za-z0-9-._~:/?#[\]()@!$&'*+,;%=]+)\)>/g; /** * convert slate internal representation to a plain text string that can be sent to the server @@ -324,13 +326,37 @@ 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 + urls = urls?.map( + (url) => + (url.startsWith('(') && url.endsWith(')') && url.substring(1, url.length - 1)) || + (url.startsWith('(') && url.substring(1)) || + (url.endsWith('/)') && url.substring(0, url.length - 1)) || + url ); const spoileredUrlsMatch = text.match(SPOILEREDLINKINPUTREGEX); let spoileredUrls = spoileredUrlsMatch ? [...new Set(spoileredUrlsMatch)] : undefined; spoileredUrls = spoileredUrls?.map((spoileredUrl) => spoileredUrl.slice(1, -1)); + const maskedSpoileredUrlsMatch = text.match(MASKEDSPOILEREDLINKINPUTREGEX); + let maskedSpoileredUrls = maskedSpoileredUrlsMatch + ? [...new Set(maskedSpoileredUrlsMatch)] + : undefined; + maskedSpoileredUrls = maskedSpoileredUrls?.map((maskedSpoileredUrl) => + maskedSpoileredUrl?.substring( + maskedSpoileredUrl.indexOf('](') + 2, + maskedSpoileredUrl.lastIndexOf(')') + ) + ); + if (maskedSpoileredUrls) + spoileredUrls = spoileredUrls + ? [...spoileredUrls, ...maskedSpoileredUrls] + : maskedSpoileredUrls; + + spoileredUrls = spoileredUrls?.filter( + (item, index) => spoileredUrls?.indexOf(item) === index + ); + // oxlint-disable-next-line no-console + console.log(maskedSpoileredUrlsMatch, maskedSpoileredUrls, spoileredUrls); urls = urls?.filter((url) => !spoileredUrls?.includes(url)); finalList = finalList.concat(urls ?? []); return; @@ -340,5 +366,5 @@ export const getLinks = (serialized: Descendant | Descendant[]): string[] | unde }; if (Array.isArray(serialized)) serialized.map((n) => parseLinks(n)); else parseLinks(serialized); - return finalList; + return finalList.filter((item, index) => finalList.indexOf(item) === index); }; diff --git a/src/app/components/message/MsgTypeRenderers.tsx b/src/app/components/message/MsgTypeRenderers.tsx index e75dd534b..d956e6fb9 100644 --- a/src/app/components/message/MsgTypeRenderers.tsx +++ b/src/app/components/message/MsgTypeRenderers.tsx @@ -149,9 +149,13 @@ export function MText({ let bundleContent: BundleContent[] | 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 - ); + urls = urls?.map( + (url) => + (url.startsWith('(') && url.endsWith(')') && url.substring(1, url.length - 1)) || + (url.startsWith('(') && url.substring(1)) || + (url.endsWith('/)') && url.substring(0, 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 { @@ -246,9 +250,13 @@ export function MEmote({ let bundleContent: BundleContent[] | 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 - ); + urls = urls?.map( + (url) => + (url.startsWith('(') && url.endsWith(')') && url.substring(1, url.length - 1)) || + (url.startsWith('(') && url.substring(1)) || + (url.endsWith('/)') && url.substring(0, 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 { @@ -308,9 +316,13 @@ export function MNotice({ let bundleContent: BundleContent[] | 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 - ); + urls = urls?.map( + (url) => + (url.startsWith('(') && url.endsWith(')') && url.substring(1, url.length - 1)) || + (url.startsWith('(') && url.substring(1)) || + (url.endsWith('/)') && url.substring(0, 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 { diff --git a/src/app/features/room/message/MessageEditor.tsx b/src/app/features/room/message/MessageEditor.tsx index 4b1557edd..575801cc7 100644 --- a/src/app/features/room/message/MessageEditor.tsx +++ b/src/app/features/room/message/MessageEditor.tsx @@ -132,12 +132,10 @@ export const MessageEditor = as<'div', MessageEditorProps>( const strippedS = s.substring(1); const isHidden = (bundleContent?.length === 0 || - bundleContent.filter((b) => b.matched_url !== strippedS).length > 0) && + bundleContent.filter((b) => s.includes(b.matched_url)).length === 0) && strippedS.match(LINKINPUTREGEX) !== null; - newBody += `${isHidden ? (isHTML && `<${s[0]}`) || `${s[0]}<` : s[0]}${strippedS}${isHidden ? (isHTML && '>') || '>' : ''}`; + 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; }; @@ -275,6 +273,9 @@ export const MessageEditor = as<'div', MessageEditorProps>( } content['com.beeper.linkpreviews'] = []; links?.forEach((link) => content['com.beeper.linkpreviews'].push({ matched_url: link })); + content['m.new_content']['com.beeper.linkpreviews'] = content['com.beeper.linkpreviews']; + // oxlint-disable-next-line no-console + console.log(content, links); return mx.sendMessage(roomId, content as RoomMessageEventContent); }, [mx, editor, roomId, mEvent, isMarkdown, getPrevBodyAndFormattedBody, room]) diff --git a/src/app/plugins/markdown/inline/rules.ts b/src/app/plugins/markdown/inline/rules.ts index c51a11b1e..8ad467fa4 100644 --- a/src/app/plugins/markdown/inline/rules.ts +++ b/src/app/plugins/markdown/inline/rules.ts @@ -109,11 +109,11 @@ export const SpoilerRule: InlineMDRule = { const LINK_ALT = `\\[${MIN_ANY}\\]`; const LINK_URL = `\\(((<)?https?:\\/\\/.[A-Za-z0-9-._~:/?#[\\]()@!$&'*+,;%=]+)(>)?\\)`; -const LINK_REG_1 = new RegExp(`${LINK_ALT}${LINK_URL}`); +const LINK_REG_1 = new RegExp(`(<)?${LINK_ALT}${LINK_URL}(>)?`); export const LinkRule: InlineMDRule = { match: (text) => text.match(LINK_REG_1), html: (parse, match) => { - let [, g1, g2] = match; + let [, , g1, g2] = match; if (!g1 || !g2) return ''; if (g2.startsWith('<') && g2.endsWith('>')) return `${parse(g1)}`;