diff --git a/DESCRIPTION b/DESCRIPTION index 0c78878..e80e14a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("Wush", "Wu", role = "ctb"), diff --git a/R/yaml.R b/R/yaml.R index 6ec32fc..413d025 100644 --- a/R/yaml.R +++ b/R/yaml.R @@ -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 }