forked from pulldown-cmark/pulldown-cmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Based on pulldown-cmark#622 and copied from https://github.com/ollpu/pulldown-cmark/tree/alt-math. Co-authored-by: rhysd <lin90162@yahoo.co.jp>
- Loading branch information
Showing
1 changed file
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
Run this with `cargo run -- -M -s specs/math.txt`. | ||
|
||
Mathematical expressions extension. Syntax based on | ||
<https://github.com/jgm/commonmark-hs/blob/master/commonmark-extensions/test/math.md> | ||
excluding the handling for balanced curly braces. | ||
|
||
Inline mode mathematical expressions: | ||
|
||
```````````````````````````````` example | ||
This sentence uses `$` delimiters to show math inline: $\sqrt{3x-1}+(1+x)^2$ | ||
$\sum_{k=1}^n a_k b_k$: Mathematical expression at head of line | ||
|
||
`\` may follow just after the first `$`: $\{1, 2, 3\}$ | ||
. | ||
<p>This sentence uses <code>$</code> delimiters to show math inline: <span class="math inline">\sqrt{3x-1}+(1+x)^2</span> | ||
<span class="math inline">\sum_{k=1}^n a_k b_k</span>: Mathematical expression at head of line</p> | ||
<p><code>\</code> may follow just after the first <code>$</code>: <span class="math inline">\{1, 2, 3\}</span> | ||
```````````````````````````````` | ||
|
||
Display mode mathematical expressions: | ||
|
||
```````````````````````````````` example | ||
**The Cauchy-Schwarz Inequality** | ||
|
||
$$\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)$$ | ||
. | ||
<p><strong>The Cauchy-Schwarz Inequality</strong></p> | ||
<p><span class="math display">\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)</span></p> | ||
```````````````````````````````` | ||
|
||
Inline math expressions cannot be empty, but display mode expressions can. | ||
|
||
```````````````````````````````` example | ||
Oops empty $$ expression. | ||
|
||
$$$$ | ||
. | ||
<p>Oops empty $$ expression.</p> | ||
<p><span class="math display"></span></p> | ||
```````````````````````````````` | ||
|
||
Math expressions pass their content through as-is, ignoring any other inline | ||
Markdown constructs: | ||
|
||
```````````````````````````````` example | ||
$a<b>c</b>$ | ||
|
||
$${a*b*c} _c_ d$$ | ||
|
||
$not `code`$ | ||
|
||
$![not an](/image)$ | ||
|
||
$<https://not.a.link/>$ | ||
|
||
$α$ | ||
. | ||
<p><span class="math inline">a<b>c</b></span></p> | ||
<p><span class="math display">{a*b*c} _c_ d</span></p> | ||
<p><span class="math inline">not `code`</span></p> | ||
<p><span class="math inline">![not an](/image)</span></p> | ||
<p><span class="math inline"><https://not.a.link/></span></p> | ||
<p><span class="math inline">&alpha;</span></p> | ||
```````````````````````````````` | ||
|
||
Sole `$` characters without a matching pair in the same block element | ||
are handled as normal text. | ||
|
||
```````````````````````````````` example | ||
Hello $world. | ||
|
||
Dollar at end of line$ | ||
. | ||
<p>Hello $world.</p> | ||
<p>Dollar at end of line$</p> | ||
```````````````````````````````` | ||
|
||
Mathematical expressions can continue across multiple lines: | ||
|
||
```````````````````````````````` example | ||
$5x + 2 = | ||
17$ | ||
|
||
$$\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) | ||
\left( \sum_{k=1}^n b_k^2 \right)$$ | ||
. | ||
<p><span class="math inline">5x + 2 = | ||
17</span></p> | ||
<p><span class="math display">\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) | ||
\left( \sum_{k=1}^n b_k^2 \right)</span></p> | ||
```````````````````````````````` | ||
|
||
Markdown hard breaks are also not recognized inside math expressions: | ||
|
||
```````````````````````````````` example | ||
$not a\ | ||
hard break | ||
either$ | ||
. | ||
<p><span class="math inline">not a\ | ||
hard break | ||
either</span></p> | ||
```````````````````````````````` | ||
|
||
|
||
`$` character can be escaped with backslash in mathematical expressions: | ||
|
||
```````````````````````````````` example | ||
$\$$ | ||
|
||
$$y = \$ x$$ | ||
. | ||
<p><span class="math inline">\$</span></p> | ||
<p><span class="math display">y = \$ x</span></p> | ||
```````````````````````````````` | ||
|
||
Inline mode math expressions cannot contain unescaped `$` characters, but | ||
display mode expressions can, though not two in a row: | ||
|
||
```````````````````````````````` example | ||
$x $ x$ | ||
|
||
$$ $ $$ | ||
|
||
$$ $$ $$ | ||
. | ||
<p>$x $ x$</p> | ||
<p><span class="math display"> $ </span></p> | ||
<p><span class="math display"> </span> $$</p> | ||
```````````````````````````````` | ||
|
||
Inline math expressions cannot start or end with whitespace, including newlines: | ||
|
||
```````````````````````````````` example | ||
these are not math texts: $ y=x$, $y=x $, $ | ||
y=x$ and $y=x | ||
$ | ||
. | ||
<p>these are not math texts: $ y=x$, $y=x $, $ | ||
y=x$ and $y=x | ||
$</p> | ||
```````````````````````````````` | ||
|
||
Inline math expressions do not need to be surrounded with whitespace: | ||
|
||
```````````````````````````````` example | ||
these are math texts: foo$y=x$bar and $y=x$bar and foo$y=x$ bar | ||
. | ||
<p>these are math texts: foo<span class="math inline">y=x</span>bar and <span class="math inline">y=x</span>bar and foo<span class="math inline">y=x</span> bar</p> | ||
```````````````````````````````` | ||
|
||
Inline math expressions can be surrounded by punctuation: | ||
|
||
```````````````````````````````` example | ||
math texts: $x=y$! and $x=y$? and $x=y$: and $x=y$. and $x=y$" | ||
|
||
also math texts: !$x=y$! and ?$x=y$? and :$x=y$: and .$x=y$. and "$x=y$" | ||
|
||
braces: ($x=y$) [$x=y$] {$x=y$} | ||
. | ||
<p>math texts: <span class="math inline">x=y</span>! and <span class="math inline">x=y</span>? and <span class="math inline">x=y</span>: and <span class="math inline">x=y</span>. and <span class="math inline">x=y</span>"</p> | ||
<p>also math texts: !<span class="math inline">x=y</span>! and ?<span class="math inline">x=y</span>? and :<span class="math inline">x=y</span>: and .<span class="math inline">x=y</span>. and "<span class="math inline">x=y</span>"</p> | ||
<p>braces: (<span class="math inline">x=y</span>) [<span class="math inline">x=y</span>] {<span class="math inline">x=y</span>}</p> | ||
```````````````````````````````` | ||
|
||
Math expression as only item on a line: | ||
|
||
```````````````````````````````` example | ||
$x=y$ | ||
. | ||
<p><span class="math inline">x=y</span></p> | ||
```````````````````````````````` | ||
|
||
Math expressions can be immediately followed by other math expressions: | ||
|
||
```````````````````````````````` example | ||
$a$$b$ | ||
|
||
$a$$$b$$ | ||
|
||
$$a$$$b$ | ||
|
||
$$a$$$$b$$ | ||
. | ||
<p><span class="math inline">a</span><span class="math inline">b</span></p> | ||
<p><span class="math inline">a</span><span class="math display">b</span></p> | ||
<p><span class="math display">a</span><span class="math inline">b</span></p> | ||
<p><span class="math display">a</span><span class="math display">b</span></p> | ||
```````````````````````````````` | ||
|
||
Both inline and display mode math expressions are inline elements with the same | ||
precedence as code spans. The leftmost valid element takes priority: | ||
|
||
```````````````````````````````` example | ||
$Inline `first$ then` code | ||
|
||
`Code $first` then$ inline | ||
|
||
$$ Display `first $$ then` code | ||
|
||
`Code $$ first` then $$ display | ||
. | ||
<p><span class="math inline">Inline `first</span> then` code</p> | ||
<p><code>Code $first</code> then$ inline</p> | ||
<p><span class="math display"> Display `first </span> then` code</p> | ||
<p><code>Code $$ first</code> then $$ display</p> | ||
```````````````````````````````` | ||
|
||
Indicators of block structure take precedence over math expressions: | ||
|
||
```````````````````````````````` example | ||
$x + y - z$ | ||
|
||
$x + y | ||
- z$ | ||
|
||
$$ x + y | ||
> z $$ | ||
. | ||
<p><span class="math inline">x + y - z</span></p> | ||
<p>$x + y</p> | ||
<ul> | ||
<li>z$</li> | ||
</ul> | ||
<p>$$ x + y</p> | ||
<blockquote> | ||
<p>z $$</p> | ||
</blockquote> | ||
```````````````````````````````` | ||
|
||
This also means that math expressions cannot contain empty lines, since they | ||
start a new paragraph: | ||
|
||
```````````````````````````````` example | ||
$not | ||
|
||
math$ | ||
|
||
$$ | ||
not | ||
|
||
math | ||
$$ | ||
. | ||
<p>$not</p> | ||
<p>math$</p> | ||
<p>$$ | ||
not</p> | ||
<p>math | ||
$$</p> | ||
```````````````````````````````` |