Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect preview output format #785

Merged
merged 2 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/rmarkdown/knit.R
Original file line number Diff line number Diff line change
@@ -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.")
}

Expand Down
22 changes: 19 additions & 3 deletions R/rmarkdown/preview.R
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
),
Expand Down
10 changes: 6 additions & 4 deletions src/rmarkdown/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<html[^\\n]*>.*</html>', 'ms');
Expand All @@ -82,12 +82,11 @@ class RMarkdownPreview extends vscode.Disposable {
content = `<html><head></head><body><pre>${html}</pre></body></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})`;
Expand All @@ -113,6 +112,9 @@ class RMarkdownPreview extends vscode.Disposable {
pre > code {
background: transparent;
}
h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 {
color: inherit;
}
</style>
`;
$('head').append(style);
Expand Down