Skip to content

Commit

Permalink
drop YAML headers in non-index chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Feb 26, 2025
1 parent e212cc9 commit 4df10a8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 8 additions & 4 deletions R/preview.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions R/site.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ sans_p = function(x) gsub('^<p[^>]*>|(</p>)?\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)
Expand Down

0 comments on commit 4df10a8

Please sign in to comment.