Skip to content

Conversation

@cderv
Copy link
Collaborator

@cderv cderv commented Jan 12, 2020

This will fixes #787

To understand what does not work currently, here is a index.Rmd to reproduce

---
title: "My book"
site: bookdown::bookdown_site
output:
  bookdown::gitbook: 
    split_by: "section"
---

# Cross-references {-}

| Target | Label |
| ------ | ----- |
| Chapter 1 | \@ref(chapter-1) |
| Section 1.1 | \@ref(s:1:1) |

# Chapter 1

## Section 1.1 {#s:1:1}

```{r iris-plot, fig.cap="A plot"}
plot(iris)
```

See figure \@ref(fig:iris-plot)

When using split_by: sections , a h2 following a h1 with no content in between will broke the section reference, and will create an incorrect number in figure reference.
You'll get a

Warning message:
The label(s) chapter-1 not found

This is because bookdown processing will add a closing div tag, to support special bookdown feature, when split_by is section or section+number

if (length(i)) x[i] = paste(x[i], '\n</div>')

When the second header follows directly the first header (i.e x[i] would be this h1 header), it will add this closing \n</div> to the end of the h1 header. This will break parse_section_labels() that won't find the id because the regex does not take into account this addition.

bookdown/R/html.R

Lines 700 to 707 in 70f9c07

sec_num = '^<h[1-6]><span class="header-section-number">([.A-Z0-9]+)</span>.+</h[1-6]>$'
# parse section numbers and labels (id's)
parse_section_labels = function(content) {
arry = character()
sec_ids = '^<div id="([^"]+)" class="section .+$'
for (i in grep(sec_num, content)) {
if (!grepl(sec_ids, content[i - 1])) next # no section id

This PR fixes this by detecting the extra div added when split by sections.
Also, I added some tests to check that it will continue to work in the future.

@cderv cderv requested a review from yihui January 12, 2020 17:46
Copy link
Contributor

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you!

@yihui yihui merged commit 2db085b into rstudio:master Jan 13, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 6, 2020
@cderv cderv deleted the cross-ref branch July 26, 2021 08:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unknown cross-reference number with gitbook

2 participants