Skip to content
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

Match markedjs behavior for parsing HTML linebreaks #1763

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 12 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,16 @@ 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