Skip to content

Commit

Permalink
don't include the yaml component in the returned value of yaml_body…
Browse files Browse the repository at this point in the history
…() if YAML metadata is not found in the input
  • Loading branch information
yihui committed Apr 19, 2024
1 parent 55575cd commit 9e2a061
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: xfun
Type: Package
Title: Supporting Functions for Packages Maintained by 'Yihui Xie'
Version: 0.43.4
Version: 0.43.5
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Wush", "Wu", role = "ctb"),
Expand Down
8 changes: 4 additions & 4 deletions R/yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,19 @@ yaml_handlers = function(h, envir) {
#' @param x A character vector of the document content.
#' @param ... Arguments to be passed to `yaml_load()`.
#' @export
#' @return A list of components `yaml` and `body`.
#' @return A list of components `yaml` (if YAML metadata exists) and `body`.
#' @examples
#' xfun::yaml_body(c('---', 'title: Hello', 'output: markdown::html_document', '---', '', 'Content.'))
yaml_body = function(x, ...) {
i = grep('^---\\s*$', x)
n = length(x)
res = if (n < 2 || length(i) < 2 || (i[1] > 1 && !all(is_blank(x[seq(i[1] - 1)])))) {
list(yaml = list(), body = x)
list(body = x)
} else list(
yaml = x[i[1]:i[2]], body = c(rep('', i[2]), tail(x, n - i[2]))
)
if ((n <- length(res$yaml)) >= 3) {
res$yaml = yaml_load(res$yaml[-c(1, n)], ...)
if ((n <- length(res$yaml)) >= 2) {
res['yaml'] = list(yaml_load(res$yaml[-c(1, n)], ...))
}
res
}

0 comments on commit 9e2a061

Please sign in to comment.