Skip to content

Commit

Permalink
fix #544: use the chapter/section title as the page title (and append…
Browse files Browse the repository at this point in the history
… the book title after it), instead of using the same title for all pages
  • Loading branch information
yihui committed Dec 3, 2018
1 parent d3d76b1 commit 3d837a6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: bookdown
Type: Package
Title: Authoring Books and Technical Documents with R Markdown
Version: 0.7.26
Version: 0.7.27
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("JJ", "Allaire", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

- Added a `quiet` argument to `serve_book()`, so that users can suppress stdout with `bookdown::serve_book(quiet = TRUE)` (thanks, @hammer, #633).

- For HTML output, the title of the current chapter or section will be added to the page title (in the `<title>` tag). This will give readers more information when reading the results from search engines or Twitter cards. Previously, all pages would have identical titles (thanks, @benwhalley and @batpigandme, #544).

## BUG FIXES

- HTML output formats such as `gitbook` and `html_document2` won't work when only unnumbered parts (i.e., `# (PART\*)`) are used (thanks, @tjmahr, #575).
Expand Down
22 changes: 21 additions & 1 deletion R/html.R
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ split_chapters = function(output, build = build_chapter, number_sections, split_
html = relocate_footnotes(html, fnts, a_targets)
html = restore_links(html, html_body, idx, nms)
html = build(
html_head, html_toc, html,
prepend_chapter_title(html_head, html), html_toc, html,
if (i > 1) nms[i - 1],
if (i < n) nms[i + 1],
if (length(nms_chaps)) nms_chaps[i],
Expand Down Expand Up @@ -1004,3 +1004,23 @@ clean_pandoc2_highlight_tags = function(x) {
x = gsub('<a class="sourceLine"[^>]+>(.*)</a>', '\\1', x)
x
}

# extract a chapter title from the body, and prepend it to the page <title>
prepend_chapter_title = function(head, body) {
r1 = '(.*?<title>)(.+?)(</title>.*)'
if (length(i <- grep(r1, head)) == 0) return(head)
body = paste(body, collapse = ' ')
r2 = '.*?<h[0-6][^>]*>(.+?)</h[0-6]>.*'
if (!grepl(r2, body)) return(head)
title = strip_html(gsub(r2, '\\1', body))
x1 = gsub(r1, '\\1', head[i])
x2 = gsub(r1, '\\2', head[i])
x3 = gsub(r1, '\\3', head[i])
if (title == x2) return(head)
head[i] = paste0(x1, title, ' | ', x2, x3)
# update possible OpenGraph tags <meta property="og:title" content="...">
gsub(
paste0(' content="', x2, '"'), paste0(' content="', title, ' | ', x2),
head, fixed = TRUE
)
}

0 comments on commit 3d837a6

Please sign in to comment.