Skip to content

Commit

Permalink
resolve the implicit latest version on jsdelivr to explicit current l…
Browse files Browse the repository at this point in the history
…atest version, so that the HTML output won't be subject to future changes in jsdelivr assets, and I won't need to update the latest version numbers manually any more
  • Loading branch information
yihui committed Aug 28, 2024
1 parent b5cfcf0 commit efc27da
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: litedown
Type: Package
Title: A Lightweight Version of R Markdown
Version: 0.1.4
Version: 0.1.5
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person()
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- A data frame (or matrix/tibble) wrapped in `I()` is fully printed to a table now by default. Without `I()`, data objects are truncated to 10 rows by default when printing to tables.

- The implicit latest version of jsdelivr resources will be resolved to an explicit version, e.g., `https://cdn.jsdelivr.net/npm/@xiee/utils/css/default.css` will be resolved to `https://cdn.jsdelivr.net/npm/@xiee/[email protected]/css/default.css`, where `X.Y.Z` is the current latest version. This will make sure the HTML output containing jsdelivr resources is stable.

# CHANGES IN litedown VERSION 0.1

- Initial CRAN release.
11 changes: 4 additions & 7 deletions R/site.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ fuse_site = function(input = '.') {
}
opts = yaml_field(info$yaml, 'html', c('meta', 'options'))
opts[['meta']] = merge_list(list(
css = jsd_version(c("@default", "@article", '@site')),
js = jsd_version(c("@sidenotes", "@appendix", "@toc-highlight")),
css = c("@default", "@article", '@site'),
js = c("@sidenotes", "@appendix", "@toc-highlight"),
include_before = nav_menu(info), include_after = format(Sys.Date(), '© %Y')
), opts[['meta']])
opts[['options']] = merge_list(
Expand Down Expand Up @@ -256,8 +256,8 @@ fuse_book = function(input = '.', output = NULL, envir = parent.frame()) {
)
})
tweak_options(format, yaml, list(
body_class = '', css = jsd_version(c("@default", "@article", "@book")),
js = jsd_version(c("@sidenotes", "@appendix", "@toc-highlight"))
body_class = '', css = c("@default", "@article", "@book"),
js = c("@sidenotes", "@appendix", "@toc-highlight")
), toc = length(preview) == 0)
fuse_output(input[1], output, unlist(res), full)
}
Expand Down Expand Up @@ -311,6 +311,3 @@ tweak_options = function(format, yaml, meta = NULL, toc = TRUE, options = NULL)
opts = options(defaults)
xfun::exit_call(function() options(opts))
}

# use a specific version of jsdelivr assets
jsd_version = function(x, v = '@1.13.19') paste0(x, v)
29 changes: 29 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,34 @@ jsdelivr = function(file, dir = 'npm/@xiee/utils/') {
ifelse(is_https(file), file, sprintf('https://cdn.jsdelivr.net/%s%s', dir, file))
}

# get the latest version of jsdelivr assets
jsd_version = local({
vers = list() # cache versions in current session
function(pkg) {
if (is.character(v <- vers[[pkg]])) return(v)
x = tryCatch(
xfun::read_utf8(paste0('https://data.jsdelivr.com/v1/package/', pkg)),
error = function(e) ''
)
v = grep_sub('.*"latest":\\s*"([0-9.]+)".*', '@\\1', x)
vers[[pkg]] <<- if (length(v)) v[1] else ''
}
})

jsd_versions = function(pkgs) unlist(lapply(pkgs, jsd_version))

# resolve the implicit latest version to current latest version
jsd_resolve = function(x) {
rs = paste0(c(
'((?<=https://cdn.jsdelivr.net/combine/)|(?<=,))',
'(?<=https://cdn.jsdelivr.net/)(?!combine/)'
), '([^/]+/(@[^/]+/)?[^/@]+)(?=/)')
for (r in rs) x = match_replace(x, r, function(z) {
paste0(z, jsd_versions(z))
})
x
}

# if both @foo and foo are present, remove foo
resolve_dups = function(x) {
x = unique(x)
Expand Down Expand Up @@ -968,6 +996,7 @@ resolve_files = function(x, ext = 'css') {
paste0('combine/', paste(z, collapse = ','))
})
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)
Expand Down
4 changes: 2 additions & 2 deletions docs/_litedown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ book:
output:
litedown::html_format:
meta:
css: ["@default@1.13.19", "@article@1.13.19", "@book@1.13.19", "@copy-button", "@heading-anchor", "@key-buttons", "@callout", "@pages@1.13.19"]
js: ["@sidenotes", "@appendix", "@toc-highlight", "@copy-button", "@external-link", "@heading-anchor", "@right-quote", "@key-buttons", "@callout", "@pages@1.13.19"]
css: ["@default", "@article", "@book", "@copy-button", "@heading-anchor", "@key-buttons", "@callout", "@pages"]
js: ["@sidenotes", "@appendix", "@toc-highlight", "@copy-button", "@external-link", "@heading-anchor", "@right-quote", "@key-buttons", "@callout", "@pages"]

0 comments on commit efc27da

Please sign in to comment.