diff --git a/NEWS.md b/NEWS.md index 8035002a8..7e7c347c2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -20,6 +20,8 @@ - Added the same CSS as in default Pandoc's template for when a CSL is used (#1045). +- No more warnings are thrown when passing several input files to `render_book(preview = TRUE)` (#1091). + ## MINOR CHANGES - `anchor_sections = TRUE` becomes the default for `bookdown::gitbook()`. diff --git a/R/render.R b/R/render.R index c0f7c326e..afc7d1f79 100644 --- a/R/render.R +++ b/R/render.R @@ -113,7 +113,7 @@ render_book = function( # store output directory and the initial input Rmd name opts$set( output_dir = output_dir, - input_rmd = xfun::relative_path(input, dir = "."), + input_rmd = relative_path(input), preview = preview ) diff --git a/R/utils.R b/R/utils.R index 485acae8f..266e34f09 100644 --- a/R/utils.R +++ b/R/utils.R @@ -284,6 +284,12 @@ dir_create = function(path) { dir_exists(path) || dir.create(path, recursive = TRUE) } +# vectorized version to get relative path of multiple input +relative_path = function(inputs, dir = ".") { + f = Vectorize(xfun::relative_path, "x", USE.NAMES = FALSE) + f(x = inputs, dir = dir, use.. = TRUE, error = TRUE) +} + # a wrapper of file.path to ignore `output_dir` if it is NULL output_path = function(...) { dir = opts$get('output_dir') diff --git a/tests/testit/test-utils.R b/tests/testit/test-utils.R index 78b71b7a1..e8e32668a 100644 --- a/tests/testit/test-utils.R +++ b/tests/testit/test-utils.R @@ -165,3 +165,8 @@ assert('fence_theorems() converts the knitr engine syntax to fenced Divs', { res = fence_theorems(text = old) (unclass(res) %==% old) }) + +assert("relative_path is vectorized", { + relative_path(c('foo/bar.txt', 'foo/baz.txt'), 'foo/') %==% c("bar.txt", "baz.txt") + relative_path('foo/bar.txt', 'foo') %==% "bar.txt" +})