Skip to content

Commit d6bbb36

Browse files
authored
Fix #956: add the config option include_md to search for .md input files (#1483)
1 parent bd78cf8 commit d6bbb36

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- New option in `gitbook`'s font settings menu to control line spacing (thanks, @hayden-MB, #1479).
44

5+
- New configuration setting `include_md` to control whether the input search includes `.md` source files in addition to `.Rmd` (thanks, @katrinabrock #1483, @kylelundstedt #956).
6+
57
# CHANGES IN bookdown VERSION 0.41
68

79
- New `mathjax-config` option for `bs4_book` and `gitbook` to control MathJax config string (thanks, @bwu62, #1472). The option can be set either in the YAML metadata or as a variable in `pandoc_args`. Currently tested and supported settings:

R/utils.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ book_filename = function(config = load_config(), fallback = TRUE) {
8686

8787
source_files = function(format = NULL, config = load_config(), all = FALSE) {
8888
subdir = config[['rmd_subdir']]; subdir_yes = isTRUE(subdir) || is.character(subdir)
89+
ext_regex = if (isTRUE(config[['include_md']])) '[.]R?md$' else '[.]Rmd$'
8990
# a list of Rmd chapters
90-
files = list.files('.', '[.]Rmd$', ignore.case = TRUE)
91+
files = list.files('.', ext_regex, ignore.case = TRUE)
9192
# content in subdir if asked
9293
subdir_files = unlist(mapply(
9394
list.files,
94-
if (is.character(subdir)) subdir else '.', '[.]Rmd$', ignore.case = TRUE,
95+
if (is.character(subdir)) subdir else '.', ext_regex, ignore.case = TRUE,
9596
recursive = subdir_yes, full.names = is.character(subdir), USE.NAMES = FALSE
9697
))
9798
subdir_files = setdiff(subdir_files, files)

inst/examples/01-introduction.Rmd

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ rmd_files:
124124
```
125125
126126
Although we have been talking about R Markdown files, the chapter files do not actually have to be R Markdown. They can be plain Markdown files (`.md`), and do not have to contain R code chunks at all. You can certainly use **bookdown** to compose novels or poems!
127+
However, by default, only `.Rmd` files (but not `.md` files) are included in the automatic collection of files.
127128

128129
At the moment, the major output formats that you may use include `bookdown::pdf_book`, `bookdown::gitbook`, `bookdown::html_book`, and `bookdown::epub_book`. There is a `bookdown::render_book()`\index{bookdown::render\_book()} function similar to `rmarkdown::render()`, but it was designed to render _multiple_ Rmd documents into a book using the output format functions. You may either call this function from command line directly, or click the relevant buttons in the RStudio IDE. Here are some command-line examples:
129130

inst/examples/04-customization.Rmd

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ We have mentioned `rmd_files` in Section \@ref(usage), and there are more (optio
205205
- `history`: similar to `edit`, a link to the edit/commit history of the current page.
206206
- `view`: similar to `edit`, a link to source code of the current page.
207207
- `rmd_subdir`: whether to search for book source Rmd files in subdirectories (by default, only the root directory is searched). This may be either a boolean (e.g. `true` will search for book source Rmd files in the project directory and all subdirectories) or list of paths if you want to search for book source Rmd files in a subset of subdirectories.
208+
- `include_md`: include `.md` files in search for book source (by default only `.Rmd` files are included).
208209
- `output_dir`: the output directory of the book (`_book` by default); this setting is read and used by `render_book()`.
209210
- `clean`: a vector of files and directories to be cleaned by the `clean_book()` function.
210211

tests/testit/test-utils.R

+8-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ assert('prepend_chapter_title() adds the chapter title to the page title', {
5353
})
5454

5555
assert('source_files() handles several configurations correctly', {
56-
get_files = function(files = NULL, dirs = NULL, ...) {
57-
source_files(config = list(rmd_files = files, rmd_subdir = dirs), ...)
56+
get_files = function(files = NULL, dirs = NULL, md = NULL, ...) {
57+
source_files(config = list(rmd_files = files, rmd_subdir = dirs, include_md = md), ...)
5858
}
5959

6060
# create dummy project
@@ -63,7 +63,7 @@ assert('source_files() handles several configurations correctly', {
6363
files = c(
6464
'index.Rmd', '_ignored.Rmd', '01-first.Rmd',
6565
'subdir/other.Rmd', 'subdir/_ignore.Rmd', 'subdir2/last.Rmd',
66-
'abc/def.Rmd', 'abc/ghi.Rmd'
66+
'abc/def.Rmd', 'abc/ghi.Rmd', 'abc/jkl.md'
6767
)
6868
lapply(unique(dirname(files)), dir.create, FALSE, recursive = TRUE)
6969
file.create(files)
@@ -79,7 +79,7 @@ assert('source_files() handles several configurations correctly', {
7979
(get_files(files[4:1]) %==% files[c(1, 4, 3)])
8080

8181
# format allows to filter selected files
82-
(get_files(list(html = 'index.Rmd'), NULL, 'html') %==% files[1])
82+
(get_files(list(html = 'index.Rmd'), NULL, NULL, 'html') %==% files[1])
8383

8484
# rmd_subdir allows subdir contents and root Rmds
8585
(get_files(, TRUE) %==% files[c(1, 3, 7:8, 4, 6)])
@@ -93,6 +93,10 @@ assert('source_files() handles several configurations correctly', {
9393
(get_files(files[3], dirname(files[c(4, 6)])) %==% files[c(3, 4, 6)])
9494
(get_files(files[3], dirname(files[c(4, 6, 7)])) %==% files[c(3, 4, 6, 7:8)])
9595

96+
# include_md toggles inclusion of md files
97+
(get_files(files[3], dirname(files[c(4, 6, 7)]), FALSE) %==% files[c(3, 4, 6, 7:8)])
98+
(get_files(files[3], dirname(files[c(4, 6, 7)]), TRUE) %==% files[c(3, 4, 6, 7:9)])
99+
96100
# clean tests
97101
unlink(project, recursive = TRUE); rm(project)
98102
setwd(old); rm(old)

0 commit comments

Comments
 (0)