mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-25 19:41:17 -08:00
perf: hoist some regex patterns
This commit is contained in:
5 files changed
+41
-29
No files matched your search
@@ -90,11 +90,14 @@ type RenderMessageContentProps = {
|
||||
onOpenMedia?: (mEvent: MatrixEvent) => boolean;
|
||||
};
|
||||
|
||||
const VIDEO_MEDIA_TYPE_REG = /\.(mp4|webm|ogg)$/i;
|
||||
const IMAGE_MEDIA_TYPE_REG = /\.(png|jpg|jpeg|gif|webp)$/i;
|
||||
const IMAGE_MEDIA_TYPE_AT_REG = /@(jpeg|webp|png|jpg)$/i;
|
||||
|
||||
const getMediaType = (url: string) => {
|
||||
const cleanUrl = url.toLowerCase();
|
||||
if (cleanUrl.match(/\.(mp4|webm|ogg)$/i)) return 'video';
|
||||
if (cleanUrl.match(/\.(png|jpg|jpeg|gif|webp)$/i) || cleanUrl.match(/@(jpeg|webp|png|jpg)$/i))
|
||||
return 'image';
|
||||
if (VIDEO_MEDIA_TYPE_REG.test(cleanUrl)) return 'video';
|
||||
if (IMAGE_MEDIA_TYPE_REG.test(cleanUrl) || IMAGE_MEDIA_TYPE_AT_REG.test(cleanUrl)) return 'image';
|
||||
return null;
|
||||
};
|
||||
|
||||
|
||||
@@ -4,6 +4,15 @@ import { testMatrixTo } from '$plugins/matrix-to';
|
||||
const LINK_URL = `(https?:\\/\\/.[A-Za-z0-9-._~:/?#[\\()@!$&'*+,;%=]+)`;
|
||||
const LINKINPUTREGEX = new RegExp(`\\(?(${LINK_URL})\\)?`, 'g');
|
||||
|
||||
const WRAPPED_ESCAPED_URL_REG = new RegExp(String.raw`\\<(${LINK_URL})\\>`, 'g');
|
||||
const OPEN_ONLY_ESCAPED_URL_REG = new RegExp(String.raw`\\<(${LINK_URL})`, 'g');
|
||||
const CLOSE_ONLY_ESCAPED_URL_REG = new RegExp(String.raw`(${LINK_URL})\\>`, 'g');
|
||||
const ESCAPED_SUPPRESSED_MD_LINK_REG = new RegExp(
|
||||
String.raw`\\<\[([^\]]*)\]\((<https?:\/\/[^>\s]+>)\)\\>`,
|
||||
'g'
|
||||
);
|
||||
const WRONG_OUTER_ESCAPED_AUTOLINK_REG = /\\<\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)(?:>|\\>)/g;
|
||||
|
||||
/**
|
||||
* `htmlToMarkdown()` escapes `<` and `>` into `\<` and `\>` in text nodes.
|
||||
*
|
||||
@@ -15,22 +24,16 @@ const LINKINPUTREGEX = new RegExp(`\\(?(${LINK_URL})\\)?`, 'g');
|
||||
export function stripMarkdownEscapesForHiddenPreviews(markdown: string): string {
|
||||
// Handle: \<https://example.com\>
|
||||
// Also handle common surrounding parens: (\<url\>), (\<url), \<url\>)
|
||||
const WRAPPED = new RegExp(String.raw`\\<(${LINK_URL})\\>`, 'g');
|
||||
const OPEN_ONLY = new RegExp(String.raw`\\<(${LINK_URL})`, 'g');
|
||||
const CLOSE_ONLY = new RegExp(String.raw`(${LINK_URL})\\>`, 'g');
|
||||
|
||||
let s = markdown.replace(WRAPPED, '<$1>').replace(OPEN_ONLY, '<$1').replace(CLOSE_ONLY, '$1>');
|
||||
let s = markdown
|
||||
.replace(WRAPPED_ESCAPED_URL_REG, '<$1>')
|
||||
.replace(OPEN_ONLY_ESCAPED_URL_REG, '<$1')
|
||||
.replace(CLOSE_ONLY_ESCAPED_URL_REG, '$1>');
|
||||
|
||||
// Restore [label](<url>) after htmlToMarkdown escaped a surrounding "\<...\>".
|
||||
const ESCAPED_SUPPRESSED_MD_LINK = new RegExp(
|
||||
String.raw`\\<\[([^\]]*)\]\((<https?:\/\/[^>\s]+>)\)\\>`,
|
||||
'g'
|
||||
);
|
||||
s = s.replace(ESCAPED_SUPPRESSED_MD_LINK, '[$1]($2)');
|
||||
s = s.replace(ESCAPED_SUPPRESSED_MD_LINK_REG, '[$1]($2)');
|
||||
|
||||
// Same for "\<[label](bare-url)>" when angle brackets were lost on the destination.
|
||||
const WRONG_OUTER_ESCAPED_AUTOLINK = /\\<\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)(?:>|\\>)/g;
|
||||
s = s.replace(WRONG_OUTER_ESCAPED_AUTOLINK, '[$1](<$2>)');
|
||||
s = s.replace(WRONG_OUTER_ESCAPED_AUTOLINK_REG, '[$1](<$2>)');
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -377,10 +377,9 @@ const scaleEmojiChunk = (text: string, output: (string | JSX.Element)[]) => {
|
||||
|
||||
export const scaleSystemEmoji = (text: string): (string | JSX.Element)[] => {
|
||||
const parts: (string | JSX.Element)[] = [];
|
||||
const urlReg = new RegExp(URL_REG, 'g');
|
||||
let lastIndex = 0;
|
||||
|
||||
[...text.matchAll(urlReg)].forEach((match) => {
|
||||
[...text.matchAll(URL_REG)].forEach((match) => {
|
||||
const start = match.index ?? 0;
|
||||
scaleEmojiChunk(text.slice(lastIndex, start), parts);
|
||||
parts.push(match[0]);
|
||||
|
||||
@@ -32,14 +32,23 @@ export type TextSegment = {
|
||||
termKey?: string;
|
||||
};
|
||||
|
||||
export const splitByAbbreviations = (text: string, abbrMap: Map<string, string>): TextSegment[] => {
|
||||
if (abbrMap.size === 0) return [{ id: 'txt-0', text }];
|
||||
const abbrPatternCache = new WeakMap<Map<string, string>, RegExp>();
|
||||
|
||||
const getAbbrPattern = (abbrMap: Map<string, string>): RegExp => {
|
||||
const cached = abbrPatternCache.get(abbrMap);
|
||||
if (cached) return cached;
|
||||
|
||||
// Build a regex that matches any of the terms at word boundaries.
|
||||
// Sort longest first so "HTTP/2" matches before "HTTP".
|
||||
const terms = [...abbrMap.keys()].toSorted((a, b) => b.length - a.length);
|
||||
const escaped = terms.map((t) => t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
||||
const pattern = new RegExp(`\\b(${escaped.join('|')})\\b`, 'gi');
|
||||
abbrPatternCache.set(abbrMap, pattern);
|
||||
return pattern;
|
||||
};
|
||||
|
||||
export const splitByAbbreviations = (text: string, abbrMap: Map<string, string>): TextSegment[] => {
|
||||
if (abbrMap.size === 0) return [{ id: 'txt-0', text }];
|
||||
|
||||
const pattern = getAbbrPattern(abbrMap);
|
||||
|
||||
const segments: TextSegment[] = [];
|
||||
let lastIndex = 0;
|
||||
|
||||
@@ -4,6 +4,7 @@ import { testMatrixUri } from '../plugins/matrix-uri';
|
||||
|
||||
const MAX_TAG_NESTING = 100;
|
||||
const INTERNAL_IMG_SRC_ATTR = 'data-sable-img-src';
|
||||
const RESTORE_PROTECTED_SRC_REG = new RegExp(`\\s${INTERNAL_IMG_SRC_ATTR}="([^"]+)"`, 'g');
|
||||
|
||||
const permittedHtmlTags = [
|
||||
'del',
|
||||
@@ -254,15 +255,12 @@ function restoreProtectedImageSources(
|
||||
sanitizedHtml: string,
|
||||
protectedSources: Map<string, string>
|
||||
): string {
|
||||
return sanitizedHtml.replace(
|
||||
new RegExp(`\\s${INTERNAL_IMG_SRC_ATTR}="([^"]+)"`, 'g'),
|
||||
(_match, sourceId: string) => {
|
||||
const validatedSrc = getValidatedProtectedImageSource(sourceId, protectedSources);
|
||||
if (!validatedSrc) return '';
|
||||
return sanitizedHtml.replace(RESTORE_PROTECTED_SRC_REG, (_match, sourceId: string) => {
|
||||
const validatedSrc = getValidatedProtectedImageSource(sourceId, protectedSources);
|
||||
if (!validatedSrc) return '';
|
||||
|
||||
return ` src="${sanitizeText(validatedSrc)}"`;
|
||||
}
|
||||
);
|
||||
return ` src="${sanitizeText(validatedSrc)}"`;
|
||||
});
|
||||
}
|
||||
|
||||
const enforceNestingLimit = (fragment: DocumentFragment): void => {
|
||||
|
||||
Reference in new issue
Block a user