minimum viable solution

This commit is contained in:
Shea Duma committed 2026-04-27 02:52:19 +03:00
1 parent db96678c29
commit 3140b1620c
4 files changed
+57 -18

No files matched your search

+29 -3
View File
@@ -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);
};
@@ -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 {
@@ -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 && `&lt;${s[0]}`) || `${s[0]}<` : s[0]}${strippedS}${isHidden ? (isHTML && '&gt;') || '>' : ''}`;
newBody += `${isHidden ? (isHTML && `&lt;${s[0]}`) || `${s[0]}<` : s[0]}${strippedS}${isHidden ? (isHTML && '&gt;') || '>' : ''}`;
});
// 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])
+2 -2
View File
@@ -109,11 +109,11 @@ export const SpoilerRule: InlineMDRule = {
const LINK_ALT = `\\[${MIN_ANY}\\]`;
const LINK_URL = `\\(((&lt;)?https?:\\/\\/.[A-Za-z0-9-._~:/?#[\\]()@!$&'*+,;%=]+)(&gt;)?\\)`;
const LINK_REG_1 = new RegExp(`${LINK_ALT}${LINK_URL}`);
const LINK_REG_1 = new RegExp(`(&lt;)?${LINK_ALT}${LINK_URL}(&gt;)?`);
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('&lt;') && g2.endsWith('&gt;'))
return `<a data-md href="${g2.substring(4, g2.lastIndexOf('&gt;'))}">${parse(g1)}</a>`;