Skip to content

Commit 2db085b

Browse files
cdervyihui
authored andcommitted
Fix rstudio#787: cross referencing doesn't work when splitting by sections and an h2 follows immediately an h1 (rstudio#849)
1 parent f447e6c commit 2db085b

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

NEWS.md

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

3+
## BUG FIXES
4+
5+
- Cross-referencing works correctly now with `gitbook` when using `split_by: section` or `split_by: section+number` (thanks, @ThierryO, @cderv, #787)
36

47
# CHANGES IN bookdown VERSION 0.17
58

R/html.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ split_chapters = function(output, build = build_chapter, number_sections, split_
279279
# h2 that immediately follows h1
280280
i = h12[n12 == 'h2' & c('h2', head(n12, -1)) == 'h1'] - 1
281281
# close the h1 section early with </div>
282-
if (length(i)) x[i] = paste(x[i], '\n</div>')
282+
# reg_chap and sec_num must take this into account so that cross reference
283+
# works when split by section. (#849)
284+
if (length(i)) x[i] = paste0(x[i], '\n</div>')
283285
# h1 that immediately follows h2 but not the first h1
284286
i = n12 == 'h1' & c('h1', head(n12, -1)) == 'h2'
285287
if (any(i) && n12[1] == 'h2') i[which(n12 == 'h1')[1]] = FALSE
@@ -575,7 +577,7 @@ prefix_section_labels = function(labels) {
575577
labels
576578
}
577579

578-
reg_chap = '^(<h1><span class="header-section-number">)([A-Z0-9]+)(</span>.+</h1>)$'
580+
reg_chap = '^(<h1><span class="header-section-number">)([A-Z0-9]+)(</span>.+</h1>)(\n</div>)?$'
579581

580582
# default names for labels
581583
label_names = list(fig = 'Figure ', tab = 'Table ', eq = 'Equation ')
@@ -690,7 +692,7 @@ i18n = function(group, key, dict = list()) {
690692
if (is.null(labels[[key]])) dict[[key]] else labels[[key]]
691693
}
692694

693-
sec_num = '^<h[1-6]><span class="header-section-number">([.A-Z0-9]+)</span>.+</h[1-6]>$'
695+
sec_num = '^<h[1-6]><span class="header-section-number">([.A-Z0-9]+)</span>.+</h[1-6]>(\n</div>)?$'
694696

695697
# parse section numbers and labels (id's)
696698
parse_section_labels = function(content) {

tests/rmd/split-section.Rmd

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@ output:
66
split_by: section
77
---
88

9-
# Section 1
9+
# Section 1
1010

1111
Some content
1212

1313
## subsection 1
1414

1515
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ if (Sys.getenv('NOT_CRAN') == 'true') local({
1010
rmarkdown::render(f, envir = globalenv(), quiet = TRUE)
1111
}
1212

13+
# split by section works correctly
14+
## id is used for html file name
15+
sections_files <- c("section-1.html", "subsection-1.html", "section-2.html", "sub2.html")
16+
if (any(!file.exists(file.path("rmd", sections_files))))
17+
stop("Failed to generate sections files")
18+
## reference is working correctly (see #787)
19+
if (!any(xfun::read_utf8("rmd/subsection-1.html") == '<p>See chapter 2 now at <a href="section-2.html#section-2">2</a></p>'))
20+
stop('Failed to reference section when split by sections')
21+
if (!any(xfun::read_utf8("rmd/sub2.html") == '<p>See figure <a href="sub2.html#fig:iris-plot">2.1</a></p>'))
22+
stop('Failed to reference figure when split by sections')
23+
24+
# Check equation label are working
1325
if (!any(readLines('rmd/equation-label.html') == 'y \\in \\mathbb{Z} \\tag{2}')) {
1426
stop('Failed to render the equation label in equation-label.Rmd')
1527
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy