Skip to content

Commit

Permalink
fix: Don't wrap HTML in p tag
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Nov 12, 2022
1 parent 46e74e4 commit 420d79d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/markdown_exec/formatters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ def base_format( # noqa: WPS231
except RuntimeError as error:
logger.warning(f"Execution of {language} code block exited with non-zero status")
return markdown.convert(str(error))
stash = {}
if html:
if html and source:
placeholder = str(uuid4())
stash[placeholder] = output
output = placeholder
wrapped_output = add_source(
source=code, location=source, output=placeholder, language=language, tabs=tabs, **extra
)
markup = markdown.convert(wrapped_output, stash={placeholder: output})
elif html:
markup = Markup(output)
elif result:
output = code_block(result, output)
if source:
output = add_source(source=code, location=source, output=output, language=language, tabs=tabs, **extra)
return markdown.convert(output, stash=stash)
wrapped_output = code_block(result, output)
markup = markdown.convert(wrapped_output)
else:
markup = markdown.convert(output)
return markup
17 changes: 17 additions & 0 deletions tests/test_base_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Tests for the base formatter."""


from markdown import Markdown

from markdown_exec.formatters.base import base_format


def test_no_p_around_html(md: Markdown) -> None:
"""Assert HTML isn't wrapped in a `p` tag.
Parameters:
md: A Markdown instance (fixture).
"""
code = "<pre><code>hello</code></pre>"
html = base_format("whatever", lambda _: _, code, md, html=True, source="", result="", tabs=("", ""))
assert html == code

0 comments on commit 420d79d

Please sign in to comment.