Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes truncation of long tokens by using utils::getParseText #136

Merged
merged 6 commits into from
Jun 9, 2022
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: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# downlit (development version)

* Very long strings or other tokens are no longer truncated
by `downlit::highlight()` (@dmurdoch, #128).

* Auto-linking works when used with double colon infix operator and `utils::help()`
(@IndrajeetPatil, #131).

Expand Down
19 changes: 18 additions & 1 deletion R/highlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ line_col <- function(x) {
data.frame(line, col)
}

# utils::getParseData will truncate very long strings or tokens;
# this function checks for that and uses the slow
# utils::getParseText function when necessary.

getFullParseData <- function(x) {
res <- utils::getParseData(x)

truncated <- res$terminal &
substr(res$text, 1, 1) == "[" &
nchar(res$text) > 5 # 5 is arbitrary, 2 would probably be enough

if (any(truncated))
res$text[truncated] <- utils::getParseText(res, res$id[truncated])

res
}

parse_data <- function(text) {
text <- standardise_text(text)
stopifnot(is.character(text), length(text) == 1)
Expand All @@ -130,7 +147,7 @@ parse_data <- function(text) {
return(NULL)
}

list(text = text, expr = expr, data = utils::getParseData(expr))
list(text = text, expr = expr, data = getFullParseData(expr))
}

# Highlighting ------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-highlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ test_that("ansi escapes are converted to html", {
expect_snapshot_output(highlight("# \033[31mhello\033[m"))
expect_snapshot_output(highlight("# \u2029[31mhello\u2029[m"))
})

test_that("can highlight vers long strings", {
val <- paste0(rep('very', 200), collapse = " ")
out <- downlit::highlight(sprintf("'%s'", val))
expect_equal(out, paste0("<span class='s'>'", val, "'</span>"))
})