mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
almost working
This commit is contained in:
1 parent
02b7e23eed
commit
7499b17d88
4 files changed
+66
-16
No files matched your search
@@ -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));
|
||||
|
||||
@@ -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 <BrokenContent body={customBody ?? body} />;
|
||||
|
||||
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 (
|
||||
<>
|
||||
|
||||
@@ -807,7 +807,6 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
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';
|
||||
|
||||
@@ -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 ? /(?=^.+<)|(?=<a.+a>)|^<.+$/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])
|
||||
|
||||
Reference in new issue
Block a user