diff --git a/DESCRIPTION b/DESCRIPTION index 99b15d66..f8667f26 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: blogdown Title: Create Blogs and Websites with R Markdown -Version: 1.8.1 +Version: 1.8.2 Authors@R: c( person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("Christophe", "Dervieux", role = "aut", email = "cderv@rstudio.com", comment = c(ORCID = "0000-0003-4474-2498")), diff --git a/NEWS.md b/NEWS.md index 018f3bbc..4b6b9677 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # CHANGES IN blogdown VERSION 1.9 +## NEW FEATURES + +- The RStudio addin "Insert Image" also works when the current document in RStudio does not belong to any site projects. Previously, it would throw an error if it is not used inside a site project. Now it works in any document in RStudio (thanks, @yufree, rstudio/rmarkdown#2280). + ## BUG FIXES - `knitr::current_input()` doesn't work in inline R expressions in YAML metadata of Rmd posts (thanks, @brshallo, #647). diff --git a/R/addin.R b/R/addin.R index 6e5e89d0..747fca07 100644 --- a/R/addin.R +++ b/R/addin.R @@ -7,7 +7,15 @@ new_post_addin = function() source_addin('new_post.R') update_meta_addin = function() source_addin('update_meta.R') -insert_image_addin = function() source_addin('insert_image.R') +insert_image_addin = function() { + # when the addin is not used inside a site project + if (xfun::try_error(site_root())) { + old = opts$get() + opts$set(site_root = I('.')) + on.exit(opts$restore(old), add = TRUE) + } + source_addin('insert_image.R') +} # use touch to update the timestamp of a file if available (not on Windows); # otherwise modify a file, undo it, and save it diff --git a/inst/scripts/insert_image.R b/inst/scripts/insert_image.R index e5561ad0..8ce568d9 100644 --- a/inst/scripts/insert_image.R +++ b/inst/scripts/insert_image.R @@ -11,7 +11,10 @@ ctx_ext = tolower(xfun::file_ext(ctx$path)) path = xfun::normalize_path(ctx$path) path = xfun::relative_path(path) -imgdir = if (bundle <- blogdown:::bundle_index(path)) { +# is this addin running inside a site or not? +is_site = !identical(blogdown:::site_root(), I('.')) + +imgdir = if ((bundle <- blogdown:::bundle_index(path)) || !is_site) { file.path(dirname(path), 'images') } else { file.path( @@ -57,7 +60,7 @@ shiny::runGadget( shiny::observeEvent(input$done, { if (is.null(input$newimg)) return(warning2('You have to choose an image!')) target = input$target - if (bundle) { + if (is_site) if (bundle) { if (!startsWith(target, dirname(path))) return(warning2( "The target file path must be under the directory '", dirname(path), "'." )) @@ -87,7 +90,9 @@ shiny::runGadget( }) } image_code = function() { - s = if (bundle) substring(target, nchar(dirname(path)) + 2) else paste0( + s = if (!is_site) { + xfun::relative_path(target, dirname(path), error = FALSE) + } else if (bundle) substring(target, nchar(dirname(path)) + 2) else paste0( ifelse(getOption('blogdown.insertimage.usebaseurl', FALSE), blogdown:::load_config()$baseurl, '/'), gsub('^static/', '', target)