Skip to content

Commit 5c018a7

Browse files
committed
fix(mdviewer): run renderAfterHTML on first load for code block sync
Call renderAfterHTML after createEntry and on cache-hit-unchanged switch so Prism highlighting and per-line source annotations run on first document load, not just on subsequent morphdom updates. Adds stronger cursor-sync highlight for code blocks where the dark background makes the default too faint.
1 parent 1238e9b commit 5c018a7

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

src-mdviewer/src/bridge.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { setLocale } from "./core/i18n.js";
99
import { marked } from "marked";
1010
import * as docCache from "./core/doc-cache.js";
1111
import { broadcastSelectionStateSync } from "./components/editor.js";
12+
import { renderAfterHTML } from "./components/viewer.js";
1213

1314
let _syncId = 0;
1415
let _lastReceivedSyncId = -1;
@@ -571,6 +572,12 @@ function handleSetContent(data) {
571572
docCache.switchTo(filePath);
572573
}
573574

575+
// Run post-render processing (Prism highlighting, code block line annotation)
576+
const content = document.getElementById("viewer-content");
577+
if (content) {
578+
renderAfterHTML(content, parseResult);
579+
}
580+
574581
setState({
575582
currentContent: markdown,
576583
parseResult: parseResult
@@ -668,6 +675,13 @@ function handleSwitchFile(data) {
668675
// Cache hit, content unchanged — instant switch
669676
docCache.switchTo(filePath);
670677

678+
// Ensure code blocks are highlighted and line-annotated (may be missing on first load)
679+
const cachedContent = document.getElementById("viewer-content");
680+
if (cachedContent && !cachedContent.querySelector("pre code span[data-source-line]") &&
681+
cachedContent.querySelector("pre[data-source-line]")) {
682+
renderAfterHTML(cachedContent, existing.parseResult);
683+
}
684+
671685
setState({
672686
currentContent: markdown,
673687
parseResult: existing.parseResult
@@ -692,6 +706,12 @@ function handleSwitchFile(data) {
692706
docCache.createEntry(filePath, markdown, parseResult);
693707
docCache.switchTo(filePath);
694708

709+
// Run post-render processing on newly created entry
710+
const newContent = document.getElementById("viewer-content");
711+
if (newContent) {
712+
renderAfterHTML(newContent, parseResult);
713+
}
714+
695715
// Restore scroll position and edit mode from reload if applicable
696716
if (_pendingReloadScroll && _pendingReloadScroll.filePath === filePath) {
697717
const entry = docCache.getEntry(filePath);

src-mdviewer/src/styles/markdown.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,15 @@
484484
background-color: rgba(50, 100, 220, 0.14);
485485
}
486486

487+
/* Stronger highlight inside code blocks (dark bg makes the default too faint) */
488+
pre .cursor-sync-highlight {
489+
background-color: rgba(100, 150, 255, 0.25);
490+
}
491+
492+
[data-theme="light"] pre .cursor-sync-highlight {
493+
background-color: rgba(50, 100, 220, 0.18);
494+
}
495+
487496
/* ===== Mermaid diagrams ===== */
488497

489498
.mermaid-diagram {

0 commit comments

Comments
 (0)