From fcbebd37203d0dedf4110a3fe7522b49e08a7213 Mon Sep 17 00:00:00 2001
From: Dorian Koch <doriank@fsmpi.rwth-aachen.de>
Date: Sat, 10 Aug 2024 18:11:50 +0200
Subject: [PATCH] Improve StylizedText rendering performance by avoiding
 markdown

---
 src/components/StylizedText.tsx | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/components/StylizedText.tsx b/src/components/StylizedText.tsx
index 852742e..6fc4de5 100644
--- a/src/components/StylizedText.tsx
+++ b/src/components/StylizedText.tsx
@@ -25,6 +25,16 @@ export function StylizedText({
         str = str.replaceAll(/<strong>(.*?)<\/strong>/gs, "**$1**");
         str = str.replaceAll(/<p>(.*?)<\/p>/gs, "$1\n\n");
         str = str.replaceAll(/<!--(.*?)-->/gs, "");
+
+        // first test if there is any markdown in the string at all
+        // markdown rendering is expensive (~1ms per Component) so we want to avoid it if possible
+        // this is a very simple test and will not catch all cases, but it will never give a false positive
+        if (!str.match(/[*#[\]\d(`>-]/)) {
+            if (mapParagraphToSpan)
+                return str.split("\n\n").map((line, index) => <span key={index}>{line}</span>);
+            return str.split("\n\n").map((line, index) => <p key={index}>{line}</p>);
+        }
+
         return (
             <Suspense fallback={str}>
                 <LazyMarkdown
-- 
GitLab