Skip to content

Commit 3d46adc

Browse files
committed
Hack around knitr 1.49 behavior
With knitr 1.49, using sew.knit_asis correctly pass modified options$results = "asis" to output hooks for consideration. However, quarto checks for options$results == "asis" in more places and knitr does not modified the cell option outside of output hook context. So we need quarto to save the step from output hook context to be reused through the rest of chunk hook processing. This change correctly make asis_output() results don't have unnecessary cell div
1 parent a712d64 commit 3d46adc

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/resources/rmd/hooks.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ knitr_hooks <- function(format, resourceDir, handledLanguages) {
165165
}
166166
delegating_output_hook = function(type, classes) {
167167
delegating_hook(type, function(x, options) {
168+
### START Knitr hack:
169+
# since knitr 1.49, we can detect if output: asis
170+
# was set by an R function itself (not cell option)
171+
# We save the information for our other processing
172+
# after output hook (i.e after sew method is called)
173+
if (identical(options[["results"]], "asis")) {
174+
.assignToQuartoToolsEnv("cell_options", list(asis_output = TRUE)) # nolint: object_usage_linter, line_length_linter.
175+
}
176+
### END
177+
168178
if (identical(options[["results"]], "asis") ||
169179
isTRUE(options[["collapse"]])) {
170180
x
@@ -182,6 +192,16 @@ knitr_hooks <- function(format, resourceDir, handledLanguages) {
182192
# entire chunk
183193
knit_hooks$chunk <- delegating_hook("chunk", function(x, options) {
184194

195+
## START knitr hack:
196+
## catch knit_asis output from save output hook state
197+
asis_output <- .getFromQuartoToolsEnv("cell_options")$asis_output # nolint: object_usage_linter, line_length_linter.
198+
if (isTRUE(asis_output)) {
199+
options[["results"]] <- "asis"
200+
}
201+
# chunk hook is called last and we can clean the cell storage
202+
on.exit(.rmFromQuartoToolsEnv("cell_options"), add = TRUE) # nolint: object_usage_linter, line_length_linter.
203+
## END
204+
185205
# Do nothing more for some specific chunk content -----
186206

187207
# Quarto language handler

src/resources/rmd/patch.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ if (utils::packageVersion("knitr") >= "1.32.8") {
152152
} else {
153153
knitr:::sew.knit_asis(x, options, ...)
154154
}
155+
## START knitr hack:
156+
## catch knit_asis output from save output hook state
157+
asis_output <- .getFromQuartoToolsEnv("cell_options")$asis_output # nolint: object_usage_linter, line_length_linter
158+
if (isTRUE(asis_output)) {
159+
options[["results"]] <- "asis"
160+
}
161+
## END
155162

156163
# if it's an html widget then it was already wrapped
157164
# by add_html_caption

0 commit comments

Comments
 (0)