From 0684a0649bb270eb1398853d27bf0e1caa869fe6 Mon Sep 17 00:00:00 2001 From: Nathan Lamy Date: Wed, 30 Jul 2025 18:24:42 +0200 Subject: [PATCH] perf: remove latex extraction from client --- app/lib/latex.ts | 33 --------------------------------- app/routes/colles.tsx | 12 ++---------- 2 files changed, 2 insertions(+), 43 deletions(-) delete mode 100644 app/lib/latex.ts diff --git a/app/lib/latex.ts b/app/lib/latex.ts deleted file mode 100644 index 1c8c7a2..0000000 --- a/app/lib/latex.ts +++ /dev/null @@ -1,33 +0,0 @@ -export function extractLatexImages(html: string) { - const imgRegex = - /]+src="(https:\/\/latex\.codecogs\.com\/gif\.latex\?(=?.*?))"[^>]*>/g; - let parts = []; - let latexMatches: string[] = []; - let lastIndex = 0; - - html.replace(imgRegex, (match, _, latex, index) => { - parts.push(html.slice(lastIndex, index)); // Add HTML before image - latexMatches.push(decodeURIComponent(latex)); // Extract and decode LaTeX - lastIndex = index + match.length; - return ""; - }); - - parts.push(html.slice(lastIndex)); // Add remaining HTML after last image - - return { parts, latexMatches }; -} - -export function renderLatex(html: string) { - const { parts, latexMatches } = extractLatexImages(html); - const outputHtml = parts - .map((part, i) => { - if (!latexMatches[i]) { - return part; - } - return `${part}$$${latexMatches[i]}$$`; - }) - .join(""); - // Remove all "\," from string - const regex = /\\,/g; - return outputHtml.replace(regex, " "); -} diff --git a/app/routes/colles.tsx b/app/routes/colles.tsx index 49319b3..76e5d88 100644 --- a/app/routes/colles.tsx +++ b/app/routes/colles.tsx @@ -1,13 +1,5 @@ import "katex/dist/katex.min.css"; import { DomUtils, parseDocument } from "htmlparser2"; -// TODO: API - remove trailing lines from HTML comment/content -// TODO: Server side image extraction and latex rendering -// TEMP SOLUTION -import { renderLatex } from "~/lib/latex"; // Custom LaTeX rendering function -// function removeTrailingLines(htmlString: string) { -// return htmlString.replace(/(\s*)+$/gi, "").trim(); -// } - import Latex from "react-latex-next"; import { useState } from "react"; import { Navigate, useNavigate, useParams } from "react-router"; @@ -61,7 +53,7 @@ export default function ColleDetailPage() { const [isReloading, setIsReloading] = useState(false); if (isUserLoading) { - return + return ; } if (userError?.message === AUTH_ERROR) { return ; @@ -435,7 +427,7 @@ function ExpandableComment({ comment }: { comment: string }) { return (
- {renderLatex(displayedComment)} + {displayedComment} {isLongComment && (