From 85eed64336854cea933f1d2eb69308c70fed1d70 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Wed, 25 Dec 2024 22:05:22 -0600 Subject: [PATCH] make `@` optional for jsdelivr resources from `npm/@xiee/utils` (sometimes even I forget to type @) --- R/utils.R | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/R/utils.R b/R/utils.R index 8168797..67cfa4d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1140,20 +1140,30 @@ resolve_files = function(x, ext = 'css') { x[i] = jsdelivr(x[i], '') x[i0] = jsd_resolve(x[i0]) - # built-in resources in this package i = dirname(x) == '.' & file_ext(x) == '' & !file_exists(x) - x[i & (x == 'slides')] = 'snap' # alias slides.css -> snap.css - files = list.files(pkg_file('resources'), sprintf('[.]%s$', ext), full.names = TRUE) - b = sans_ext(basename(files)) - if (any(!x[i] %in% b)) stop( - "Invalid '", ext, "' option: ", paste0("'", setdiff(x[i], b), "'", collapse = ', '), - " (possible values are: ", paste0("'", b, "'", collapse = ', '), ")" - ) - x[i] = files[match(x[i], b)] + x[i] = map_assets(x[i], ext) x = if (ext %in% c('css', 'js')) gen_tags(x, ext) else read_all(x) I(x) } +map_assets = function(x, ext) { + x[x == 'slides'] = 'snap' # alias slides.css -> snap.css + # built-in resources in this package + b1 = c(if (ext != 'js') 'default', 'snap') + i1 = x %in% b1 + x[i1] = pkg_file('resources', paste0(x[i1], '.', ext)) + # in case users forgot to type @ for jsdelivr assets + b2 = sub('@', '', assets[, ext]) + i2 = x %in% b2 + x[i2] = jsdelivr(sprintf('%s/%s.min.%s', ext, x[i2], ext)) + x[i2] = jsd_resolve(x[i2]) + if (any(i3 <- !(i1 | i2))) stop( + "Invalid '", ext, "' option: ", paste0("'", x[i3], "'", collapse = ', '), + " (possible values are: ", paste0("'", unique(c(b1, na.omit(b2))), "'", collapse = ', '), ")" + ) + x +} + # generate tags for css/js depending on whether they need to be embedded or offline gen_tag = function(x, ext = file_ext(x), embed_https = FALSE, embed_local = FALSE) { if (ext == 'css') {