Skip to content
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

Improve HTML5 compliance #2523

Merged
merged 7 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# pkgdown (development version)

* Improve HTML5 compliance (#2369):
* No longer support IE9 or earlier
* Strip trailing whitespace
* Label `<nav>`s and improve navbar html.
* `build_home_index()` now renders math if you use it in your home page (#2263).
* `build_home()` now correctly escapes special HTML characters in the bibtex citation (#2022).
* BS5 templates no longer include empty link to logo when none exists (#2536).
Expand Down
36 changes: 18 additions & 18 deletions R/navbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,16 @@ bs4_navbar_links_tags <- function(links, depth = 0L, side = "left") {
return(
htmltools::tags$li(
class = menu_class,
htmltools::tags$a(
href = "#", class = "nav-link dropdown-toggle",
`data-bs-toggle` = "dropdown", role = "button",
`aria-expanded` = "false", `aria-haspopup` = "true",
htmltools::tags$button(
href = "#",
class = "nav-link dropdown-toggle",
`data-bs-toggle` = "dropdown",
type = "button",
`aria-expanded` = "false",
`aria-haspopup` = "true",
link_text,
id = paste0("dropdown-", make_slug(link_text)),
"aria-label" = x$`aria-label` %||% NULL
"aria-label" = x$`aria-label` %||% NULL
),
htmltools::tags$div(
class = dropdown_class,
Expand Down Expand Up @@ -336,24 +339,21 @@ bs4_navbar_links_tags <- function(links, depth = 0L, side = "left") {

tags <- purrr::map2(links, seq_along(links), tackle_link, is_submenu = is_submenu, depth = depth)
htmltools::tagList(tags)

}

bs4_navbar_link_text <- function(x, ...) {

if (!is.null(x$icon)) {
# find the iconset
split <- strsplit(x$icon, "-")
if (length(split[[1]]) > 1) {
iconset <- split[[1]][[1]]
}
else {
iconset <- ""
}
htmltools::tagList(htmltools::tags$span(class = paste(iconset, x$icon)), " ", x$text, ...)
}
else
# Find the icon set
classes <- strsplit(x$icon, " ")[[1]]
icon_classes <- classes[grepl("-", classes)]
hadley marked this conversation as resolved.
Show resolved Hide resolved
iconset <- purrr::map_chr(strsplit(icon_classes, "-"), 1)
class <- paste0(unique(c(iconset, classes)), collapse = " ")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why `paste0()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just use it everywhere so I don't have to remember


text <- paste0(if (!is.null(x$text)) " ", x$text)
htmltools::tagList(htmltools::tags$span(class = class), text, ...)
} else {
htmltools::tagList(x$text, ...)
}
}

# Testing helpers ---------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ render_page_html <- function(pkg, name, data = list(), depth = 0L) {
template <- find_template("layout", name, pkg = pkg)
rendered <- render_template(template, components)

# Strip trailing whitespace
rendered <- gsub("\\s+\n", "\n", rendered, perl = TRUE)

xml2::read_html(rendered, encoding = "UTF-8")
}

Expand Down Expand Up @@ -98,7 +101,9 @@ data_template <- function(pkg = ".", depth = 0L) {
version = tr_("Version"),
examples = tr_("Examples"),
citation = tr_("Citation"),
author_details = tr_("Additional details")
author_details = tr_("Additional details"),
toc = tr_("Table of contents"),
site_nav = tr_("Site navigation")
)

# Components that mostly end up in the <head>
Expand Down
2 changes: 1 addition & 1 deletion inst/BS5/templates/content-article-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h3>{{{title}}}</h3>
</main>

<aside class="col-md-3">
<nav id="toc">
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
</nav>
</aside>
Expand Down
2 changes: 1 addition & 1 deletion inst/BS5/templates/content-article.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ <h4 data-toc-skip class="date">$date$</h4>
</main>
{{#toc}}
<aside class="col-md-3">
<nav id="toc">
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
</nav>
</aside>
Expand Down
2 changes: 1 addition & 1 deletion inst/BS5/templates/content-authors.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h1>{{{pagetitle}}}</h1>
</main>

<aside class="col-md-3">
<nav id="toc">
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
</nav>
</aside>
Expand Down
2 changes: 1 addition & 1 deletion inst/BS5/templates/content-citation-authors.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ <h2>{{#translate}}{{author_details}}{{/translate}}</h2>
</main>

<aside class="col-md-3">
<nav id="toc">
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
</nav>
</aside>
Expand Down
2 changes: 1 addition & 1 deletion inst/BS5/templates/content-news-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1>{{{pagetitle}}}</h1>
</main>

<aside class="col-md-3">
<nav id="toc">
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
</nav>
</aside>
Expand Down
2 changes: 1 addition & 1 deletion inst/BS5/templates/content-news.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1>{{{pagetitle}}}</h1>
</main>

<aside class="col-md-3">
<nav id="toc">
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
</nav>
</aside>
Expand Down
4 changes: 2 additions & 2 deletions inst/BS5/templates/content-reference-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<h1>{{{pagetitle}}}</h1>
</div>

{{#rows}}<div id="{{slug}}" class="section level2">
{{#rows}}<div {{#slug}}id="{{.}}"{{/slug}} class="section level2">
maelle marked this conversation as resolved.
Show resolved Hide resolved
{{#title}}<h2>{{{.}}}</h2>{{/title}}
{{#subtitle}}<h3>{{{.}}}</h3>{{/subtitle}}
{{#desc}}<div class="section-desc">{{{desc}}}</div>{{/desc}}
Expand All @@ -21,7 +21,7 @@ <h1>{{{pagetitle}}}</h1>
</main>

<aside class="col-md-3">
<nav id="toc">
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
</nav>
</aside>
Expand Down
2 changes: 1 addition & 1 deletion inst/BS5/templates/content-reference-topic.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ <h2>{{#translate}}{{examples}}{{/translate}}</h2>
{{/examples}}
</main>
<aside class="col-md-3">
<nav id="toc">
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
</nav>
</aside>
Expand Down
2 changes: 1 addition & 1 deletion inst/BS5/templates/content-title-body.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h1>{{pagetitle}}</h1>
</main>

<aside class="col-md-3">
<nav id="toc">
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
</nav>
</aside>
Expand Down
2 changes: 1 addition & 1 deletion inst/BS5/templates/content-tutorial-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h1>{{{pagetitle}}}</h1>
</main>

<aside class="col-md-3">
<nav id="toc">
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
</nav>
</aside>
Expand Down
2 changes: 1 addition & 1 deletion inst/BS5/templates/content-tutorial.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h1>{{pagetitle}}</h1>
</div>
</main>
<aside class="col-md-3">
<nav id="toc">
<nav id="toc" aria-label="{{#translate}}{{toc}}{{/translate}}">
<h2>{{#translate}}{{on_this_page}}{{/translate}}</h2>
</nav>
</aside>
Expand Down
5 changes: 0 additions & 5 deletions inst/BS5/templates/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script>

<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

{{#yaml}}{{#ganalytics}}
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{.}}"></script>
Expand Down
2 changes: 1 addition & 1 deletion inst/BS5/templates/navbar.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{#navbar}}
<nav class="navbar fixed-top navbar-{{{type}}} navbar-expand-lg bg-{{{bg}}}" data-bs-theme="{{{type}}}">
<nav class="navbar fixed-top navbar-{{{type}}} navbar-expand-lg bg-{{{bg}}}" data-bs-theme="{{{type}}}" aria-label="{{#translate}}{{site_nav}}{{/translate}}">
<div class="container">
{{#includes}}{{{before_title}}}{{/includes}}
<a class="navbar-brand me-2" href="{{#site}}{{root}}{{/site}}index.html">{{#site}}{{title}}{{/site}}</a>
Expand Down
Loading
Loading