Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a9400de

Browse files
authored
Pandoc 3.2.1 will now URL encode the link id for HTML 4 (#1505)
* Pandoc 3.2.1 will now URL encode the link id for HTML 4 gitbook TOC processing needs to account for that. * Adding test to check the TOC does get processed correctly by gitbook() logic
1 parent 52c8724 commit a9400de

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

NEWS.md

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

3+
- `gitbook()` TOC now works again with Pandoc 3.2.1 and above (thanks, @privefl, #1503).
4+
35

46
# CHANGES IN bookdown VERSION 0.44
57

R/html.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ restore_links = function(segment, full, lines, filenames) {
965965
if (length(x) == 0) return(x)
966966
links = gsub(r, '\\1', x)
967967
for (i in seq_along(links)) {
968-
a = grep(sprintf(' id="%s"', links[i]), full, fixed = TRUE)
968+
a = grep(sprintf(' id="%s"', URLdecode(links[i])), full, fixed = TRUE)
969969
if (length(a) == 0) next
970970
a = a[1]
971971
x[i] = sprintf(

tests/testthat/test-gitbook.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ test_that("gitbook_toc correctly process pandoc html with anchor section", {
3838
expect_true(all(xml2::xml_find_lgl(TOC, "not(boolean(./a/span[count(node()) = 0]))")))
3939
})
4040

41+
test_that("gitbook_toc correctly process pandoc html with non-ascii character", {
42+
skip_on_cran()
43+
skip_if_not_pandoc()
44+
skip_if_not_installed("xml2")
45+
book <- local_book()
46+
# add a file with non-ascii
47+
xfun::in_dir(book, xfun::write_utf8(c("# Entraîneur", "", "some content", "", "## été", "", "Content"), "03-entraineur.Rmd"))
48+
res <- .render_book_quiet(book, output_format = gitbook())
49+
content <- xml2::read_html(res)
50+
TOC <- xml2::xml_find_all(content, "//div[@class='book-summary']/nav/ul/li")
51+
expect_equal(xml2::xml_attr(TOC, "class"), rep("chapter", 4))
52+
expect_equal(xml2::xml_attr(TOC, "data-path"), c("index.html", "introduction.html", "references.html", "entraîneur.html"))
53+
H1 <- xml2::xml_find_all(TOC, "a")
54+
expect_equal(xml2::xml_text(H1), c("Preface", "1 Introduction", "# References", "2 Entraîneur"))
55+
expect_equal(xml2::xml_attr(H1, "href"), c("index.html", "introduction.html", "references.html", "entraîneur.html"))
56+
H2 <- xml2::xml_find_all(TOC, ".//li/a")
57+
if (rmarkdown::pandoc_available("3.2.1")) {
58+
expect_equal(xml2::xml_attr(H2, "href"), "entraîneur.html#%C3%A9t%C3%A9")
59+
} else {
60+
expect_equal(xml2::xml_attr(H2, "href"), "entraîneur.html#été")
61+
}
62+
expect_equal(xml2::xml_text(H2), "2.1 été")
63+
})
64+
4165
# https://github.com/rstudio/bookdown/pull/1408
4266
# https://github.com/rstudio/bookdown/issues/1101
4367
test_that("gitbook() correctly handles extra_dependency after its own", {

0 commit comments

Comments
 (0)