Skip to content

Commit a07ae07

Browse files
committed
fix rstudio#832: remove attributes on headers <h1>...<h6> since Pandoc 2.8 may add them, e.g., <h1 number="1"> (jgm/pandoc#5944), which will make bookdown fail to parse things like section numbers
also merge the 'class' attribute of <h1> into its parent div's class
1 parent 51e7ff1 commit a07ae07

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
## BUG FIXES
88

9+
- Fixed cross-reference issues with Pandoc 2.8 (thanks, @N0rbert @RLesur, #832).
10+
911
- For output formats like `pdf_book`, unused arguments passed to `base_format` will be discarded (thanks, @jooyoungseo, #790).
1012

1113
- For the sake of backward-compatibility, prevent the commands `\frontmatter`, `\mainmatter`, and `\backmatter` from being automatically added to the LaTeX output when the Pandoc version is higher than 2.7, because **bookdown** users may have added these commands by themselves (thanks, @remlapmot, rstudio/rmarkdown#1721).

R/html.R

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ html_document2 = function(
132132
config$post_processor = function(metadata, input, output, clean, verbose) {
133133
if (is.function(post)) output = post(metadata, input, output, clean, verbose)
134134
x = read_utf8(output)
135+
x = clean_html_tags(x)
135136
x = restore_appendix_html(x, remove = FALSE)
136137
x = restore_part_html(x, remove = FALSE)
137138
x = resolve_refs_html(x, global = !number_sections)
@@ -249,7 +250,7 @@ split_chapters = function(output, build = build_chapter, number_sections, split_
249250
if (!(split_level %in% 0:2)) stop('split_level must be 0, 1, or 2')
250251

251252
x = read_utf8(output)
252-
x = clean_meta_tags(x)
253+
x = clean_html_tags(x)
253254

254255
i1 = find_token(x, '<!--bookdown:title:start-->')
255256
i2 = find_token(x, '<!--bookdown:title:end-->')
@@ -448,6 +449,30 @@ clean_meta_tags = function(x) {
448449
x
449450
}
450451

452+
# remove extra attributes on headers (Pandoc 2.8+), and merge the 'class' of a
453+
# header into its parent div: https://github.com/rstudio/bookdown/issues/832
454+
clean_header_tags = function(x) {
455+
r1 = '^(<div [^>]*class="section level[1-6][^"]*)(">)$'
456+
r2 = '^(<h[1-6])([^>]+)(>.+</h[1-6]>.*)$'
457+
i = grep(r2, x)
458+
i = i[grep(r1, x[i - 1])] # the line above <h1> should be <div>
459+
if (length(i) == 0) return(x)
460+
461+
a = gsub(r2, '\\2', x[i]) # attributes of <h1>
462+
j = grep(r3 <- '(.* class=")([^"]+)(".*)', a)
463+
a = gsub(r3, ' \\2', a[j]) # extract the 'class' attribute of <h1>
464+
k = i[j] - 1 # add the 'class' attribute of <h1> to its parent <div>
465+
x[k] = paste0(gsub(r1, '\\1', x[k]), a, gsub(r1, '\\2', x[k]))
466+
x[i] = gsub(r2, '\\1\\3', x[i]) # remove attributes on <h1>
467+
x
468+
}
469+
470+
clean_html_tags = function(x) {
471+
x = clean_meta_tags(x)
472+
x = clean_header_tags(x)
473+
x
474+
}
475+
451476
# move files to output dir if specified
452477
move_to_output_dir = function(files) {
453478
files2 = output_path(files)

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