Cache another regex

This commit is contained in:
Lim Chee Aun 2024-10-13 11:32:52 +08:00
parent 66e8bfe18c
commit f85cf22558

View file

@ -28,6 +28,7 @@ const INLINE_CODE_REGEX = /`[^`]+`/;
const TWITTER_DOMAIN_REGEX = /(twitter|x)\.com/i; const TWITTER_DOMAIN_REGEX = /(twitter|x)\.com/i;
const TWITTER_MENTION_REGEX = /@[a-zA-Z0-9_]+@(twitter|x)\.com/; const TWITTER_MENTION_REGEX = /@[a-zA-Z0-9_]+@(twitter|x)\.com/;
const TWITTER_MENTION_CAPTURE_REGEX = /(@([a-zA-Z0-9_]+)@(twitter|x)\.com)/g; const TWITTER_MENTION_CAPTURE_REGEX = /(@([a-zA-Z0-9_]+)@(twitter|x)\.com)/g;
const CODE_INLINE_CAPTURE_REGEX = /(`[^]+?`)/g;
function createDOM(html, isDocumentFragment) { function createDOM(html, isDocumentFragment) {
if (isDocumentFragment) { if (isDocumentFragment) {
@ -197,7 +198,7 @@ function _enhanceContent(content, opts = {}) {
for (const node of textNodes) { for (const node of textNodes) {
let html = escapeHTML(node.nodeValue); let html = escapeHTML(node.nodeValue);
if (INLINE_CODE_REGEX.test(html)) { if (INLINE_CODE_REGEX.test(html)) {
html = html.replaceAll(/(`[^]+?`)/g, '<code>$1</code>'); html = html.replaceAll(CODE_INLINE_CAPTURE_REGEX, '<code>$1</code>');
} }
fauxDiv.innerHTML = html; fauxDiv.innerHTML = html;
// const nodes = [...fauxDiv.childNodes]; // const nodes = [...fauxDiv.childNodes];