<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/rstudio-pandoc-html-to-markdown/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.yihanwu.ca/post/rstudio-pandoc-html-to-markdown/"> R on YIHAN WU</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The <code>knitr</code> and <code>rmarkdown</code> packages are used in conjunction with <a rel="nofollow" href="https://pandoc.org/" target="_blank">pandoc</a> to convert R code and figures to a variety of formats including PDF, and word. Here, I’m exploring how to convert HTML back to markdown format. This post came about when I was searching how to convert XML to markdown, which I still haven’t found an easy way to do. Pandoc is not the only way to convert HTML to markdown (see <a rel="nofollow" href="https://domchristie.github.io/turndown/" target="_blank">turndown</a>, <a rel="nofollow" href="https://github.com/aaronsw/html2text" target="_blank">html2text</a>)</p>
<p>Pandoc is packaged within RStudio and on Windows, the executables are located within <code>Program Files/RStudio/bin/pandoc</code>. The <code>rmarkdown</code> package contains wrapper functions for using pandoc within RStudio.</p>
<p>Here, I am trying to convert this <a rel="nofollow" href="https://www.dummies.com/web-design-development/site-development/a-sample-web-page-in-html/" target="_blank">example HTML page</a> back to markdown using the function <code>pandoc_convert</code>. First, <code>pandoc_convert</code> requires an actual file which means it does not accept a quoted string of HTML code in its <code>input</code> argument.</p>
<p></p>
<p>The example html:</p>
<pre><code></code></pre>
<p>I saved the HTML example <a rel="nofollow" href="https://www.yihanwu.ca/static/files/example.html" target="_blank">here</a> as <code>example.html</code>.</p>
<pre><code>html_page &lt;- readLines(&quot;../../static/files/example.html&quot;)</code></pre>
<p>We can print the object in R.</p>
<pre><code>cat(html_page)</code></pre>
<pre><code>## </code></pre>
<p><code>pandoc</code> can convert between many different formats, and for markdown, it has multiple variants including the github flavored variant (for Github), and php markdown extra (the variant used by WordPress sites).</p>
<p>The safest variant to pick is <code>markdown_strict</code> which is the original <code>markdown</code> variant.</p>
<p>Pandoc requires the file path which in my case, is located in a different directory rather than my working directory.</p>
<pre><code>library(rmarkdown)
file_path &lt;- &quot;../../static/files/example.html&quot;
pandoc_convert(file_path, to = &quot;markdown_strict&quot;)</code></pre>
<pre><code>Enter the main heading, usually the same as the title.
======================================================

Be **bold** in stating your key points. Put them in a list:

-   The first item in your list
-   The second item; *italicize* key words

Improve your image by including an image.

![A Great HTML Resource](http://www.mygifs.com/CoverImage.gif)

Add a link to your favorite [Web site](https://www.dummies.com/). Break
up your page with a horizontal rule or two.

------------------------------------------------------------------------

Finally, link to [another page](page2.html) in your own Web site.

Â© Wiley Publishing, 2011
</code></pre>
<p>Notice that heading 1 is formatted with <code>====</code> rather than the <code>#</code> that RMarkdown seems to favor. We can require pandoc to use the <code>#</code> during the conversion by adding an argument.</p>
<pre><code>pandoc_convert(file_path, to = &quot;markdown_strict&quot;, options = c(&quot;--atx-headers&quot;))</code></pre>
<pre><code># Enter the main heading, usually the same as the title.

Be **bold** in stating your key points. Put them in a list:

-   The first item in your list
-   The second item; *italicize* key words

Improve your image by including an image.

![A Great HTML Resource](http://www.mygifs.com/CoverImage.gif)

Add a link to your favorite [Web site](https://www.dummies.com/). Break
up your page with a horizontal rule or two.

------------------------------------------------------------------------

Finally, link to [another page](page2.html) in your own Web site.

Â© Wiley Publishing, 2011</code></pre>
<p>Right now, the output is being piped to the console. A file can be created instead with:</p>
<pre><code>pandoc_convert(file_path, to = &quot;markdown_strict&quot;, output = &quot;example.md&quot;)</code></pre>
<p>Pandoc has a multitude of styling extensions for markdown variants, all listed on the <a rel="nofollow" href="https://pandoc.org/MANUAL.html" target="_blank">manual page.</a></p>
<p>Pandoc ignores everything enclosed in <code></code>. When converting from markdown to HTML, these comments are usually directly placed as is in the HTML document but the opposite does not seem to be true.</p>
<p></p>
<p>Lastly, this was tested using pandoc version 1.19.2.1. Pandoc 2.5 was released last month.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.yihanwu.ca/post/rstudio-pandoc-html-to-markdown/"> R on YIHAN WU</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/2vcqP5vimQ0" height="1"></a></div>