feat(markdown): intercept GFM tables and render via imgui.begin_table
This commit is contained in:
+22
-7
@@ -108,23 +108,38 @@ class MarkdownRenderer:
|
||||
|
||||
def render(self, text: str, context_id: str = "default") -> None:
|
||||
"""
|
||||
|
||||
Render Markdown text with code block interception.
|
||||
|
||||
Render Markdown text with code block interception and GFM table substitution.
|
||||
[C: src/theme_2.py:render_post_fx, tests/test_theme_nerv_alert.py:test_alert_pulsing_render_active, tests/test_theme_nerv_alert.py:test_alert_pulsing_render_inactive, tests/test_theme_nerv_fx.py:TestThemeNervFx.test_alert_pulsing_render, tests/test_theme_nerv_fx.py:TestThemeNervFx.test_crt_filter_disabled, tests/test_theme_nerv_fx.py:TestThemeNervFx.test_crt_filter_render]
|
||||
"""
|
||||
if not text:
|
||||
return
|
||||
|
||||
# Split into markdown and code blocks
|
||||
parts = re.split(r'(```[\s\S]*?```)', text)
|
||||
|
||||
from src.markdown_table import parse_tables, render_table
|
||||
blocks = parse_tables(text)
|
||||
sentinel = "\x00TBL{}\x00"
|
||||
masked = text
|
||||
for idx, block in enumerate(blocks):
|
||||
start, end = block.span
|
||||
original_block = "\n".join(masked.splitlines()[start:end])
|
||||
masked = masked.replace(original_block, sentinel.format(idx), 1)
|
||||
|
||||
parts = re.split(r"(```[\s\S]*?```)", masked)
|
||||
|
||||
block_idx = 0
|
||||
for part in parts:
|
||||
if part.startswith('```') and part.endswith('```'):
|
||||
if part.startswith("```") and part.endswith("```"):
|
||||
self._render_code_block(part, context_id, block_idx)
|
||||
block_idx += 1
|
||||
elif part.strip():
|
||||
imgui_md.render(part)
|
||||
sub_parts = re.split(r"(\x00TBL\d+\x00)", part)
|
||||
for sp in sub_parts:
|
||||
if sp.startswith("\x00TBL") and sp.endswith("\x00"):
|
||||
tbl_idx = int(sp[4:-1])
|
||||
try: render_table(blocks[tbl_idx])
|
||||
except Exception: imgui.text(sp)
|
||||
else:
|
||||
if sp.strip(): imgui_md.render(sp)
|
||||
|
||||
def render_unindented(self, text: str) -> None:
|
||||
"""Render Markdown text with automatic unindentation."""
|
||||
|
||||
Reference in New Issue
Block a user