rstudio / blogdown

Create Blogs and Websites with R Markdown
https://pkgs.rstudio.com/blogdown/
1.73k stars 334 forks source link

Watch and rebuild files in `static/` #637

Open cderv opened 3 years ago

cderv commented 3 years ago

For context, this is from a question by @gadenbuie

I’m working on a workshop page with blogdown where I have regular posts in content/... and slides in static/slides/. I have a custom build script to build the slides (since they’re a little complicated). I’d like blogdown to watch for changes in both content/ and static/ but to use my R/build.R script to render the slides rather than trying to render the .Rmd files individually. Is this possible?

Ideally, this would be

Current workaround is

options(
  blogdown.knit.on_save = FALSE,
  blogdown.method = "custom"
)
.serve_site <- function() {
  check_and_rebuild <- function() {
    if (Sys.getenv("BLOGDOWN_SERVING_DIR", "") == "") {
      message("blogdown is no longer serving the site, not watching for changes")
      return()
    }
    changed <- blogdown:::filter_timestamp(blogdown:::list_rmds(c("content", "static")))
    if (length(changed)) {
      blogdown::build_site(build_rmd = 'timestamp')
    }
    later::later(check_and_rebuild, delay = 5)
  }
  blogdown::serve_site()
  check_and_rebuild()
}

This should be possible to have this behavior built in, meaning whether to automatically knit Rmd under static/ on save

gadenbuie commented 3 years ago

FTR (just in case anyone finds my function above), I ended up needing to explicitly call build_rmds() on the changed files in content/.

.serve_site <- function() {
  check_and_rebuild <- function() {
    if (Sys.getenv("BLOGDOWN_SERVING_DIR", "") == "") {
      message("Blogdown is no longer serving the site, not watching for changes")
      return()
    }
    changed <- blogdown:::filter_timestamp(blogdown:::list_rmds(c("content", "static")))
    if (length(changed)) {
      changed_content <- changed[grepl("content/", changed, fixed = TRUE)]
      if (length(changed_content)) {
        blogdown:::build_rmds(changed_content)
      }
      blogdown::build_site(build_rmd = "timestamp")
    }
    later::later(check_and_rebuild, delay = 5)
  }
  blogdown::serve_site()
  check_and_rebuild()
}