Skip to content

Commit 4142976

Browse files
committed
Match markedjs behavior for parsing HTML linebreaks
1 parent 6876519 commit 4142976

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

nbconvert/filters/markdown_mistune.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def output_latex_environment(self, m):
106106

107107
class MarkdownWithMath(mistune.Markdown):
108108
def __init__(self, renderer, **kwargs):
109+
kwargs["parse_block_html"] = True
110+
109111
if "inline" not in kwargs:
110112
kwargs["inline"] = MathInlineLexer
111113
if "block" not in kwargs:
@@ -115,6 +117,18 @@ def __init__(self, renderer, **kwargs):
115117
def output_multiline_math(self):
116118
return self.inline(self.token["text"])
117119

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

119133
class IPythonRenderer(mistune.Renderer):
120134
def block_code(self, code, lang):

0 commit comments

Comments
 (0)