From f798b78450063cdf9424653125e26fc6b05c066c Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 18 May 2021 16:41:17 +0200 Subject: [PATCH] Insure fontawesome V4 compatibility in navbar (#1994) This allows to have old navbar definition to work with new FA5 also. It was a breaking change previously to this. Co-authored-by: Yihui Xie --- NEWS.md | 2 ++ R/html_dependencies.R | 4 ++-- R/html_document.R | 13 ++++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index ae08454f78..2b2e0f9e31 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,8 @@ rmarkdown 2.9 - Floating ToC in `html_document` can now hide headings with unnumbered and unlisted classes (thanks, @atusy, #1993). +- Fix prefix handling in R Markdown website's navbar for Fontawesome V5 and compatibility with V4. For icon only available in V5, the full prefix + name should be use, especially with new `fab` prefix (e.g. `fab fa-r-project`). If no prefix is used (e.g `fa-home` instead of `fas fa-home`), the `fa` prefix will be added for V4 compatibility as it has been deprecated in V5. We advice to use the full prefix + name for icons following Fontawesome documentation. (#1994) + - `rmarkdown::site_generator()` can hang session waiting for input when the `site` field is not found in the YAML frontmatter of `index.Rmd` (thanks, @kevinushey @mirh, #2043). diff --git a/R/html_dependencies.R b/R/html_dependencies.R index 99280d6463..5a055d1569 100644 --- a/R/html_dependencies.R +++ b/R/html_dependencies.R @@ -222,7 +222,7 @@ navbar_icon_dependencies <- function(navbar) { source <- read_utf8(navbar) # find icon references - res <- regexec('<(span|i) +class *= *("|\') *(fa\\w fa|ion ion)-', source) + res <- regexec('<(span|i) +class *= *("|\') *(fa\\w? fa|ion ion)-', source) matches <- regmatches(source, res) libs <- c() for (match in matches) { @@ -232,7 +232,7 @@ navbar_icon_dependencies <- function(navbar) { libs <- unique(libs) # return their dependencies - any_fa <- any(grepl("fa\\w fa", libs)) + any_fa <- any(grepl("fa\\w? fa", libs)) any_ion <- any(grepl("ion ion", libs)) html_dependencies_fonts(any_fa, any_ion) } diff --git a/R/html_document.R b/R/html_document.R index 6d02f39646..dd10edd20b 100644 --- a/R/html_document.R +++ b/R/html_document.R @@ -652,15 +652,18 @@ navbar_link_text <- function(x, ...) { iconset <- split[[1]][[1]] else iconset <- "" - # check if a full class is passed for fontawesome - # use default 'fas' otherwise + # check if a full class is passed for fontawesome = V5 + # Add fa deprecated fa prefix otherwise = V4 compatibility # https://github.com/rstudio/rmarkdown/issues/1554 - class = if (grepl("^fa\\w fa", iconset)) { + class = if (grepl("^fa\\w? fa", iconset)) { + # Fontawesome 5 - full new prefix + name must be passed + # if old fa prefix is passed - keep it for compatibility x$icon } else if (iconset == "fa") { - paste("fas", x$icon) + # Fontawesome 4 compatibility - Add deprecated fa prefix + paste("fa", x$icon) } else { - # should be other than FontAwesome + # Other Icon sets paste(iconset, x$icon) } tagList(tags$span(class = class), " ", x$text, ...)