From 2bedd23d1af90881b3be76ff648d720a41d18321 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Thu, 19 Nov 2026 17:56:24 -0500 Subject: [PATCH 1/4] roam(): preview a book's index.Rmd alone or as the whole book `?preview=2` now always renders a single file; the new `?preview=3` renders the whole book for the book index (same as 2 for other files). In the file listing, the Run button of a book's index.Rmd runs it alone, and a new "run to end" button runs the whole book. The toolbar Run button of the index runs the whole book, and the index chapter in a whole-book page gets its own Run button. Co-Authored-By: Claude Code --- .github/workflows/R-CMD-check.yaml | 2 +- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/preview.R | 28 +++++++++++++++++++++------- inst/resources/listing.css | 2 +- inst/resources/server.css | 3 +++ inst/resources/server.js | 2 +- tests/test-cran/test-preview.R | 25 +++++++++++++++++++++++++ 8 files changed, 55 insertions(+), 11 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 05279acf..b68f82e7 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -61,7 +61,7 @@ jobs: - uses: yihui/actions/setup-r-dependencies@HEAD with: # install the package itself as we register vignette engine - extra-packages: . ${{ env.USE_R_CODECOV == 'true' && ' covr' || '' }} + extra-packages: . ${{ env.USE_R_CODECOV == 'true' && ' covr pkgbuild' || '' }} # install dependencies for cairo_pdf device brew-packages: xquartz diff --git a/DESCRIPTION b/DESCRIPTION index 98b8581b..e11474b4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: litedown Type: Package Title: A Lightweight Version of R Markdown -Version: 1.0.23 +Version: 1.0.24 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("Tim", "Taylor", role = "ctb", comment = c(ORCID = "0000-0002-8587-7113")), diff --git a/NEWS.md b/NEWS.md index a22dfe7b..3e922bc2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -32,6 +32,8 @@ - Code blocks are now syntax highlighted by the lightweight `prism-lite.js`, which needs no language files. Support for Prism.js and highlight.js has been removed from the `js_highlight` option, which now only turns highlighting on or off. To use either library, set `js_highlight: false` and load it via the `css` and `js` variables, as shown in [Using Prism.js or highlight.js](https://pkg.yihui.org/litedown/book/#sec:other-highlighters). +- In the `litedown::roam()` preview of a [book](https://pkg.yihui.org/litedown/book/#sec:live-preview), the Run button of `index.Rmd` in the file listing now previews `index.Rmd` alone, and a new "run to end" button next to it (as well as the Run button in the top toolbar) previews the whole book. + # CHANGES IN litedown VERSION 1.0 - The Markdown backend has been changed from the **commonmark** package to a copy of 'cmark-gfm' bundled with **litedown**, with our own patches and extensions. Features that used to be emulated with fragile text processing in R (LaTeX math, superscript/subscript/strikethrough, raw content blocks, and inline attributes on links and images) are now handled directly by the Markdown parser, which is faster and more reliable, especially for large documents and documents with multibyte (e.g., CJK) characters. This also fixes several long-standing rendering bugs (#33, #135, #139). The **commonmark** package is no longer a dependency (#141, #142, #143). Rendering output is essentially unchanged, with one minor exception: superscript, subscript, and strikethrough now require the delimiters to "hug" their content (e.g., `a^b^` works, but `a ^ b ^` no longer produces a superscript), consistent with how `*emphasis*` works. diff --git a/R/preview.R b/R/preview.R index a73514c2..d525d8e8 100644 --- a/R/preview.R +++ b/R/preview.R @@ -280,11 +280,14 @@ dir_page = function(dir = '.') { # order first by folder, then by .Rmd/.R, and other files go to the end boxes = lapply(files[i1][order(grepl('^[_.]', basename(files[i1])))], function(f) { b = basename(f) + book_idx = is_book_index(f) txt = readLines(f, n = 10, encoding = 'UTF-8', warn = FALSE) fenced_div(c( fenced_div(c( p_link(b, a = NULL), info(f, b), - p_link(b, '.run', 2, 'title="Run in memory"') + p_link(b, '.run', 2, 'title="Run in memory"'), + # the book index can also be run as the whole book + if (book_idx) p_link(b, '.run1', 3) ), '.caption .name'), fenced_block(txt) ), c('.box', muted(f))) @@ -341,9 +344,11 @@ is_lite_ext = function(ext = file_ext(file), file) tolower(ext) %in% lite_exts # render the path to HTML if possible file_resp = function(x, preview) { - raw = preview == '0' # 0: send raw response; 1: render verbatim; 2: fuse()/mark() + # 0: send raw response; 1: render verbatim; 2: fuse()/mark(); 3: same as 2 + # but render the whole book if the file is the index of a book + raw = preview == '0' ext = if (raw) '' else tolower(file_ext(x)) - if (preview == '2' && is_lite_ext(ext)) { + if (preview %in% c('2', '3') && is_lite_ext(ext)) { # to clean up the __files/ dir if requested (via options()) and the dir # didn't exist before if (getOption('litedown.roam.cleanup', FALSE)) { @@ -358,7 +363,10 @@ file_resp = function(x, preview) { info$type, book = { store_book(info$root, x, info$index) - fuse_book(if (info$index) info$root else x, full_output, globalenv()) + fuse_book( + if (info$index && preview == '3') info$root else x, full_output, + globalenv() + ) }, site = fuse_site(x), if (ext == 'md') mark_full(x) else fuse(x, full_output, envir = globalenv()) @@ -376,6 +384,9 @@ file_resp = function(x, preview) { } } +# is the file the index of a book? +is_book_index = function(x) is_index(x) && with(proj_info(x), type == 'book' && index) + # store book dir for books to resolve number_refs() because the book may be # partially rendered (in which case we can't resolve refs to other chapters) store_book = function(dir, x, index = FALSE) { @@ -438,8 +449,8 @@ dir_title = function(f, preview = '1') { c( sprintf('[%s/](%s)', c('.', d), c(rev(strrep('../', seq_along(d))), './')), if (file_exists(f)) b, if (is_lite_ext(file = f)) c( - btn('.save'), if (preview != '2') - btn('.run', sprintf('%s?preview=2', b), 'title="Run"') + btn('.save'), if (preview != (n <- if (is_book_index(f)) '3' else '2')) + btn('.run', sprintf('%s?preview=%s', b, n), 'title="Run"') ) else if (is_html_file(f)) btn('.run', b, 'title="View the HTML page"'), if (file_exists(f) && is_text_file(file = f)) btn('.open') @@ -465,9 +476,12 @@ btn = function(t, u = '#', a = character()) { sprintf(' [%s](<%s>)%s ', t, u, a) } -.icons = c(.open = '✎', .run = '►', .save = '↯') +.icons = c( + .open = '✎', .run = '►', .run1 = '⏭︎', .save = '↯' +) .titles = c( .open = 'Open in editor', .run = 'Run in memory and preview', + .run1 = 'Run the whole book in memory and preview', .save = 'Render to disk in a fresh R session' ) diff --git a/inst/resources/listing.css b/inst/resources/listing.css index f23ff77c..0a1df9ee 100644 --- a/inst/resources/listing.css +++ b/inst/resources/listing.css @@ -22,7 +22,7 @@ body:has(.boxes, .listing) { em:not(:hover), pre code:not(:hover) { opacity: .7; } - .run { + .run, .run1 { float: right; } pre { diff --git a/inst/resources/server.css b/inst/resources/server.css index 67057558..dd87b94f 100644 --- a/inst/resources/server.css +++ b/inst/resources/server.css @@ -172,6 +172,9 @@ body.waiting { @media (prefers-color-scheme: dark) { .litedown-dialog { color: initial; } } +.pagesjs [data-source] > .buttons { + display: none; +} @media screen { .pagesjs > .nav-path { display: block; diff --git a/inst/resources/server.js b/inst/resources/server.js index b23ec901..fcc99241 100644 --- a/inst/resources/server.js +++ b/inst/resources/server.js @@ -220,7 +220,7 @@ const u = el.dataset.source; if (!u || $('.buttons', el)) return; const u2 = Array((u.match(/\/+/g) || []).length).fill('../').join(''), - a2 = i === 0 ? '' : `►`; + a2 = `►`; el.insertAdjacentHTML('afterbegin', `${a2}✎`); }); const nav = $('.nav-path'); diff --git a/tests/test-cran/test-preview.R b/tests/test-cran/test-preview.R index b08cc8de..ad020a3e 100644 --- a/tests/test-cran/test-preview.R +++ b/tests/test-cran/test-preview.R @@ -94,6 +94,31 @@ assert('file_resp() sends raw, verbatim, or rendered responses by preview level' (has_match(p2, ' Date: Thu, 19 Nov 2026 18:12:44 -0500 Subject: [PATCH 2/4] Initialize .env$refs as a list to work in R < 4.0 `NULL[[b]] = ` is an error in R < 4.0 ("more elements supplied than there are to replace"), so the first book chapter that stored its cross-reference db into the NULL `.env$refs` failed. Covered by the book index preview test. Co-Authored-By: Claude Code --- R/package.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/package.R b/R/package.R index 4826b385..993f408e 100644 --- a/R/package.R +++ b/R/package.R @@ -16,6 +16,9 @@ # an internal environment to store some intermediate objects .env = new_env() +# cross-reference dbs of book chapters (must be a list: NULL[[i]] = +# is an error in R < 4.0) +.env$refs = list() #' The `fuse()` environment #' From 791156ec63955cb40434c421e9036fcfacdb8fef Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Thu, 19 Nov 2026 19:30:09 -0600 Subject: [PATCH 3/4] ensure right envir for fuse_book()/fuse_site() --- R/site.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/site.R b/R/site.R index 762cb713..37659245 100644 --- a/R/site.R +++ b/R/site.R @@ -128,7 +128,8 @@ build_proj = function(x, root) { if (!is.null(fl)) options(litedown.html.meta = list( header_includes = fl$meta, js = set_names(fl$js, p) )) - get(paste0('fuse_', type), asNamespace('litedown'))(x) + f = asNamespace('litedown')[[paste0('fuse_', type)]] + f(x, envir = globalenv()) }, list(x, type, fl, lib_priority + 10), fail = paste0('Failed to run litedown::fuse_', type, '() on ', x)) } From 735a8bcda0b31c139accb98b9de67e60eaf461e5 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Thu, 19 Nov 2026 19:33:22 -0600 Subject: [PATCH 4/4] install dev version of xfun --- .github/workflows/R-CMD-check.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index b68f82e7..ee64da75 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -64,6 +64,7 @@ jobs: extra-packages: . ${{ env.USE_R_CODECOV == 'true' && ' covr pkgbuild' || '' }} # install dependencies for cairo_pdf device brew-packages: xquartz + r-universe: yihui cran - uses: yihui/actions/check-r-package@HEAD