# Parse YAML front matter
text <- "---
title: My Document
date: 2024-01-01
---
Document content here"
result <- parse_front_matter(text)
result$data$title # "My Document"
result$body # "Document content here"
# Parse TOML front matter
text <- "+++
title = 'My Document'
date = 2024-01-01
+++
Document content"
result <- parse_front_matter(text)
# Get raw YAML without parsing
result <- parse_front_matter(text, parse_yaml = identity)
# Use a custom parser that adds metadata
result <- parse_front_matter(
text,
parse_yaml = function(x) {
data <- yaml12::parse_yaml(x)
data$parsed_at <- Sys.time()
data
}
)
# Or read from a file
tmpfile <- tempfile(fileext = ".md")
writeLines(text, tmpfile)
read_front_matter(tmpfile)
Run the code above in your browser using DataLab