Skip to content

Commit 2333f69

Browse files
authored
Respect preview output format (#785)
* Respect output format * Fix anchor tags
1 parent c5ee36f commit 2333f69

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

R/rmarkdown/preview.R

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

66
# get values from extension-set env values
7-
87
knit_dir <- Sys.getenv("VSCR_KNIT_DIR")
98
lim <- Sys.getenv("VSCR_LIM")
109
file_path <- Sys.getenv("VSCR_FILE_PATH")
1110
output_file_loc <- Sys.getenv("VSCR_OUTPUT_FILE")
1211
tmp_dir <- Sys.getenv("VSCR_TMP_DIR")
1312

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+
}
28+
)
29+
1430
# set the knitr chunk eval directory
1531
# mainly affects source calls
1632
knitr::opts_knit[["set"]](root.dir = knit_dir)
@@ -20,7 +36,7 @@ cat(
2036
lim,
2137
rmarkdown::render(
2238
file_path,
23-
output_format = rmarkdown::html_document(),
39+
output_format = set_html,
2440
output_file = output_file_loc,
2541
intermediates_dir = tmp_dir
2642
),

src/rmarkdown/preview.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class RMarkdownPreview extends vscode.Disposable {
7171
}
7272

7373
private getHtmlContent(htmlContent: string): void {
74-
let content = htmlContent.replace(/<(\w+)\s+(href|src)="(?!\w+:)/g,
74+
let content = htmlContent.replace(/<(\w+)\s+(href|src)="(?!(\w+:)|#)/g,
7575
`<$1 $2="${String(this.panel.webview.asWebviewUri(vscode.Uri.file(tmpDir())))}/`);
7676

7777
const re = new RegExp('<html[^\\n]*>.*</html>', 'ms');
@@ -82,12 +82,11 @@ class RMarkdownPreview extends vscode.Disposable {
8282
content = `<html><head></head><body><pre>${html}</pre></body></html>`;
8383
}
8484

85-
this.htmlLightContent = content;
86-
8785
const $ = cheerio.load(content);
88-
const chunkCol = String(config().get('rmarkdown.chunkBackgroundColor'));
86+
this.htmlLightContent = $.html();
8987

9088
// make the output chunks a little lighter to stand out
89+
const chunkCol = String(config().get('rmarkdown.chunkBackgroundColor'));
9190
const colReg = /[0-9.]+/g;
9291
const regOut = chunkCol.match(colReg);
9392
const outCol = `rgba(${regOut[0] ?? 100}, ${regOut[1] ?? 100}, ${regOut[2] ?? 100}, ${Number(regOut[3]) + 0.05 ?? .5})`;
@@ -113,6 +112,9 @@ class RMarkdownPreview extends vscode.Disposable {
113112
pre > code {
114113
background: transparent;
115114
}
115+
h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 {
116+
color: inherit;
117+
}
116118
</style>
117119
`;
118120
$('head').append(style);

0 commit comments

Comments
 (0)