This vignette shows some examples for different Markdown extensions and HTML options.

library(markdown)

Markdown Extensions

# The following examples are short, so we set the HTML option 'fragment_only'

options(markdown.HTML.options = "fragment_only")

# no_intra_emphasis example
cat(markdownToHTML(text = "foo_bar_function", extensions = c()))
## <p>foo<em>bar</em>function</p>
cat(markdownToHTML(text = "foo_bar_function", extensions = c("no_intra_emphasis")))
## <p>foo_bar_function</p>
# tables example (need 4 spaces at beginning of line here)
cat(markdownToHTML(text = "
    First Header  | Second Header
    ------------- | -------------
    Content Cell  | Content Cell
    Content Cell  | Content Cell
", extensions = c()))
## <pre><code>First Header  | Second Header
## ------------- | -------------
## Content Cell  | Content Cell
## Content Cell  | Content Cell
## </code></pre>
# but not here
cat(markdownToHTML(text = "
First Header  | Second Header
------------- | -------------
Content Cell  | Content Cell
Content Cell  | Content Cell
", extensions = c("tables")))
## <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>
# fenced_code example (need at least three leading ~ or `)
fenced_block <- function(text, x = "`", n = 3) {
  fence <- paste(rep(x, n), collapse = "")
  paste(fence, text, fence, sep = "")
}
cat(markdownToHTML(text = fenced_block("
preformatted text here without having to indent
first line.
"), extensions = c()))
## <p><code>
## preformatted text here without having to indent
## first line.
## </code></p>
cat(markdownToHTML(text = fenced_block("
preformatted text here without having to indent
first line.
"), extensions = c("fenced_code")))
## <pre><code>preformatted text here without having to indent
## first line.
## </code></pre>
# autolink example
cat(markdownToHTML(text = "https://www.r-project.org/", extensions = c()))
## <p>https://www.r-project.org/</p>
cat(markdownToHTML(text = "https://www.r-project.org/", extensions = c("autolink")))
## <p><a href="https://www.r-project.org/">https://www.r-project.org/</a></p>
# strikethrough example
cat(markdownToHTML(text = "~~awesome~~", extensions = c()))
## <p>~~awesome~~</p>
cat(markdownToHTML(text = "~~awesome~~", extensions = c("strikethrough")))
## <p><del>awesome</del></p>
# lax_spacing
cat(markdownToHTML(text = "
Embedding html without surrounding with empty newline.
<div>_markdown_</div>
extra text.
", extensions = c("")))
## <p>Embedding html without surrounding with empty newline.
## <div><em>markdown</em></div>
## extra text.</p>
cat(markdownToHTML(text = "
Embedding html without surrounding with empty newline.
<div>_markdown_</div>
extra text.
", extensions = c("lax_spacing")))
## <p>Embedding html without surrounding with empty newline.</p>
## 
## <div>_markdown_</div>
## 
## <p>extra text.</p>
# space_headers example
cat(markdownToHTML(text = "#A Header\neven though there is no space between # and A",
                   extensions = c("")))
## <h1>A Header</h1>
## 
## <p>even though there is no space between # and A</p>
cat(markdownToHTML(text = "#Not A Header\nbecause there is no space between # and N",
                   extensions = c("space_headers")))
## <p>#Not A Header
## because there is no space between # and N</p>
# superscript example
cat(markdownToHTML(text = "2^10", extensions = c()))
## <p>2^10</p>
cat(markdownToHTML(text = "2^10", extensions = c("superscript")))
## <p>2<sup>10</sup></p>

HTML Options

# HTML OPTIONS

# The following examples are short, so we allways add the HTML option 'fragment_only'
tOpt <- "fragment_only"

# skip_html example
mkd = '<style></style><img src="http://cran.rstudio.com/Rlogo.jpg"><a href="#">Hello</a>'
cat(markdownToHTML(text = mkd, options = c(tOpt)))
## <p><style></style><img src="http://cran.rstudio.com/Rlogo.jpg"><a href="#">Hello</a></p>
cat(markdownToHTML(text = mkd, options = c(tOpt, "skip_html")))
## <p>Hello</p>
# skip_style example
cat(markdownToHTML(text = mkd, options = c(tOpt)))
## <p><style></style><img src="http://cran.rstudio.com/Rlogo.jpg"><a href="#">Hello</a></p>
cat(markdownToHTML(text = mkd, options = c(tOpt, "skip_style")))
## <p><img src="http://cran.rstudio.com/Rlogo.jpg"><a href="#">Hello</a></p>
# skip_images example
cat(markdownToHTML(text = mkd, options = c(tOpt)))
## <p><style></style><img src="http://cran.rstudio.com/Rlogo.jpg"><a href="#">Hello</a></p>
cat(markdownToHTML(text = mkd, options = c(tOpt, "skip_images")))
## <p><style></style><a href="#">Hello</a></p>
# skip_links example
cat(markdownToHTML(text = mkd, options = c(tOpt)))
## <p><style></style><img src="http://cran.rstudio.com/Rlogo.jpg"><a href="#">Hello</a></p>
cat(markdownToHTML(text = mkd, options = c(tOpt, "skip_links")))
## <p><style></style><img src="http://cran.rstudio.com/Rlogo.jpg">Hello</p>
# safelink example
cat(markdownToHTML(text = '[foo](http://google.com "baz")', options = c(tOpt)))
## <p><a href="http://google.com" title="baz">foo</a></p>
cat(markdownToHTML(text = '[foo](http://google.com "baz")', options = c(tOpt, "safelink")))
## <p><a href="http://google.com" title="baz">foo</a></p>
# toc example
mkd <- paste(c("# Header 1", "p1", "## Header 2", "p2"), collapse = "\n")

cat(markdownToHTML(text = mkd, options = c(tOpt)))
## <h1>Header 1</h1>
## 
## <p>p1</p>
## 
## <h2>Header 2</h2>
## 
## <p>p2</p>
cat(markdownToHTML(text = mkd, options = c(tOpt, "toc")))
## <div id="toc">
## <div id="toc_header">Table of Contents</div>
## <ul>
## <li>
## <a href="#toc_0">Header 1</a>
## <ul>
## <li>
## <a href="#toc_1">Header 2</a>
## </li>
## </ul>
## </li>
## </ul>
## </div>
## 
## 
## <h1 id="toc_0">Header 1</h1>
## 
## <p>p1</p>
## 
## <h2 id="toc_1">Header 2</h2>
## 
## <p>p2</p>
# hard_wrap example
cat(markdownToHTML(text = "foo\nbar\n", options = c(tOpt)))
## <p>foo
## bar</p>
cat(markdownToHTML(text = "foo\nbar\n", options = c(tOpt, "hard_wrap")))
## <p>foo<br>
## bar</p>
# use_xhtml example
cat(markdownToHTML(text = "foo\nbar\n", options = c(tOpt, "hard_wrap")))
## <p>foo<br>
## bar</p>
cat(markdownToHTML(text = "foo\nbar\n", options = c(tOpt, "hard_wrap", "use_xhtml")))
## <p>foo<br/>
## bar</p>
# escape example
mkd = '<style></style><img src="http://cran.rstudio.com/Rlogo.jpg"><a href="#">Hello</a>'
cat(markdownToHTML(text = mkd, options = c(tOpt, "skip_html")))
## <p>Hello</p>
# overrides all 'skip_*' options
cat(markdownToHTML(text = mkd, options = c(tOpt, "skip_html", "escape")))
## <p>&lt;style&gt;&lt;/style&gt;&lt;img src=&quot;http://cran.rstudio.com/Rlogo.jpg&quot;&gt;&lt;a href=&quot;#&quot;&gt;Hello&lt;/a&gt;</p>
# smartypants example
cat(markdownToHTML(text = "1/2 (c)", options = c(tOpt)))
## <p>1/2 (c)</p>
cat(markdownToHTML(text = "1/2 (c)", options = c(tOpt, "smartypants")))
## <p>&frac12; &copy;</p>
cat(smartypants(text = "1/2 (c)\n"))
## &frac12; &copy;