From f342f8adc7c70aca58f5e4bee290615d014daa6f Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Thu, 20 Jun 2024 12:27:34 -0700 Subject: [PATCH] refac: code highlight optimisation --- src/lib/components/chat/Messages/CodeBlock.svelte | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lib/components/chat/Messages/CodeBlock.svelte b/src/lib/components/chat/Messages/CodeBlock.svelte index 4f576ef24..9714e86c7 100644 --- a/src/lib/components/chat/Messages/CodeBlock.svelte +++ b/src/lib/components/chat/Messages/CodeBlock.svelte @@ -203,8 +203,18 @@ __builtins__.input = input`); }; }; + let debounceTimeout; $: if (code) { - highlightedCode = hljs.highlightAuto(code, hljs.getLanguage(lang)?.aliases).value || code; + // Function to perform the code highlighting + const highlightCode = () => { + highlightedCode = hljs.highlightAuto(code, hljs.getLanguage(lang)?.aliases).value || code; + }; + + // Clear the previous timeout if it exists + clearTimeout(debounceTimeout); + + // Set a new timeout to debounce the code highlighting + debounceTimeout = setTimeout(highlightCode, 10); }