From a45cd3da8c2176a73234f778501bd3f0b584e570 Mon Sep 17 00:00:00 2001
From: Yihui Xie
Date: Mon, 16 Dec 2024 11:33:31 -0600
Subject: [PATCH] fix #61: use the internal .env to manage the automatically
added css/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
---
DESCRIPTION | 2 +-
NEWS.md | 2 ++
R/mark.R | 43 ++++++++++++++++++++++---------------------
R/package.R | 8 ++++----
R/utils.R | 22 +++++++++++++---------
5 files changed, 42 insertions(+), 35 deletions(-)
diff --git a/DESCRIPTION b/DESCRIPTION
index 2abcd35..d002732 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -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 = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666", URL = "https://yihui.org")),
person()
diff --git a/NEWS.md b/NEWS.md
index 632294c..9563a61 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -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.
diff --git a/R/mark.R b/R/mark.R
index 6b4e043..ff0acce 100644
--- a/R/mark.R
+++ b/R/mark.R
@@ -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('(
]*?)disabled="" (/>)', '\\1\\2', ret)
@@ -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 = '
(.*?)
'
- if (length(grep(r_mmd, ret))) {
+ if (has_mermaid <- length(grep(r_mmd, ret))) {
ret = gsub(r_mmd, '
\\1
', 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 = '(
'
# deal with ```{.class1 .class2 attrs}, which is not supported by commonmark
@@ -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('$$
', ret, fixed = TRUE) || grepl('\\)', 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)
@@ -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), '.')
)
@@ -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('', d, ''), '\n', collapse = '\n'
), '\n'
)
- new_asis(c(pkg_style(), res))
+ set_meta(css = '@manual')
+ new_asis(res)
}
# format authors, adding URL and ORCID links as appropriate
@@ -371,11 +372,10 @@ pkg_manual = function(
res = gsub('', '
', 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)
diff --git a/R/utils.R b/R/utils.R
index 0e09f9e..ace45da 100644
--- a/R/utils.R
+++ b/R/utils.R
@@ -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
@@ -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('