From 4df10a81d9c3294e496ea1cfbcf1871533497630 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Wed, 26 Feb 2025 11:48:36 -0600 Subject: [PATCH] drop YAML headers in non-index chapters --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/preview.R | 12 ++++++++---- R/site.R | 5 +++-- R/utils.R | 7 +++++++ 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3dddbf2..c6f1e34 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: litedown Type: Package Title: A Lightweight Version of R Markdown -Version: 0.5.16 +Version: 0.5.17 Authors@R: c( person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666", URL = "https://yihui.org")), person("Tim", "Taylor", role = "ctb", comment = c(ORCID = "0000-0002-8587-7113")), diff --git a/NEWS.md b/NEWS.md index 9670b01..ab11c98 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,8 @@ - Fixed a bug that `fuse()` fails to print the error location when the whole input document consists of a single chunk that throws an error (thanks, @kevinushey, yihui/knitr#2387). +- `fuse_book()` will ignore YAML headers in book chapters except for the index chapter. + # CHANGES IN litedown VERSION 0.5 - Added a wizard in `roam()` to create new `.Rmd`/`.md`/`.R` files with selected HTML features. diff --git a/R/preview.R b/R/preview.R index ba6b591..8ba6060 100644 --- a/R/preview.R +++ b/R/preview.R @@ -219,7 +219,7 @@ file_resp = function(x, preview) { list(payload = switch( info$type, book = { - store_book(info$root, x) + store_book(info$root, x, info$index) fuse_book(if (info$index) info$root else x, full_output, globalenv()) }, site = fuse_site(x), @@ -240,9 +240,13 @@ file_resp = function(x, preview) { # store book dir for books to resolve number_refs() because the book may be # partially rendered (in which case we can't resolve refs to other chapters) -store_book = function(dir, x) { - .env$current_book = dir; .env$current_file = x - exit_call(function() .env$current_book <- .env$current_file <- NULL) +store_book = function(dir, x, index = FALSE) { + f = function(...) .mapply( + function(n, v) .env[[paste0('current_', n)]] = v, + c('book', 'file', 'index'), list(...) + ) + f(dir, x, index) + exit_call(function() f(NULL)) } # detect project type for a directory (_litedown.yml may be in an upper-level dir) diff --git a/R/site.R b/R/site.R index 9818742..bd6827f 100644 --- a/R/site.R +++ b/R/site.R @@ -232,8 +232,9 @@ fuse_book = function(input = '.', output = NULL, envir = parent.frame()) { fuse(x, fmt, NULL, envir) } } - # remove YAML in the preview mode since we only need the body - if (length(preview)) out = xfun::yaml_body(split_lines(out), parse = FALSE)$body + # remove YAML in the preview mode or for non-index chapters since we only need the body + if (length(preview) || isFALSE(.env$current_index) || x != input[1]) + out = sans_yaml(out) if (format != 'html') return(out) # add input filenames to the end for HTML output and wrap each file in a div diff --git a/R/utils.R b/R/utils.R index 991227d..993aecd 100644 --- a/R/utils.R +++ b/R/utils.R @@ -31,6 +31,13 @@ sans_p = function(x) gsub('^]*>|(

)?\n$', '', x) # remove ugly single quotes, e.g., 'LaTeX' -> LaTeX sans_sq = function(x) gsub("(^|\\W)'([^']+)'(\\W|$)", '\\1\\2\\3', x) +# remove YAML header +sans_yaml = function(x) { + if (length(x) && grepl('^---\\s*?($|\n)', x[1])) + x = xfun::yaml_body(split_lines(x), parse = FALSE)$body + x +} + is_lang = function(x) is.symbol(x) || is.language(x) uapply = function(..., recursive = TRUE) unlist(lapply(...), recursive = recursive)