Skip to content

Commit 7ef2d05

Browse files
committed
fix inline code in table cells
1 parent 19c67b9 commit 7ef2d05

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

rich/markdown.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,13 @@ def __init__(self, justify: JustifyMethod) -> None:
331331
self.justify = justify
332332

333333
def on_text(self, context: MarkdownContext, text: TextType) -> None:
334-
text = Text(text) if isinstance(text, str) else text
335-
text.stylize(context.current_style)
336-
self.content.append_text(text)
334+
if isinstance(text, str):
335+
self.content.append(text, context.current_style)
336+
else:
337+
self.content.append_text(text)
338+
# text = Text(text) if isinstance(text, str) else text
339+
# text.stylize(context.current_style)
340+
# self.content.append_text(text)
337341

338342

339343
class ListElement(MarkdownElement):

tests/test_markdown.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,22 @@ def test_table_with_empty_cells() -> None:
199199
assert result == expected
200200

201201

202+
def test_inline_code_in_table_cells() -> None:
203+
"""Test inline code in table cells.
204+
205+
Regression test for https://github.com/Textualize/rich/issues/4038
206+
207+
"""
208+
markdown = Markdown(
209+
"| Col |\n|---|\n| `print('hello');` |\n",
210+
inline_code_theme="monokai",
211+
inline_code_lexer="python",
212+
)
213+
result = render(markdown)
214+
expected = "\n\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m\x1b[36mCol\x1b[0m\x1b[1m \x1b[0m\x1b[36m \x1b[0m\n\x1b[36m ─────────────── \x1b[0m\n\x1b[36m \x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mprint\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m(\x1b[0m\x1b[38;2;230;219;116;48;2;39;40;34m'\x1b[0m\x1b[38;2;230;219;116;48;2;39;40;34mhello\x1b[0m\x1b[38;2;230;219;116;48;2;39;40;34m'\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m)\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m;\x1b[0m\x1b[36m \x1b[0m\n\x1b[36m \x1b[0m\n"
215+
assert result == expected
216+
217+
202218
if __name__ == "__main__":
203219
markdown = Markdown(MARKDOWN)
204220
rendered = render(markdown)

0 commit comments

Comments
 (0)