Skip to content

Commit 68f7395

Browse files
authored
Fix fragment navigation for gistpreview.github.io URLs (#13)
> The gistpreview mechanis has a bug: fragment links like https://gistpreview.github.io/?edbd5ddcb39d1edc9e175f1bf7b9ef9a/page-001.html#msg-2025-12-26T15-30-45-910Z do not naught the user to the right point, presumably because the content has not loaded in time > > Fix that with more JavaScript in the existing gistpreview JavaScript When accessing a gistpreview URL with a fragment (e.g. #msg-2025-12-26T15-30-45-910Z), the browser's native fragment navigation fails because gistpreview loads content dynamically. By the time the browser tries to scroll to the element, it doesn't exist yet. Added JavaScript that: - Checks for fragment in window.location.hash - Uses scrollIntoView to navigate to the target element - Retries with increasing delays (100ms, 300ms, 500ms, 1s) to handle dynamic content loading https://gistpreview.github.io/?883ee001b0d63a2045f1e2c7c2598a9f/index.html
1 parent 5b65cbc commit 68f7395

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/claude_code_transcripts/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,30 @@ def render_message(log_type, message_json, timestamp):
747747
var anchor = parts.length > 1 ? '#' + parts[1] : '';
748748
link.setAttribute('href', '?' + gistId + '/' + filename + anchor);
749749
});
750+
751+
// Handle fragment navigation after dynamic content loads
752+
// gistpreview.github.io loads content dynamically, so the browser's
753+
// native fragment navigation fails because the element doesn't exist yet
754+
function scrollToFragment() {
755+
var hash = window.location.hash;
756+
if (!hash) return false;
757+
var targetId = hash.substring(1);
758+
var target = document.getElementById(targetId);
759+
if (target) {
760+
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
761+
return true;
762+
}
763+
return false;
764+
}
765+
766+
// Try immediately in case content is already loaded
767+
if (!scrollToFragment()) {
768+
// Retry with increasing delays to handle dynamic content loading
769+
var delays = [100, 300, 500, 1000];
770+
delays.forEach(function(delay) {
771+
setTimeout(scrollToFragment, delay);
772+
});
773+
}
750774
})();
751775
"""
752776

tests/test_generate_html.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,24 @@ def test_injects_js_into_html_files(self, output_dir):
365365
assert index_content.endswith("</body></html>")
366366
assert "<script>" in index_content
367367

368+
def test_gist_preview_js_handles_fragment_navigation(self):
369+
"""Test that GIST_PREVIEW_JS includes fragment navigation handling.
370+
371+
When accessing a gistpreview URL with a fragment like:
372+
https://gistpreview.github.io/?GIST_ID/page-001.html#msg-2025-12-26T15-30-45-910Z
373+
374+
The content loads dynamically, so the browser's native fragment
375+
navigation fails because the element doesn't exist yet. The JS
376+
should scroll to the fragment element after content loads.
377+
"""
378+
# The JS should check for fragment in URL
379+
assert (
380+
"location.hash" in GIST_PREVIEW_JS
381+
or "window.location.hash" in GIST_PREVIEW_JS
382+
)
383+
# The JS should scroll to the element
384+
assert "scrollIntoView" in GIST_PREVIEW_JS
385+
368386
def test_skips_files_without_body(self, output_dir):
369387
"""Test that files without </body> are not modified."""
370388
original_content = "<html><head><title>Test</title></head></html>"

0 commit comments

Comments
 (0)