From 84614f56ffc6e676b488702887edb9a25d170024 Mon Sep 17 00:00:00 2001 From: ElianHugh Date: Fri, 10 Sep 2021 20:15:30 +1000 Subject: [PATCH 1/2] Respect output format --- R/rmarkdown/knit.R | 2 +- R/rmarkdown/preview.R | 22 +++++++++++++++++++--- src/rmarkdown/preview.ts | 3 +++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/R/rmarkdown/knit.R b/R/rmarkdown/knit.R index 895fa27b5..734bd91a7 100644 --- a/R/rmarkdown/knit.R +++ b/R/rmarkdown/knit.R @@ -1,5 +1,5 @@ # requires rmarkdown package to run (and knitr) -if (!requireNamespace(rmarkdown, quietly = TRUE)) { +if (!requireNamespace("rmarkdown", quietly = TRUE)) { stop("Knitting requires the {rmarkdown} package.") } diff --git a/R/rmarkdown/preview.R b/R/rmarkdown/preview.R index ecf5e6cc4..87bdea362 100644 --- a/R/rmarkdown/preview.R +++ b/R/rmarkdown/preview.R @@ -1,16 +1,32 @@ # requires rmarkdown package to run (and knitr) -if (!requireNamespace(rmarkdown, quietly = TRUE)) { +if (!requireNamespace("rmarkdown", quietly = TRUE)) { stop("Previewing documents requires the {rmarkdown} package.") } # get values from extension-set env values - knit_dir <- Sys.getenv("VSCR_KNIT_DIR") lim <- Sys.getenv("VSCR_LIM") file_path <- Sys.getenv("VSCR_FILE_PATH") output_file_loc <- Sys.getenv("VSCR_OUTPUT_FILE") tmp_dir <- Sys.getenv("VSCR_TMP_DIR") +# if an output format ends up as html, we should not overwrite +# the format with rmarkdown::html_document() +set_html <- tryCatch( + expr = { + lines <- suppressWarnings(readLines(file_path, encoding = "UTF-8")) + out <- rmarkdown:::output_format_from_yaml_front_matter(lines) + output_format <- rmarkdown:::create_output_format(out$name, out$options) + if (!output_format$pandoc$to == "html") { + rmarkdown::html_document() + } else { + NULL + } + }, error = function(e) { + rmarkdown::html_document() + } +) + # set the knitr chunk eval directory # mainly affects source calls knitr::opts_knit[["set"]](root.dir = knit_dir) @@ -20,7 +36,7 @@ cat( lim, rmarkdown::render( file_path, - output_format = rmarkdown::html_document(), + output_format = set_html, output_file = output_file_loc, intermediates_dir = tmp_dir ), diff --git a/src/rmarkdown/preview.ts b/src/rmarkdown/preview.ts index ca4d13f77..98b45c5a2 100644 --- a/src/rmarkdown/preview.ts +++ b/src/rmarkdown/preview.ts @@ -113,6 +113,9 @@ class RMarkdownPreview extends vscode.Disposable { pre > code { background: transparent; } + h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 { + color: inherit; + } `; $('head').append(style); From f023a85a0343e909a153a13b0ec6ca2d8cdea9ab Mon Sep 17 00:00:00 2001 From: "Elian H. Thiele-Evans" <60372411+ElianHugh@users.noreply.github.com> Date: Sat, 11 Sep 2021 13:21:51 +1000 Subject: [PATCH 2/2] Fix anchor tags --- src/rmarkdown/preview.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/rmarkdown/preview.ts b/src/rmarkdown/preview.ts index 98b45c5a2..e5a9466f6 100644 --- a/src/rmarkdown/preview.ts +++ b/src/rmarkdown/preview.ts @@ -71,7 +71,7 @@ class RMarkdownPreview extends vscode.Disposable { } private getHtmlContent(htmlContent: string): void { - let content = htmlContent.replace(/<(\w+)\s+(href|src)="(?!\w+:)/g, + let content = htmlContent.replace(/<(\w+)\s+(href|src)="(?!(\w+:)|#)/g, `<$1 $2="${String(this.panel.webview.asWebviewUri(vscode.Uri.file(tmpDir())))}/`); const re = new RegExp('.*', 'ms'); @@ -82,12 +82,11 @@ class RMarkdownPreview extends vscode.Disposable { content = `
${html}
`; } - this.htmlLightContent = content; - const $ = cheerio.load(content); - const chunkCol = String(config().get('rmarkdown.chunkBackgroundColor')); + this.htmlLightContent = $.html(); // make the output chunks a little lighter to stand out + const chunkCol = String(config().get('rmarkdown.chunkBackgroundColor')); const colReg = /[0-9.]+/g; const regOut = chunkCol.match(colReg); const outCol = `rgba(${regOut[0] ?? 100}, ${regOut[1] ?? 100}, ${regOut[2] ?? 100}, ${Number(regOut[3]) + 0.05 ?? .5})`;