Skip to content

Commit

Permalink
Match markedjs behavior for parsing HTML linebreaks
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Apr 20, 2022
1 parent 6876519 commit 4142976
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions nbconvert/filters/markdown_mistune.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def output_latex_environment(self, m):

class MarkdownWithMath(mistune.Markdown):
def __init__(self, renderer, **kwargs):
kwargs["parse_block_html"] = True

if "inline" not in kwargs:
kwargs["inline"] = MathInlineLexer
if "block" not in kwargs:
Expand All @@ -115,6 +117,18 @@ def __init__(self, renderer, **kwargs):
def output_multiline_math(self):
return self.inline(self.token["text"])

def output_open_html(self):
# This makes mistune behave more like markedjs
# (markedjs behing used by Jupyter Notebook and JupyterLab)
MathInlineGrammar.linebreak = re.compile(r'^ *\n(?!\s*$)')
MathInlineGrammar.text = re.compile(
r'^[\s\S]+?(?=[\\<!\[_*`~]|https?://| *\n|$)'
)
out = super().output_open_html()
MathInlineGrammar.linebreak = re.compile(r'^ {2,}\n(?!\s*$)')
MathInlineGrammar.text = re.compile(r'^[\s\S]+?(?=[\\<!\[_*`~]|https?://| {2,}\n|$)')
return out


class IPythonRenderer(mistune.Renderer):
def block_code(self, code, lang):
Expand Down

0 comments on commit 4142976

Please sign in to comment.