Skip to content

Instantly share code, notes, and snippets.

@navanchauhan
Last active March 27, 2024 00:14
Show Gist options
  • Save navanchauhan/041c91d0181be95add33c26d61f6433f to your computer and use it in GitHub Desktop.
Save navanchauhan/041c91d0181be95add33c26d61f6433f to your computer and use it in GitHub Desktop.
from markdown2 import Markdown
md = Markdown(extras=["fenced-code-blocks", "latex"])
test_string = r"""## Simple Test
Inline Equations can be written as $y=mx+b$.
Block equations are wrapped using
$$
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
$$
This extra does not run inside fenced-blocks, so
```
some random code, describing $a and $b won't accidentally render $y=mx$
```
"""
print(md.convert(test_string))
""" Output:
<h2>Simple Test</h2>
<p>Inline Equations can be written as <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>y</mi><mo>&#x0003D;</mo><mi>m</mi><mi>x</mi><mo>&#x0002B;</mo><mi>b</mi></mrow></math>.</p>
<p>Block equations are wrapped using</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mi>x</mi><mo>&#x0003D;</mo><mfrac><mrow><mo>&#x02212;</mo><mi>b</mi><mi>&#x000B1;</mi><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo>&#x02212;</mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mrow></math>
<p>This extra does not run inside fenced-blocks, so </p>
<pre><code>some random code, describing $a and $b won't accidentally render $y=mx$
</code></pre>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment