Skip to content

Commit 4df10a8

Browse files
committed
drop YAML headers in non-index chapters
1 parent e212cc9 commit 4df10a8

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: litedown
22
Type: Package
33
Title: A Lightweight Version of R Markdown
4-
Version: 0.5.16
4+
Version: 0.5.17
55
Authors@R: c(
66
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666", URL = "https://yihui.org")),
77
person("Tim", "Taylor", role = "ctb", comment = c(ORCID = "0000-0002-8587-7113")),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
- 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).
1212

13+
- `fuse_book()` will ignore YAML headers in book chapters except for the index chapter.
14+
1315
# CHANGES IN litedown VERSION 0.5
1416

1517
- Added a wizard in `roam()` to create new `.Rmd`/`.md`/`.R` files with selected HTML features.

R/preview.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ file_resp = function(x, preview) {
219219
list(payload = switch(
220220
info$type,
221221
book = {
222-
store_book(info$root, x)
222+
store_book(info$root, x, info$index)
223223
fuse_book(if (info$index) info$root else x, full_output, globalenv())
224224
},
225225
site = fuse_site(x),
@@ -240,9 +240,13 @@ file_resp = function(x, preview) {
240240

241241
# store book dir for books to resolve number_refs() because the book may be
242242
# partially rendered (in which case we can't resolve refs to other chapters)
243-
store_book = function(dir, x) {
244-
.env$current_book = dir; .env$current_file = x
245-
exit_call(function() .env$current_book <- .env$current_file <- NULL)
243+
store_book = function(dir, x, index = FALSE) {
244+
f = function(...) .mapply(
245+
function(n, v) .env[[paste0('current_', n)]] = v,
246+
c('book', 'file', 'index'), list(...)
247+
)
248+
f(dir, x, index)
249+
exit_call(function() f(NULL))
246250
}
247251

248252
# detect project type for a directory (_litedown.yml may be in an upper-level dir)

R/site.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,9 @@ fuse_book = function(input = '.', output = NULL, envir = parent.frame()) {
232232
fuse(x, fmt, NULL, envir)
233233
}
234234
}
235-
# remove YAML in the preview mode since we only need the body
236-
if (length(preview)) out = xfun::yaml_body(split_lines(out), parse = FALSE)$body
235+
# remove YAML in the preview mode or for non-index chapters since we only need the body
236+
if (length(preview) || isFALSE(.env$current_index) || x != input[1])
237+
out = sans_yaml(out)
237238

238239
if (format != 'html') return(out)
239240
# add input filenames to the end for HTML output and wrap each file in a div

R/utils.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ sans_p = function(x) gsub('^<p[^>]*>|(</p>)?\n$', '', x)
3131
# remove ugly single quotes, e.g., 'LaTeX' -> LaTeX
3232
sans_sq = function(x) gsub("(^|\\W)'([^']+)'(\\W|$)", '\\1\\2\\3', x)
3333

34+
# remove YAML header
35+
sans_yaml = function(x) {
36+
if (length(x) && grepl('^---\\s*?($|\n)', x[1]))
37+
x = xfun::yaml_body(split_lines(x), parse = FALSE)$body
38+
x
39+
}
40+
3441
is_lang = function(x) is.symbol(x) || is.language(x)
3542

3643
uapply = function(..., recursive = TRUE) unlist(lapply(...), recursive = recursive)

0 commit comments

Comments
 (0)