Skip to content

Commit

Permalink
fix #610: do not re-render Rmd files in the public/ directory while s…
Browse files Browse the repository at this point in the history
…erving 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
  • Loading branch information
yihui committed Apr 12, 2021
1 parent 4da784b commit 0cde6d6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 21 additions & 5 deletions R/check.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 == '<meta name="generator" content="pandoc" />')) 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.')
}
7 changes: 6 additions & 1 deletion R/serve.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0cde6d6

Please # to comment.