This vignette shows some examples for different Markdown rendering options.
library(litedown)
# toc example
mkd <- c("# Header 1", "p1", "## Header 2", "p2")
mark(mkd, options = "+number_sections")
<h1 id="chp:header-1"><span class="section-number main-number">1</span> Header 1</h1>
<p>p1</p>
<h2 id="sec:header-2"><span class="section-number">1.1</span> Header 2</h2>
<p>p2</p>
mark(mkd, options = "+number_sections+toc")
<div id="TOC">
<ul class="numbered">
<li><a href="#chp:header-1"><span class="section-number main-number">1</span> Header 1</a>
<ul>
<li><a href="#sec:header-2"><span class="section-number">1.1</span> Header 2</a></li>
</ul>
</li>
</ul>
</div>
<h1 id="chp:header-1"><span class="section-number main-number">1</span> Header 1</h1>
<p>p1</p>
<h2 id="sec:header-2"><span class="section-number">1.1</span> Header 2</h2>
<p>p2</p>
# hard_wrap example
mark("foo\nbar\n")
<p>foo
bar</p>
mark("foo\nbar\n", options = "+hardbreaks")
<p>foo<br />
bar</p>
# latex math example
mkd <- c(
"`$x$` is inline math $x$!", "", "Display style:", "", "$$x + y$$", "",
"\\begin{align}
a^{2}+b^{2} & = c^{2}\\\\
\\sin^{2}(x)+\\cos^{2}(x) & = 1
\\end{align}"
)
mark(mkd)
<p><code>$x$</code> is inline math \(x\)!</p>
<p>Display style:</p>
<p>$$x + y$$</p>
<p>\begin{align}
a^{2}+b^{2} & = c^{2}\\
\sin^{2}(x)+\cos^{2}(x) & = 1
\end{align}</p>
mark(mkd, options = "-latex_math")
<p><code>$x$</code> is inline math $x$!</p>
<p>Display style:</p>
<p>$$x + y$$</p>
<p>\begin{align}
a^{2}+b^{2} & = c^{2}\
\sin^{2}(x)+\cos^{2}(x) & = 1
\end{align}</p>
# table example
mark("
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
")
<table>
<thead>
<tr>
<th>First Header</th>
<th>Second Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content Cell</td>
<td>Content Cell</td>
</tr>
<tr>
<td>Content Cell</td>
<td>Content Cell</td>
</tr>
</tbody>
</table>
# caption
mark("
| a | b |
|---|--:|
| A | 9 |
Table: A table _caption_.
")
<table>
<caption>A table <em>caption</em>.</caption>
<thead>
<tr>
<th>a</th>
<th align="right">b</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td align="right">9</td>
</tr>
</tbody>
</table>
# no table
mark("
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
", options = '-table')
<p>First Header | Second Header
———–– | ———––
Content Cell | Content Cell
Content Cell | Content Cell</p>
# autolink example
mark("https://www.r-project.org/")
<p><a href="https://www.r-project.org/">https://www.r-project.org/</a></p>
mark("https://www.r-project.org/", options = "-autolink")
<p>https://www.r-project.org/</p>
# strikethrough example
mark("~~awesome~~")
<p><del>awesome</del></p>
mark("~~awesome~~", options = "-strikethrough")
<p>~~awesome~~</p>
# superscript and subscript examples
mark("2^10^")
<p>2<sup>10</sup></p>
mark("2^10^", options = "-superscript")
<p>2^10^</p>
mark("H~2~O")
<p>H<sub>2</sub>O</p>
mark("H~2~O", options = "-subscript")
<p>H~2~O</p>
# code blocks
mark('```r\n1 + 1;\n```')
<pre><code class="language-r">1 + 1;
</code></pre>
mark('```{.r}\n1 + 1;\n```')
<pre><code class="language-r">1 + 1;
</code></pre>
mark('```{.r .js}\n1 + 1;\n```')
<pre><code class="language-r js">1 + 1;
</code></pre>
mark('```{.r .js #foo}\n1 + 1;\n```')
<pre><code class="language-r js" id="foo">1 + 1;
</code></pre>
mark('```{.r .js #foo style="background:lime;"}\n1 + 1;\n```')
<pre><code class="language-r js" id="foo" style="background:lime;">1 + 1;
</code></pre>
mark('````\nA _code chunk_:\n\n```{r, echo=TRUE}\n1 + 1;\n```\n````')
<pre><code>A _code chunk_:
```{r, echo=TRUE}
1 + 1;
```
</code></pre>
# raw blocks
mark('```{=html}\n<p>raw HTML</p>\n```')
<p>raw HTML</p>
mark('```{=latex}\n<p>raw HTML</p>\n```')
# filter out HTML tags
mkd = '<style>a {}</style><script type="text/javascript">console.log("No!");</script>\n[Hello](#)'
mark(mkd)
<style>a {}</style><script type="text/javascript">console.log("No!");</script>
<p><a href="#">Hello</a></p>
# tagfiler doesn't work: https://github.com/r-lib/commonmark/issues/15
# mark(mkd, options = "tagfilter")