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

dollarmath: Add allow_blank_lines option #46

Merged
merged 11 commits into from
Mar 6, 2023
5 changes: 2 additions & 3 deletions mdit_py_plugins/dollarmath/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,14 @@ def _math_block_dollar(
start = state.bMarks[nextLine] + state.tShift[nextLine]
end = state.eMarks[nextLine]

if end - start < 2:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should stay (but break not continue) to safeguard against any index errors

Copy link
Contributor Author

@eric-wieser eric-wieser Jun 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line doesn't exist any more in the javascript version, why would you want to keep it in the Python version?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because JavaScript does not act the same as Python; if you index a JavaScript array with "bad indexes" it will simply return null, whereas in python it will raise an indexerror.
There has been multiple bug fixed for this in markdown-it-py

Copy link
Contributor Author

@eric-wieser eric-wieser Jun 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only indexing that takes place is state.src[start:end], and that will never give an IndexError. If start=end then it will return "", but that's absolutely fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rowanc1: what can we do to progress here?

continue

lineText = state.src[start:end]

if lineText.strip().endswith("$$"):
haveEndMarker = True
end = end - 2 - (len(lineText) - len(lineText.strip()))
break
if lineText.strip() == "":
break # blank lines are not allowed within $$

# reverse the line and match
if allow_labels:
Expand Down
22 changes: 12 additions & 10 deletions tests/fixtures/dollar_math.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ b = 2
</div>
.

display equation with blank lines. (valid=False)
.
$$
1+1=2

$$
.
<p>$$
1+1=2</p>
<p>$$</p>
.

equation followed by a labelled equation (valid=True)
.
$$
Expand Down Expand Up @@ -540,13 +552,3 @@ i.e., $[\alpha \bar{X}, \infty)$ is a lower 1-sided $1-\alpha$ confidence bound
<div class="math inline">\mathbb P (\alpha \bar{X} \ge \mu) \le \alpha;</div>
i.e., <span class="math inline">[\alpha \bar{X}, \infty)</span> is a lower 1-sided <span class="math inline">1-\alpha</span> confidence bound for <span class="math inline">\mu</span>.</p>
.

display equation with label containing whitespace. (valid=True)
.
$$1+1=2$$ (a b)
.
<div id="a-b" class="math block">
<a href="#a-b" class="mathlabel" title="Permalink to this equation">¶</a>
1+1=2
</div>
.
eric-wieser marked this conversation as resolved.
Show resolved Hide resolved