From 6bd1bb082aca9c8d5d68bae8b2e24c049d24ee3c Mon Sep 17 00:00:00 2001 From: Jared Vititoe Date: Fri, 11 Sep 2026 14:28:39 -0400 Subject: [PATCH] Debounce the live markdown preview (#108) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updatePreview() was bound directly to the comment textarea's 'input' event with no debounce, re-running the full markdown parser (regex passes for headings, tables, links, footnotes, etc.) on every single keystroke. Wrapped it with the existing lt.debounce() helper (150ms) — the initial preview render on enabling the toggle still happens immediately; only the per-keystroke live updates are debounced. Verified via jsdom with real timers: 10 rapid keystrokes within the debounce window produce exactly one parse call instead of ten. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Lhz7pGMaoTfL5sdYS5XiKv --- assets/js/ticket.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/assets/js/ticket.js b/assets/js/ticket.js index bc0aea0..f40cab2 100644 --- a/assets/js/ticket.js +++ b/assets/js/ticket.js @@ -357,12 +357,17 @@ function togglePreview() { if (isPreviewEnabled) { preview.innerHTML = parseMarkdown(textarea.value); - textarea.addEventListener('input', updatePreview); + textarea.addEventListener('input', debouncedUpdatePreview); } else { - textarea.removeEventListener('input', updatePreview); + textarea.removeEventListener('input', debouncedUpdatePreview); } } +// Re-running the full markdown parser on every single keystroke is wasted +// work while the user is still mid-word; 150ms debounce keeps the preview +// feeling live without re-parsing on every keystroke. +const debouncedUpdatePreview = window.lt ? lt.debounce(updatePreview, 150) : updatePreview; + function updatePreview() { const textarea = document.getElementById('newComment'); const previewDiv = document.getElementById('markdownPreview');