Skip to content

Commit

Permalink
Use user cache dir + html dep
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed May 28, 2024
1 parent 319fb45 commit 4cf0d92
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 63 deletions.
108 changes: 90 additions & 18 deletions R/external-assets.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,94 @@
assemble_ext_assets <- function(pkg) {
path_assets_yaml <- path_pkgdown(paste0("BS", pkg$bs_version), "assets_external.yaml")
deps_ext <- yaml::read_yaml(path_assets_yaml)

purrr::map_chr(deps_ext, ~ {
path <- path_deps(pkg, basename(.x$url))
download.file(.x$url, path, quiet = TRUE, mode = "wb")
check_integrity(path, .x$integrity)

.x$url <- fs::path_rel(path, pkg$dst_path)

# assemble HTML tag
switch(
.x$type,
"stylesheet" = sprintf('<link rel="stylesheet" href="%s" />', .x$url),
"script" = sprintf('<script src="%s"></script>', .x$url),
cli::cli_abort("Unknown asset type {.val {.x$type}} defined in {.file path_assets_yaml}.")
external_dependencies <- function() {
list(
cached_dependency(
name = "headroom",
version = "0.11.0",
files = list(
list(
url = "https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js",
integrity = "sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0="
),
list(
url = "https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js",
integrity = "sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4="
)
)
),
cached_dependency(
name = "bootstrap-toc",
version = "1.0.1",
files = list(
list(
url = "https://cdn.jsdelivr.net/gh/afeld/[email protected]/dist/bootstrap-toc.min.js",
integrity = "sha256-4veVQbu7//Lk5TSmc7YV48MxtMy98e26cf5MrgZYnwo="
)
)
),
cached_dependency(
name = "clipboard.js",
version = "2.0.11",
files = list(
list(
url = "https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js",
integrity = "sha512-7O5pXpc0oCRrxk8RUfDYFgn0nO1t+jLuIOQdOMRp4APB7uZ4vSjspzp5y6YDtDs4VzUSTbWzBFZ/LKJhnyFOKw=="
)
)
),
cached_dependency(
name = "search",
version = "1.0.0",
files = list(
list(
url = "https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.min.js",
integrity = "sha512-KnvCNMwWBGCfxdOtUpEtYgoM59HHgjHnsVGSxxgz7QH1DYeURk+am9p3J+gsOevfE29DV0V+/Dd52ykTKxN5fA=="
),
list(
url = "https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js",
integrity = "sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg=="
),
list(
url = "https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js",
integrity = "sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww=="
)
)
),
cached_dependency(
name = "MathJax",
version = "2.7.5",
files = list(
list(
url = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js",
integrity = "sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k="
),
list(
url = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js",
integrity = "sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA="
)
)
)
})
)
}

cached_dependency <- function(name, version, files) {
cache_dir <- path(tools::R_user_dir("pkgdown", "cache"), name, version)
dir_create(cache_dir)

for (file in files) {
cache_path <- path(cache_dir, path_file(file$url))
if (!file_exists(cache_path)) {
download.file(file$url, cache_path, quiet = TRUE, mode = "wb")
check_integrity(cache_path, file$integrity)
}
}
dep_files <- dir(cache_dir)

htmltools::htmlDependency(
name = name,
version = version,
src = cache_dir,
script = dep_files[path_ext(dep_files) == "js"],
stylesheet = dep_files[path_ext(dep_files) == "css"]
)
}

check_integrity <- function(path, integrity) {
Expand Down
11 changes: 2 additions & 9 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ build_bslib <- function(pkg = ".", call = caller_env()) {

deps <- c(
bslib::bs_theme_dependencies(bs_theme),
list(fontawesome::fa_html_dependency())
list(fontawesome::fa_html_dependency()),
external_dependencies()
)

deps <- lapply(deps, htmltools::copyDependencyToDir, path_deps(pkg))
Expand All @@ -27,14 +28,6 @@ build_bslib <- function(pkg = ".", call = caller_env()) {
}

head <- htmltools::renderDependencies(deps, srcType = "file")

# include additional external assets
head <- paste(
head,
paste0(assemble_ext_assets(pkg), collapse = "\n"),
sep = "\n"
)

write_lines(head, path_data_deps(pkg))
}

Expand Down
36 changes: 0 additions & 36 deletions inst/BS5/assets_external.yaml

This file was deleted.

9 changes: 9 additions & 0 deletions tests/testthat/_snaps/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
Copying <pkgdown>/BS5/assets/link.svg to link.svg
Copying <pkgdown>/BS5/assets/pkgdown.js to pkgdown.js
Copying pkgdown/extra.css to extra.css
Updating deps/MathJax-2.7.5/MathJax.js
Updating deps/MathJax-2.7.5/TeX-AMS-MML_HTMLorMML.js
Updating deps/bootstrap-5.3.1/bootstrap.bundle.min.js
Updating deps/bootstrap-5.3.1/bootstrap.bundle.min.js.map
Updating deps/bootstrap-5.3.1/bootstrap.min.css
Updating deps/bootstrap-toc-1.0.1/bootstrap-toc.min.js
Updating deps/clipboard.js-2.0.11/clipboard.min.js
Updating deps/font-awesome-6.4.2/css/all.css
Updating deps/font-awesome-6.4.2/css/all.min.css
Updating deps/font-awesome-6.4.2/css/v4-shims.css
Expand All @@ -37,9 +41,14 @@
Updating deps/font-awesome-6.4.2/webfonts/fa-solid-900.woff2
Updating deps/font-awesome-6.4.2/webfonts/fa-v4compatibility.ttf
Updating deps/font-awesome-6.4.2/webfonts/fa-v4compatibility.woff2
Updating deps/headroom-0.11.0/headroom.min.js
Updating deps/headroom-0.11.0/jQuery.headroom.min.js
Updating deps/jquery-3.6.0/jquery-3.6.0.js
Updating deps/jquery-3.6.0/jquery-3.6.0.min.js
Updating deps/jquery-3.6.0/jquery-3.6.0.min.map
Updating deps/search-1.0.0/autocomplete.jquery.min.js
Updating deps/search-1.0.0/fuse.min.js
Updating deps/search-1.0.0/mark.min.js

# site meta doesn't break unexpectedly

Expand Down

0 comments on commit 4cf0d92

Please sign in to comment.