Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ 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
r-universe: yihui cran

- uses: yihui/actions/check-r-package@HEAD

Expand Down
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: 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")),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]] = <vector>
# is an error in R < 4.0)
.env$refs = list()

#' The `fuse()` environment
#'
Expand Down
28 changes: 21 additions & 7 deletions R/preview.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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)) {
Expand All @@ -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())
Expand All @@ -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) {
Expand Down Expand Up @@ -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')
Expand All @@ -465,9 +476,12 @@ btn = function(t, u = '#', a = character()) {
sprintf(' [%s](<%s>)%s ', t, u, a)
}

.icons = c(.open = '&#9998;', .run = '&#9658;', .save = '&#8623;')
.icons = c(
.open = '&#9998;', .run = '&#9658;', .run1 = '&#9197;&#65038;', .save = '&#8623;'
)
.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'
)

Expand Down
3 changes: 2 additions & 1 deletion R/site.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion inst/resources/listing.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ body:has(.boxes, .listing) {
em:not(:hover), pre code:not(:hover) {
opacity: .7;
}
.run {
.run, .run1 {
float: right;
}
pre {
Expand Down
3 changes: 3 additions & 0 deletions inst/resources/server.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion inst/resources/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? '' : `<a href="${u2}${u}?preview=2" class="btn-lite run" title="Preview this chapter only (${u})" aria-label="Preview ${u}">►</a>`;
a2 = `<a href="${u2}${u}?preview=2" class="btn-lite run" title="Preview this chapter only (${u})" aria-label="Preview ${u}">►</a>`;
el.insertAdjacentHTML('afterbegin', `<span class="buttons">${a2}<a href="?path=${u}" class="btn-lite open pencil" title="Open ${u} in editor" aria-label="Open ${u} in editor">✎</a></span>`);
});
const nav = $('.nav-path');
Expand Down
25 changes: 25 additions & 0 deletions tests/test-cran/test-preview.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ assert('file_resp() sends raw, verbatim, or rendered responses by preview level'
(has_match(p2, '<h1'))
})

assert('file_resp() previews a book index alone (2) or as the whole book (3)', {
d = tempfile(); dir.create(d)
on.exit(unlink(d, recursive = TRUE), add = TRUE)
writeLines(c('book:', ' new_session: false'), file.path(d, '_litedown.yml'))
writeLines('# Index', i <- file.path(d, 'index.md'))
writeLines('# Other', file.path(d, 'a.md'))
in_dir(d, {
p2 = file_resp('index.md', '2')$payload
p3 = file_resp('index.md', '3')$payload
})
(has_match(p2, 'Index'))
(has_match(p2, !'Other'))
(has_match(p3, 'Other'))
# the listing offers both buttons for the book index, one for other files
l = in_dir(d, dir_page('.'))
(has_match(l, 'index.md\\?preview=3'))
(has_match(l, 'index.md\\?preview=2'))
(has_match(l, I('Run the whole book')))
(has_match(l, !'a.md\\?preview=3'))
# the toolbar Run button runs the whole book for the index, a file otherwise
t = in_dir(d, c(dir_title('index.md', '2'), dir_title('a.md', '1')))
(has_match(t, 'index.md\\?preview=3'))
(has_match(t, 'a.md\\?preview=2'))
})

assert('file_page() injects navigation links at the top of a rendered page', {
d = tempfile(); dir.create(d)
on.exit(unlink(d, recursive = TRUE), add = TRUE)
Expand Down
Loading