From 4fc254c1dd2292004e26c4ffafde31c4fd856747 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Sun, 15 Jun 2025 08:11:32 -0700 Subject: [PATCH] Optimize syntax highlighting on long conversations --- js/main.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/js/main.js b/js/main.js index e970884d..9f77e4a7 100644 --- a/js/main.js +++ b/js/main.js @@ -231,8 +231,15 @@ function doSyntaxHighlighting() { if (messageBodies.length > 0) { observer.disconnect(); - messageBodies.forEach((messageBody) => { + let hasSeenVisible = false; + + // Go from last message to first + for (let i = messageBodies.length - 1; i >= 0; i--) { + const messageBody = messageBodies[i]; + if (isElementVisibleOnScreen(messageBody)) { + hasSeenVisible = true; + // Handle both code and math in a single pass through each message const codeBlocks = messageBody.querySelectorAll("pre code:not([data-highlighted])"); codeBlocks.forEach((codeBlock) => { @@ -249,8 +256,12 @@ function doSyntaxHighlighting() { { left: "\\[", right: "\\]", display: true }, ], }); + } else if (hasSeenVisible) { + // We've seen visible messages but this one is not visible + // Since we're going from last to first, we can break + break; } - }); + } observer.observe(targetElement, config); }