Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(markdown): render table without extra newlines above #3080

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- Fixed a bug with extra space above a markdown table with empty cells https://github.com/Textualize/rich/issues/3027

## [13.7.0] - 2023-11-15

### Added
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The following people have contributed to the development of Rich:
- [Aryaz Eghbali](https://github.com/AryazE)
- [Oleksis Fraga](https://github.com/oleksis)
- [Andy Gimblett](https://github.com/gimbo)
- [Tom Gooding](https://github.com/TomJGooding)
- [Michał Górny](https://github.com/mgorny)
- [Nok Lam Chan](https://github.com/noklam)
- [Leron Gray](https://github.com/daddycocoaman)
Expand Down
4 changes: 4 additions & 0 deletions rich/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ def on_child_close(
class TableRowElement(MarkdownElement):
"""MarkdownElement corresponding to `tr_open` and `tr_close`."""

new_line = False

def __init__(self) -> None:
self.cells: List[TableDataElement] = []

Expand All @@ -312,6 +314,8 @@ class TableDataElement(MarkdownElement):
"""MarkdownElement corresponding to `td_open` and `td_close`
and `th_open` and `th_close`."""

new_line = False

@classmethod
def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
style = str(token.attrs.get("style")) or ""
Expand Down
26 changes: 26 additions & 0 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,32 @@ def test_partial_table():
assert result == expected


def test_table_with_empty_cells():
"""Test a table with empty cells is rendered without extra newlines above.

Regression test for #3027 https://github.com/Textualize/rich/issues/3027
"""
complete_table = Markdown(
"""\
| First Header | Second Header |
| ------------- | ------------- |
| Content Cell | Content Cell |
| Content Cell | Content Cell |
"""
)
table_with_empty_cells = Markdown(
"""\
| First Header | |
| ------------- | ------------- |
| Content Cell | Content Cell |
| | Content Cell |
"""
)
result = len(render(table_with_empty_cells).splitlines())
expected = len(render(complete_table).splitlines())
assert result == expected


if __name__ == "__main__":
markdown = Markdown(MARKDOWN)
rendered = render(markdown)
Expand Down