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.
When using
Markdownwithinline_code_lexerandinline_code_theme, inline code (backticks) inside table cells loses its syntax highlighting and falls back to the defaultmarkdown.codestyle. The same inline code renders correctly in paragraphs, headings, and list items.Root cause:
TableDataElement.on_text(markdown.py) unconditionally appliescontext.current_styleover the text:When
MarkdownContext.on_textprocesses acode_inlinetoken, it produces aTextobject with per-character syntax-highlight spans. ButTableDataElement.on_textthen callstext.stylize(context.current_style)on thatTextobject, which overwrites all the highlight spans with the flatmarkdown.codestyle.Compare with
TextElement.on_text, which correctly avoids this:Here, the style is only applied when
textis a plain string. When it's already a highlightedTextobject,Noneis passed, preserving the spans.Fix:
TableDataElement.on_textshould follow the same pattern:Repro:
The standalone
`hello`renders with the monokai theme; the one inside the table does not.