Skip to content

Commit

Permalink
close rstudio/rmarkdown#2280: make the RStudio addin "Insert Image" w…
Browse files Browse the repository at this point in the history
…ork in any document (not necessarily inside a blogdown site project)

also close rstudio/rmarkdown#2281
  • Loading branch information
yihui committed Mar 8, 2022
1 parent 5eb849a commit d25c8de
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Christophe", "Dervieux", role = "aut", email = "[email protected]", comment = c(ORCID = "0000-0003-4474-2498")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
10 changes: 9 additions & 1 deletion R/addin.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions inst/scripts/insert_image.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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), "'."
))
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d25c8de

Please sign in to comment.