Skip to content

Commit 3ed8483

Browse files
authored
Hide preview env values to prevent accidental deletion (#1117)
1 parent b6e0a92 commit 3ed8483

2 files changed

Lines changed: 41 additions & 30 deletions

File tree

R/rmarkdown/knit.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ cat(
2929
.vsc.knit_lim,
3030
eval(parse(text = .vsc.knit_command)),
3131
.vsc.knit_lim,
32-
sep = ""
32+
sep = "",
33+
file = stdout()
3334
)

R/rmarkdown/preview.R

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,54 @@ if (!requireNamespace("rmarkdown", quietly = TRUE)) {
44
}
55

66
# get values from extension-set env values
7-
knit_dir <- Sys.getenv("VSCR_KNIT_DIR")
8-
lim <- Sys.getenv("VSCR_LIM")
9-
file_path <- Sys.getenv("VSCR_FILE_PATH")
10-
output_file_loc <- Sys.getenv("VSCR_OUTPUT_FILE")
11-
tmp_dir <- Sys.getenv("VSCR_TMP_DIR")
7+
# Hiding values is necessary to prevent their accidental removal
8+
# See: https://github.com/REditorSupport/vscode-R/issues/860
9+
attach(
10+
local({
11+
.vsc.knit_dir <- Sys.getenv("VSCR_KNIT_DIR")
12+
.vsc.knit_lim <- Sys.getenv("VSCR_LIM")
13+
.vsc.file_path <- Sys.getenv("VSCR_FILE_PATH")
14+
.vsc.output_file_loc <- Sys.getenv("VSCR_OUTPUT_FILE")
15+
.vsc.tmp_dir <- Sys.getenv("VSCR_TMP_DIR")
1216

13-
# if an output format ends up as html, we should not overwrite
14-
# the format with rmarkdown::html_document()
15-
set_html <- tryCatch(
16-
expr = {
17-
lines <- suppressWarnings(readLines(file_path, encoding = "UTF-8"))
18-
out <- rmarkdown:::output_format_from_yaml_front_matter(lines)
19-
output_format <- rmarkdown:::create_output_format(out$name, out$options)
20-
if (!output_format$pandoc$to == "html") {
21-
rmarkdown::html_document()
22-
} else {
23-
NULL
24-
}
25-
}, error = function(e) {
26-
rmarkdown::html_document()
27-
}
17+
# if an output format ends up as html, we should not overwrite
18+
# the format with rmarkdown::html_document()
19+
.vsc.set_html <- tryCatch(
20+
expr = {
21+
lines <- suppressWarnings(readLines(.vsc.file_path, encoding = "UTF-8"))
22+
out <- rmarkdown:::output_format_from_yaml_front_matter(lines)
23+
output_format <- rmarkdown:::create_output_format(out$name, out$options)
24+
if (!output_format$pandoc$to == "html") {
25+
rmarkdown::html_document()
26+
} else {
27+
NULL
28+
}
29+
}, error = function(e) {
30+
rmarkdown::html_document()
31+
}
32+
)
33+
environment()
34+
}),
35+
name = "tools:vscode",
36+
warn.conflicts = FALSE
2837
)
2938

3039
# set the knitr chunk eval directory
3140
# mainly affects source calls
32-
if (nzchar(knit_dir)) {
33-
knitr::opts_knit[["set"]](root.dir = knit_dir)
41+
if (nzchar(.vsc.knit_dir)) {
42+
knitr::opts_knit[["set"]](root.dir = .vsc.knit_dir)
3443
}
3544

3645
# render and get file output location for use in extension
3746
cat(
38-
lim,
47+
.vsc.knit_lim,
3948
rmarkdown::render(
40-
file_path,
41-
output_format = set_html,
42-
output_file = output_file_loc,
43-
intermediates_dir = tmp_dir
49+
.vsc.file_path,
50+
output_format = .vsc.set_html,
51+
output_file = .vsc.output_file_loc,
52+
intermediates_dir = .vsc.tmp_dir
4453
),
45-
lim,
46-
sep = ""
54+
.vsc.knit_lim,
55+
sep = "",
56+
file = stdout()
4757
)

0 commit comments

Comments
 (0)