This commit is contained in:
Jan Szczuka 2025-11-10 12:16:25 +08:00 committed by GitHub
commit 2865c8d19b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View file

@ -508,6 +508,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]]] = {
@ -540,6 +541,7 @@ class Markdown(JupyterMixin):
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
@ -550,6 +552,7 @@ class Markdown(JupyterMixin):
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."""
@ -592,7 +595,7 @@ class Markdown(JupyterMixin):
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:

View file

@ -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)