Skip to contents

Saves an HTML file, URL, Word document, or other pandoc-supported input to a Markdown file. Conversion is performed using Pandoc.

Usage

save_to_md(
  input,
  output,
  md_format = md_format_default,
  wrap = Inf,
  args_to_deflist = FALSE
)

Arguments

input

(Scalar character)
Path to a local file or a URL (http:// or https://). Common inputs include .html files, .docx files, and URLs. Any format supported by Pandoc is accepted.

output

(Scalar character)
File path where the result will be written. The extension must be .md.

md_format

(Scalar character: md_format_default)
Pandoc output format string used for Markdown conversion. See Pandoc manual for available formats and extensions.

wrap

(Scalar integer: Inf; [1, Inf])
Column width for word wrapping in Markdown output. Inf disables wrapping.

args_to_deflist

(Scalar logical: FALSE)
If TRUE, converts argument tables in the HTML to definition lists before Markdown conversion. Only applies when input is an HTML file or URL.

Value

The value of output, invisibly.

Details

Pandoc must be installed and on the system PATH. The default markdown format is:

md_format_default <- paste0(
  "markdown",
  "-raw_html",
  "-inline_code_attributes",
  "-header_attributes",
  "-link_attributes",
  "-native_divs",
  "-native_spans",
  "-auto_identifiers",
  "-fenced_divs",
  "-bracketed_spans",
  "-smart"
)

Examples

#----------------------------------------------------------------------------
# save_to_md() example
#----------------------------------------------------------------------------
library(bkmisc)

# Save a local HTML help page to markdown
html_in <- tempfile(fileext = ".html")
tools::Rd2HTML(
  tools::Rd_db("base")[["mean.Rd"]],
  out = html_in,
  package = "base"
)
md_out <- tempfile(fileext = ".md")
save_to_md(html_in, md_out)
readLines(md_out) |> head(20)
#>  [1] "  ------------- -----------------"                  
#>  [2] "  mean {base}     R Documentation"                  
#>  [3] "  ------------- -----------------"                  
#>  [4] ""                                                   
#>  [5] "## Arithmetic Mean"                                 
#>  [6] ""                                                   
#>  [7] "### Description"                                    
#>  [8] ""                                                   
#>  [9] "Generic function for the (trimmed) arithmetic mean."
#> [10] ""                                                   
#> [11] "### Usage"                                          
#> [12] ""                                                   
#> [13] "``` R"                                              
#> [14] "mean(x, ...)"                                       
#> [15] ""                                                   
#> [16] "## Default S3 method:"                              
#> [17] "mean(x, trim = 0, na.rm = FALSE, ...)"              
#> [18] "```"                                                
#> [19] ""                                                   
#> [20] "### Arguments"