Skip to content

Commit

Permalink
fix #61: use the internal .env to manage the automatically added css/…
Browse files Browse the repository at this point in the history
…js assets instead of manipulating the `meta` object directly, otherwise `css` would be present in `meta` when math/mermaid/prism is used and the default css wouldn't be added
  • Loading branch information
yihui committed Dec 16, 2024
1 parent 06a5de4 commit a45cd3d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 35 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.4.10
Version: 0.4.11
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666", URL = "https://yihui.org")),
person()
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

- Added an argument `examples` to `pkg_manual()` to run examples and show their output (thanks, @TimTaylor, #54).

- Fixed a bug that the default CSS wouldn't be added when a math expression exists on the page (thanks, @calvinw, #61).

- Fixed a bug that cross-references to other chapters of a book could not be resolved when previewing a single chapter.

- Fixed a bug that the file navigation by line numbers on code blocks stopped working in `litedown::roam()` due to yihui/lite.js@5e06d19.
Expand Down
43 changes: 22 additions & 21 deletions R/mark.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ mark = function(input, output = NULL, text = NULL, options = NULL, meta = list()
maths[x]
})

has_mermaid = FALSE

if (format == 'html') {
# don't disable check boxes
ret = gsub('(<li><input type="checkbox" [^>]*?)disabled="" (/>)', '\\1\\2', ret)
Expand Down Expand Up @@ -255,12 +257,8 @@ mark = function(input, output = NULL, text = NULL, options = NULL, meta = list()
}, perl = FALSE) # for perl = TRUE, we'd need (?s) before (.+?)
# support mermaid
r_mmd = '<pre><code class="language-mermaid">(.*?)</code></pre>'
if (length(grep(r_mmd, ret))) {
if (has_mermaid <- length(grep(r_mmd, ret))) {
ret = gsub(r_mmd, '<pre class="mermaid">\\1</pre>', ret)
# add the js asset automatically if not detected
if (length(grep('mermaid', meta[['js']])) == 0) meta = add_meta(
meta, c(js = '@npm/mermaid/dist/mermaid.min.js')
)
}
r4 = '(<pre><code class="language-)\\{([^"]+)}">'
# deal with ```{.class1 .class2 attrs}, which is not supported by commonmark
Expand All @@ -287,14 +285,13 @@ mark = function(input, output = NULL, text = NULL, options = NULL, meta = list()
if (isTRUE(options[['number_sections']])) ret = number_sections(ret)
# build table of contents
ret = add_toc(ret, options)
# add js/css for math
# math
if (!has_math) has_math = length(ret) && (
grepl('$$</p>', ret, fixed = TRUE) || grepl('\\)</span>', ret, fixed = TRUE)
) # math may be from pkg_manual()'s HTML
is_katex = TRUE
if (has_math && length(js_math <- js_options(options[['js_math']], 'katex'))) {
is_katex = js_math$package == 'katex'
meta = set_math(meta, js_math, is_katex)
}
# number figures and tables, etc.
ret = number_refs(ret, r_ref, is_katex)
Expand Down Expand Up @@ -358,10 +355,20 @@ mark = function(input, output = NULL, text = NULL, options = NULL, meta = list()
clever = isTRUE(options[['cleveref']])
if (format == 'latex') ret = latex_refs(ret, r_ref, clever) else clever = FALSE

meta$body = I(ret)

# use the template (if provided) to create a standalone document
if (is.character(template)) {
meta$body = I(ret)
if (format == 'html') {
# clear the internal js/css
on.exit(rm(list = c('js', 'css'), envir = .env), add = TRUE)
# add js/css for math
if (has_math) set_math(js_math, is_katex)
# add js/css for syntax highlighting
set_highlight(options, ret)
# add js for mermaid
if (has_mermaid && length(grep('mermaid', meta[['js']])) == 0)
set_meta(js = '@npm/mermaid/dist/mermaid.min.js')
}
ret = build_output(
format, options, template, meta, test = c(if (length(input)) dirname(input), '.')
)
Expand Down Expand Up @@ -409,19 +416,13 @@ mark = function(input, output = NULL, text = NULL, options = NULL, meta = list()
build_output = function(format, options, template, meta, ...) {
tpl = one_string(template, ...)
if (format == 'html') {
b = meta$body
set_meta = function(name, value) {
if (!name %in% names(meta)) meta[[name]] <<- value
}
set_meta('css', 'default')
set_meta('plain-title', I(str_trim(commonmark::markdown_text(meta[['title']]))))
meta = set_highlight(meta, options, b)
# if the class .line-numbers is present, add js/css for line numbers
if (any(grepl('<code class="[^"]*line-numbers', b))) for (i in c('css', 'js')) {
meta[[i]] = c(meta[[i]], '@code-line-numbers')
}
defaults = list(
'css' = 'default',
'plain-title' = I(str_trim(commonmark::markdown_text(meta[['title']])))
)
for (i in setdiff(names(defaults), names(meta))) meta[[i]] = defaults[[i]]
# special handling for css/js "files" that have no extensions
for (i in c('css', 'js')) meta[[i]] = resolve_files(meta[[i]], i)
for (i in c('css', 'js')) meta[[i]] = resolve_files(c(meta[[i]], .env[[i]]), i)
}
sub_vars(tpl, meta, ...)
}
Expand Down
8 changes: 4 additions & 4 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ pkg_desc = function(name = detect_pkg()) {
paste0('\n<td>', d, '</td>'), '\n</tr>', collapse = '\n'
), '\n</tbody></table>'
)
new_asis(c(pkg_style(), res))
set_meta(css = '@manual')
new_asis(res)
}

# format authors, adding URL and ORCID links as appropriate
Expand Down Expand Up @@ -371,11 +372,10 @@ pkg_manual = function(
res = gsub('<code class="language-R"', '<code class="language-r"', res, fixed = TRUE)
res = gsub('&#8288;', '', res, fixed = TRUE)
res = gsub('<table>', '<table class="table-full">', res, fixed = TRUE)
new_asis(c(pkg_style(), toc, res))
set_meta(css = '@manual')
new_asis(c(toc, res))
}

pkg_style = function() gen_tag(jsd_resolve(jsdelivr('css/manual.min.css')))

run_examples = function(html, config, path) {
config$dev.path = path = paste0(config$dev.path, path)
on.exit(xfun::del_empty_dir(dirname(path)), add = TRUE)
Expand Down
22 changes: 13 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ restore_html = function(x) {
}

# set js/css variables according to the js_math option
set_math = function(meta, o, is_katex) {
set_math = function(o, is_katex) {
if (is_katex) o$js = c(o$js, 'dist/contrib/auto-render.min.js')
js = js_combine(sprintf('npm/%s%s/%s', o$package, o$version, o$js))
js = if (is_katex) c(js, '@render-katex') else c('@mathjax-config', js)
css = sprintf('@npm/%s%s/%s', o$package, o$version, o$css)
add_meta(meta, list(js = js, css = css))
set_meta(js = js, css = css)
}

# use jsdelivr's combine feature
Expand Down Expand Up @@ -242,16 +242,20 @@ js_libs = list(
)
)

add_meta = function(x, v) {
for (i in names(v)) x[[i]] = c(v[[i]], x[[i]])
x
set_meta = function(...) {
v = list(...)
for (i in names(v)) .env[[i]] = c(.env[[i]], v[[i]])
}

# set js/css variables according to the js_highlight option
set_highlight = function(meta, options, html) {
set_highlight = function(options, html) {
# if the class .line-numbers is present, add js/css for line numbers
if (any(grepl('<code class="[^"]*line-numbers', html)))
set_meta(js = '@code-line-numbers', css = '@code-line-numbers')

r = '(?<=<code class="language-)([^"]+)(?=")'
if (!any(grepl(r, html, perl = TRUE))) return(meta)
if (!length(o <- js_options(options[['js_highlight']], 'prism'))) return(meta)
if (!any(grepl(r, html, perl = TRUE))) return()
if (!length(o <- js_options(options[['js_highlight']], 'prism'))) return()

p = o$package
# return jsdelivr subpaths
Expand Down Expand Up @@ -302,7 +306,7 @@ set_highlight = function(meta, options, html) {
# embedding faster because each js is a separate URL that has been downloaded)
js = if (is.null(l)) paste0('@', js) else js_combine(js)

add_meta(meta, list(js = js, css = css))
set_meta(js = js, css = css)
}

# figure out which language support files are needed for highlight.js/prism.js
Expand Down

0 comments on commit a45cd3d

Please sign in to comment.