Skip to content

Commit f93e712

Browse files
atusycderv
andauthored
add number_sections argument to markdown_document2 (closes #756) (#938)
* add number_sections argument to markdown_document2 * add a quick test * update version number Co-authored-by: Christophe Dervieux <cderv@rstudio.com>
1 parent a4c97d8 commit f93e712

File tree

6 files changed

+46
-6
lines changed

6 files changed

+46
-6
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: bookdown
22
Type: Package
33
Title: Authoring Books and Technical Documents with R Markdown
4-
Version: 0.20.5
4+
Version: 0.20.6
55
Authors@R: c(
66
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
77
person("JJ", "Allaire", role = "ctb"),
@@ -61,7 +61,7 @@ License: GPL-3
6161
Imports:
6262
htmltools (>= 0.3.6),
6363
knitr (>= 1.22),
64-
rmarkdown (>= 2.3),
64+
rmarkdown (>= 2.3.5),
6565
xfun (>= 0.13),
6666
tinytex (>= 0.12)
6767
Suggests:
@@ -80,3 +80,4 @@ SystemRequirements: Pandoc (>= 1.17.2)
8080
LazyData: TRUE
8181
RoxygenNote: 7.1.1
8282
Encoding: UTF-8
83+
Remotes: rstudio/rmarkdown

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGES IN bookdown VERSION 0.21
22

3+
## NEW FEATURES
4+
5+
- Add the `number_sections` argument to `markdown_document2()` and its family. This allows to have now figure references numbered by chapters in these formats, like `word_document2()` or `odt_document2()` for example (thanks, @atusy, #756).
6+
37
## BUG FIXES
48

59
- Correctly encode the document title when creating the twitter sharing link from a bookdown chapter (thanks, @maelle, #934).

R/word.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#' @rdname html_document2
22
#' @export
33
markdown_document2 = function(
4-
fig_caption = TRUE, md_extensions = NULL, pandoc_args = NULL, ...,
5-
base_format = rmarkdown::md_document
4+
number_sections = TRUE, fig_caption = TRUE, md_extensions = NULL,
5+
pandoc_args = NULL, ..., base_format = rmarkdown::md_document
66
) {
77
from = rmarkdown::from_rmarkdown(fig_caption, md_extensions)
88

99
config = get_base_format(base_format, list(
10-
fig_caption = fig_caption, md_extensions = md_extensions, pandoc_args = pandoc_args, ...
10+
number_sections = number_sections, fig_caption = fig_caption,
11+
md_extensions = md_extensions, pandoc_args = pandoc_args, ...
1112
))
1213
pre = config$pre_processor
1314
config$pre_processor = function(metadata, input_file, ...) {
1415
# Pandoc does not support numbered sections for Word, so figures/tables have
1516
# to be numbered globally from 1 to n
16-
process_markdown(input_file, from, pandoc_args, TRUE)
17+
process_markdown(input_file, from, pandoc_args, !number_sections)
1718
if (is.function(pre)) pre(metadata, input_file, ...)
1819
}
1920
post = config$post_processor

man/html_document2.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/rmd/number-sections.Rmd

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: "A Book"
3+
author: "Frida Gomam"
4+
output:
5+
bookdown::markdown_document2:
6+
number_sections: true
7+
---
8+
9+
# Section 1
10+
11+
Some content
12+
13+
## subsection 1
14+
15+
Hello.
16+
17+
See chapter 2 now at \@ref(section-2)
18+
19+
# Section 2
20+
21+
## subsection 2 {#sub2}
22+
23+
```{r iris-plot, fig.cap = "A plot"}
24+
plot(iris)
25+
```
26+
27+
See figure \@ref(fig:iris-plot)

tests/test-rmd.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ if (Sys.getenv('NOT_CRAN') == 'true') local({
4646
!any(grepl('id="fn3"', readLines('rmd/subsection-footnotes-1.html')))) {
4747
stop('Failed to move the footnotes back to subsection 1 in parse_footnotes.Rmd')
4848
}
49+
50+
# number sections now works in markdown_document2
51+
if (!any(readLines("rmd/number-sections.md") == "1.1 subsection 1") ||
52+
!any(grepl("<a href=.*>2.1</a>", readLines("rmd/number-sections.md")))) {
53+
stop("Something wrong in number_sections.Rmd")
54+
}
4955
})

0 commit comments

Comments
 (0)