Skip to content

feat: Add breaks option to Markdown #3617

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion rich/markdown.py
Original file line number Diff line number Diff line change
@@ -513,6 +513,7 @@ class Markdown(JupyterMixin):
enabled. Defaults to None.
inline_code_theme: (Optional[str], optional): Pygments theme for inline code
highlighting, or None for no highlighting. Defaults to None.
breaks (bool, optional): Enable line breaks. Defaults to False.
"""

elements: ClassVar[dict[str, type[MarkdownElement]]] = {
@@ -545,6 +546,7 @@ def __init__(
hyperlinks: bool = True,
inline_code_lexer: str | None = None,
inline_code_theme: str | None = None,
breaks: bool = False,
) -> None:
parser = MarkdownIt().enable("strikethrough").enable("table")
self.markup = markup
@@ -555,6 +557,7 @@ def __init__(
self.hyperlinks = hyperlinks
self.inline_code_lexer = inline_code_lexer
self.inline_code_theme = inline_code_theme or code_theme
self.breaks = breaks

def _flatten_tokens(self, tokens: Iterable[Token]) -> Iterable[Token]:
"""Flattens the token stream."""
@@ -597,7 +600,7 @@ def __rich_console__(
elif node_type == "hardbreak":
context.on_text("\n", node_type)
elif node_type == "softbreak":
context.on_text(" ", node_type)
context.on_text("\n" if self.breaks else " ", node_type)
elif node_type == "link_open":
href = str(token.attrs.get("href", ""))
if self.hyperlinks:
8 changes: 8 additions & 0 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
@@ -198,6 +198,14 @@ def test_table_with_empty_cells() -> None:
expected = len(render(complete_table).splitlines())
assert result == expected

# write a test where markdown breaks is set to true
def test_markdown_render_breaks():
markdown = Markdown("this is a breaks test", breaks=True)
result = render(markdown)
print(repr(result))

expected = ('this is a breaks test \n')
assert result == expected

if __name__ == "__main__":
markdown = Markdown(MARKDOWN)