Skip to content

Commit 3f79011

Browse files
committed
fix: code-annotation not detected when non-R code blocks are present
Fixes #13216
1 parent 5d4ae28 commit 3f79011

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/resources/rmd/rmd.R

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
knit_meta <- lapply(data, jsonlite::unserializeJSON)
3535

3636
# determine files_dir
37-
files_dir <- if (!is.null(libDir)) libDir else
37+
files_dir <- if (!is.null(libDir)) {
38+
libDir
39+
} else {
3840
rmarkdown:::knitr_files_dir(output)
41+
}
3942

4043
# yield pandoc format
4144
list(
@@ -60,7 +63,9 @@
6063

6164
# bail if we don't have any perserved chunks and aren't doing code linking
6265
code_link <- isHTML && isTRUE(format$render$`code-link`)
63-
if (length(preserved_chunks) == 0 && code_link == FALSE) return()
66+
if (length(preserved_chunks) == 0 && code_link == FALSE) {
67+
return()
68+
}
6469

6570
# change to input dir and make input relative
6671
oldwd <- setwd(dirname(rmarkdown:::abs_path(input)))
@@ -97,7 +102,14 @@
97102
# within a code chunk
98103
for (x in seq_along(chunkStarts)) {
99104
start <- chunkStarts[x]
100-
end <- chunkEnds[x]
105+
# Ensure end is greater than start
106+
end <- start
107+
for (e in chunkEnds) {
108+
if (e > start) {
109+
end <- e
110+
break
111+
}
112+
}
101113
for (y in start:end) {
102114
if (y > start && y < end) {
103115
chunkMap[y] <- TRUE
@@ -107,7 +119,7 @@
107119

108120
# look for at least one annotations that is in a code chunk
109121
for (a in annotations) {
110-
if (chunkMap[a] == TRUE) {
122+
if (chunkMap[a]) {
111123
hasAnnotations <- TRUE
112124
break
113125
}
@@ -191,8 +203,12 @@
191203

192204
run <- function(input, port, host) {
193205
shiny_args <- list()
194-
if (!is.null(port)) shiny_args$port <- port
195-
if (!is.null(host)) shiny_args$host <- host
206+
if (!is.null(port)) {
207+
shiny_args$port <- port
208+
}
209+
if (!is.null(host)) {
210+
shiny_args$host <- host
211+
}
196212

197213
# we already ran quarto render before the call to run
198214
Sys.setenv(RMARKDOWN_RUN_PRERENDER = "0")
@@ -226,7 +242,9 @@
226242
# print execute-debug message ("spin" and "run" don't pass format option)
227243
debug <- (!request$action %in% c("spin", "run")) &&
228244
isTRUE(params$format$execute[["debug"]])
229-
if (debug) message("[knitr engine]: ", request$action)
245+
if (debug) {
246+
message("[knitr engine]: ", request$action)
247+
}
230248

231249
# dispatch request
232250
if (request$action == "spin") {
@@ -267,7 +285,9 @@
267285
}
268286

269287
# write results
270-
if (debug) message("[knitr engine]: writing results")
288+
if (debug) {
289+
message("[knitr engine]: writing results")
290+
}
271291
resultJson <- jsonlite::toJSON(auto_unbox = TRUE, result)
272292
xfun:::write_utf8(paste(resultJson, collapse = "\n"), request[["results"]])
273293
if (debug) message("[knitr engine]: exiting")

0 commit comments

Comments
 (0)