From 1cb68e4e3f1221c3c449d613fe5a1e6ff684fd6f Mon Sep 17 00:00:00 2001 From: Ed_ Date: Thu, 4 Jun 2026 23:13:33 -0400 Subject: [PATCH] feat(markdown): apply active theme syntax palette to code blocks --- src/markdown_helper.py | 7 +++++++ tests/test_markdown_render_robust.py | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/markdown_helper.py b/src/markdown_helper.py index 8307e03e..57bace74 100644 --- a/src/markdown_helper.py +++ b/src/markdown_helper.py @@ -73,6 +73,13 @@ class MarkdownRenderer: # Optional callback for custom local link handling (e.g., opening in IDE) self.on_local_link: Optional[Callable[[str], None]] = None + # Apply the current theme's syntax palette on construction so new + # editors we create pick up the right colors. The renderer is re-created + # when the theme changes (see theme_2 module-load behavior). + from src import theme_2 + palette_id = theme_2.get_syntax_palette_for_theme(theme_2.get_current_palette()) + theme_2.apply_syntax_palette(palette_id) + # Language mapping for ImGuiColorTextEdit self._lang_map = { "python": _get_language_id("python"), diff --git a/tests/test_markdown_render_robust.py b/tests/test_markdown_render_robust.py index b6df6072..be6cc3ca 100644 --- a/tests/test_markdown_render_robust.py +++ b/tests/test_markdown_render_robust.py @@ -54,3 +54,28 @@ def test_duplicate_table_content_both_get_replaced(): full = "".join(str(c) for c in mock_md.render.call_args_list) assert "| A | B |" not in full, f"Raw table text leaked on duplicate: {full!r}" + +def test_render_applies_current_syntax_palette(monkeypatch): + """When a theme is active, the render path should call apply_syntax_palette + so new code-block editors pick up the theme's syntax palette.""" + from unittest.mock import patch + from src import theme_2 as theme + + class _Tracker: + last_palette = None + last_call = False + + def fake_apply(palette_id): + _Tracker.last_palette = palette_id + _Tracker.last_call = True + + monkeypatch.setattr(theme, "apply_syntax_palette", fake_apply) + monkeypatch.setattr(theme, "_current_palette", "solarized_dark") + monkeypatch.setattr(theme, "get_syntax_palette_for_theme", lambda n: "dark") + + from src.markdown_helper import MarkdownRenderer + with patch("src.markdown_helper.imgui_md") as mock_md, patch("src.markdown_helper.imgui") as mock_imgui, patch("src.markdown_table.imgui") as mock_table_imgui: + _mock_table_calls(mock_table_imgui) + MarkdownRenderer().render("hello world", context_id="p") + assert _Tracker.last_call, "expected apply_syntax_palette to be called" +