add_title_comments: Add part and chapter title information as comments to...

View source: R/add_title_comments.R

add_title_commentsR Documentation

Add part and chapter title information as comments to _bookdown.yml or master documents

Description

It can be frustrating trying to remember which chapter is which in a bookdown project with a lot of chapters. This function helps identify the content of the chapter by adding part and title information in the form of comments to the _bookdown.yml or master file.

Usage

add_title_comments(file = "_bookdown.yml", master = FALSE)

Arguments

file

defaults to _bookdown.yml

master

indicates that the file is a master document with children, defaults to FALSE

Details

add_title_comments() searches through the specified file line by line looking for lines with Rmd files. When it finds one, it reads the first line of the actual Rmd file and then adds the following to a copy of the file in memory:

If the first line starts with "#" and contains "(PART)", the line is added as a comment in its own line before the Rmd line.

If the first line starts with "#" but doesn't contain "(PART)", the line is added as a comment after the Rmd file name.

If the first line does not start with "#" but the Rmd filename is Index (or index), it reads the full file and looks for the first line beginning with "#" to add as above. (Note that if comments appear in the 'Index.Rmd“ before the title line, they will be misinterpreted as titles.)

For master files, the child chapter title is added as a comment inside the code chunk, and echo=FALSE is added to the chunk options.

WARNING: all existing comments are erased so proceed with caution. As a safety measure, the function does not overwrite the specified file, but rather returns an object which can be written to file with writeLines(). See examples.

Real example: https://github.com/jtr13/1201/blob/master/_bookdown.yml

Examples

## Not run: 
writeLines(add_title_comments(), "test.yml")
writeLines(add_title_comments(), "_bookdown.yml") # be careful!
writeLines(add_title_comments("master_workshops.Rmd", master = TRUE), "test.yml")

## End(Not run)


jtr13/jtr documentation built on June 11, 2025, 12:42 a.m.