Skip to content

[BUG] TableDataElement.on_text clobbers syntax-highlight spans for inline code #4038

@jph00

Description

@jph00

When using Markdown with inline_code_lexer and inline_code_theme, inline code (backticks) inside table cells loses its syntax highlighting and falls back to the default markdown.code style. The same inline code renders correctly in paragraphs, headings, and list items.

Root cause:

TableDataElement.on_text (markdown.py) unconditionally applies context.current_style over the text:

def on_text(self, context: MarkdownContext, text: TextType) -> None:
    text = Text(text) if isinstance(text, str) else text
    text.stylize(context.current_style)
    self.content.append_text(text)

When MarkdownContext.on_text processes a code_inline token, it produces a Text object with per-character syntax-highlight spans. But TableDataElement.on_text then calls text.stylize(context.current_style) on that Text object, which overwrites all the highlight spans with the flat markdown.code style.

Compare with TextElement.on_text, which correctly avoids this:

def on_text(self, context: MarkdownContext, text: TextType) -> None:
    self.text.append(text, context.current_style if isinstance(text, str) else None)

Here, the style is only applied when text is a plain string. When it's already a highlighted Text object, None is passed, preserving the spans.

Fix:

TableDataElement.on_text should follow the same pattern:

def on_text(self, context: MarkdownContext, text: TextType) -> None:
    if isinstance(text, str):
        self.content.append(text, context.current_style)
    else:
        self.content.append_text(text)

Repro:

from rich.console import Console
from rich.markdown import Markdown

md = Markdown(
    "| Col |\n|---|\n| `hello` |\n\n`hello`",
    inline_code_theme="monokai",
    inline_code_lexer="python",
)
Console().print(md)

The standalone `hello` renders with the monokai theme; the one inside the table does not.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions