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

Defer JavaScript scripts in html_document #2046

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
16 changes: 12 additions & 4 deletions R/html_dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ html_dependency_accessible_code_block <- function() {
name = "accessible-code-block",
version = "0.0.1",
src = pkg_file("rmd/h/accessibility"),
script = "empty-anchor.js"
script = list(
src = "empty-anchor.js",
defer = NA
)
)
}

Expand Down Expand Up @@ -207,11 +210,13 @@ html_dependency_navigation <- function(code_menu, source_embed) {
}

html_dependency_anchor_sections <- function() {

htmlDependency(name = "anchor-sections",
version = "1.0.1",
src = pkg_file("rmd/h/anchor-sections"),
script = "anchor-sections.js",
script = list(
src = "anchor-sections.js",
defer = NA
),
stylesheet = "anchor-sections.css")
}

Expand Down Expand Up @@ -426,7 +431,10 @@ html_dependency_header_attrs <- function() {
"header-attrs",
version = packageVersion("rmarkdown"),
src = pkg_file("rmd/h/pandoc"),
script = "header-attrs.js"
script = list(
src = "header-attrs.js",
defer = NA
)
)
)
}
4 changes: 2 additions & 2 deletions inst/rmd/h/accessibility/empty-anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// v0.0.1
// Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020.

document.addEventListener('DOMContentLoaded', function() {
(function() {
const codeList = document.getElementsByClassName("sourceCode");
for (var i = 0; i < codeList.length; i++) {
var linkList = codeList[i].getElementsByTagName('a');
Expand All @@ -12,4 +12,4 @@ document.addEventListener('DOMContentLoaded', function() {
}
}
}
});
})();
4 changes: 2 additions & 2 deletions inst/rmd/h/anchor-sections/anchor-sections.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Anchor sections v1.0 written by Atsushi Yasumoto on Oct 3rd, 2020.
document.addEventListener('DOMContentLoaded', function() {
(function() {
// Do nothing if AnchorJS is used
if (typeof window.anchors === 'object' && anchors.hasOwnProperty('hasAnchorJSLink')) {
return;
Expand Down Expand Up @@ -30,4 +30,4 @@ document.addEventListener('DOMContentLoaded', function() {
x.classList.add('hasAnchor');
x.appendChild(anchor);
});
});
})();
8 changes: 5 additions & 3 deletions inst/rmd/h/pandoc/header-attrs.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
// be compatible with the behavior of Pandoc < 2.8).
document.addEventListener('DOMContentLoaded', function(e) {
(function() {
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
var i, h, a;
var i, h, a, hasAnchor;
for (i = 0; i < hs.length; i++) {
h = hs[i];
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
hasAnchor = h.classList.contains('hasAnchor');
a = h.attributes;
while (a.length > 0) h.removeAttribute(a[0].name);
if (hasAnchor) h.classList.add('hasAnchor'); // avoid a conflict with anchor-sections.js
}
});
})();