Skip to content

Adapt regex detection to Pandoc 3 figures to still get numbering #1496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# CHANGES IN bookdown VERSION 0.44

- Figure numbering is now correctly working in HTML with Pandoc 3, when Markdown syntax is used to include images (thanks, @N0rbert, #1467).


# CHANGES IN bookdown VERSION 0.43

Expand Down
4 changes: 2 additions & 2 deletions R/html.R
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ parse_fig_labels = function(content, global = FALSE) {
m = gregexpr(sprintf('\\(#((%s):[-/[:alnum:]]+)\\)', reg_label_types), content)
labs = regmatches(content, m)
cntr = new_counters(label_types, chaps) # chapter counters
figs = grep('^<div class="figure', content)
figs = grep('^<div class="(figure|float)', content)
eqns = grep('<span class="math display">', content)

for (i in seq_along(labs)) {
Expand All @@ -764,7 +764,7 @@ parse_fig_labels = function(content, global = FALSE) {
arry = c(arry, setNames(num, lab))

switch(type, fig = {
if (length(grep('^<p class="caption', content[i - 0:1])) == 0) {
if (length(grep('^<p class="caption|^<div class="figcaption', content[i - 0:1])) == 0) {
# remove these labels, because there must be a caption on this or
# previous line (possible negative case: the label appears in the alt
# text of <img>)
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/resources/figures.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: Figure cross-reference issue in HTML
output:
bookdown::html_document2: default
---

# Zero 1 {-#zero1}

Some text in 1st unnumbered section.

# Practically long header about some theories {#theory}

## Markdown Syntax {#markdown-syntax}

Content

### Images {#markdown-syntax-media}

First figure of first section is below, it reference is Fig. \@ref(fig:md-logo).

![(\#fig:md-logo) Markdown logo](md.png)

...
Binary file added tests/testthat/resources/md.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions tests/testthat/test-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,27 @@ test_that("add_toc_class() works for all pandoc versions", {
TOC <- xml2::xml_find_all(content, "//div[@id = 'TOC']/ul/li")
expect_equal(xml2::xml_attr(TOC, "class"), c("has-sub", NA, "has-sub"))
})


test_that("Figure are numbered correctly", {
skip_on_cran()
skip_if_not_pandoc()
skip_if_not_installed("xml2")

dir <- withr::local_tempdir("bookdown-test")
rmd_file <- "figures.Rmd"
file.copy(test_path("resources", rmd_file), dir)
file.copy(test_path("resources", "md.png"), dir)
withr::local_dir(dir)
out <- rmarkdown::render(rmd_file, output_format = "bookdown::html_document2")

content <- xml2::read_html(out)
xpath <- if (rmarkdown::pandoc_version() >= "3") {
"//div[@class='figcaption']"
} else {
"//p[@class='caption']"
}
figure <- xml2::xml_find_first(content, xpath)
expect_match(xml2::xml_text(figure), "Figure 1.1", fixed = TRUE)

})