From 0cde6d6b43d94455a5f9b4470a738b4d22696cd3 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Mon, 12 Apr 2021 16:43:42 -0500 Subject: [PATCH] fix #610: do not re-render Rmd files in the public/ directory while serving and watching the site check_site() also detects problematic .html files generated from the rmarkdown::html_document format in the public/ directory and offers advice on fixes also close #608 --- NEWS.md | 2 ++ R/check.R | 26 +++++++++++++++++++++----- R/serve.R | 7 ++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/NEWS.md b/NEWS.md index b2067344..df9dcb61 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,6 +22,8 @@ - The RStudio addin "New Post" does not work when the YAML metadata of a post only contains a string. Normally the metadata should be a list (thanks, msmielak, https://stackoverflow.com/q/66857582/559676). +- When `.Rmd` is not ignored in the `ignoreFiles` field in the site config file (this can be detected by `blogdown::check_site()` and you should fix it), `.Rmd` files may be copied to the `public/` directory when building the site via `blogdown::build_site()`, which can cause problems when `blogdown::serve_site()` is running, i.e., `.Rmd` files will be rendered to the `rmarkdown::html_document()` format by `rmarkdown::render()`. As a result, the corresponding web pages will not be rendered by Hugo but only Pandoc, and they will lose the site style or shortcodes (thanks, @ogansser, #610 #608). Now `blogdown::check_site()` should detect this problem and recommend fixes. + # CHANGES IN blogdown VERSION 1.2 ## NEW FEATURES diff --git a/R/check.R b/R/check.R index 017d4fdc..fc7efe13 100644 --- a/R/check.R +++ b/R/check.R @@ -483,18 +483,34 @@ clean_duplicates = function(preview = TRUE) in_root({ }) check_garbage_html = function() { - res = unlist(lapply(list_files(content_file(), '[.]html$'), function(f) { + res = unlist(lapply(list_files('.', '[.]html$'), function(f) { if (file.size(f) < 200000) return() x = readLines(f, n = 15) if (any(x == '')) return(f) })) - if (n <- length(res)) { + res = sub('^[.]/', '', res) + ok = TRUE + i = xfun::is_sub_path(res, content_file()) + if (n <- sum(i)) { msg_todo( 'Found ', n, ' incompatible .html file', if (n > 1) 's', - ' introduced by previous blogdown versions:\n\n', action_list(res), '\n\n', + ' in the content directory:\n\n', action_list(res[i]), '\n\n', ' To fix, run the above command and then blogdown::build_site(build_rmd = "newfile").' ) - } else { - msg_okay('Found 0 incompatible .html files to clean up.') + ok = FALSE + } + i = xfun::is_sub_path(res, p <- publish_dir()) + if (n <- sum(i) && any(i1 <- file_exists(f1 <- with_ext(res[i], 'Rmd')))) { + i2 = file_exists(f2 <- content_file(substr(f1[i1], nchar(p) + 1, nchar(f1)))) + if (n <- sum(i2)) { + msg_todo( + 'Found ', n, ' incompatible .html file', if (n > 1) 's', + ' in the publish directory:\n\n', action_list(c(res[i][i1][i2], f1[i1])), '\n\n', + ' To fix, restart R, run the above command, fix other issues identified ', + 'by blogdown::check_site(), and then run blogdown::build_site().' + ) + ok = FALSE + } } + if (ok) msg_okay('Found 0 incompatible .html files to clean up.') } diff --git a/R/serve.R b/R/serve.R index e9ca4249..034244a6 100644 --- a/R/serve.R +++ b/R/serve.R @@ -211,7 +211,12 @@ serve_it = function(pdir = publish_dir(), baseurl = site_base_dir()) { rebuild(rmd_files <- filter_newfile(list_rmds())) watch = servr:::watch_dir('.', rmd_pattern, handler = function(files) { - rmd_files <<- list_rmds(files = files) + files = list_rmds(files = files) + # ignore Rmd files in the public/ directory, in case users forgot to set + # ignoreFiles in config.yaml and Rmd files would be copied to public/ + # (they should not be): https://github.com/rstudio/blogdown/issues/610 + i = if (g == 'hugo') !xfun::is_sub_path(files, rel_path(publish_dir())) else TRUE + rmd_files <<- files[i] }) watch_build = function() { # stop watching if stop_server() has cleared served_dirs