Skip to content

Commit

Permalink
Fix syntax highlighting on windows (#1288)
Browse files Browse the repository at this point in the history
Fixes #1282
  • Loading branch information
hadley authored Apr 6, 2020
1 parent 1522b56 commit 6310038
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pkgdown (development version)

* Syntax highlighting works on Windows once more (#1282)

* Rendering empty `.md` file now returns empty string (#1285).

* `build_articles_index()` is now exported to rapidly rebuild the index (#1281)
Expand Down
3 changes: 2 additions & 1 deletion R/highlight.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
highlight_text <- function(text) {
stopifnot(is.character(text), length(text) == 1)

text <- gsub("\r", "", text)
expr <- tryCatch(
parse(text = text, keep.source = TRUE),
error = function(e) NULL
)

# Failed to parse, or yielded empty expression
if (length(expr) == 0) {
return(text)
return(escape_html(text))
}

packages <- extract_package_attach(expr)
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-highlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,18 @@ test_that("can link to implicit base topics", {
"<span class='fu'><a href='https://rdrr.io/r/stats/median.html'>median</a></span>()"
)
})

test_that("can parse code with carriage returns", {
scoped_package_context("test")

expect_equal(
highlight_text("1\r\n2"),
"<span class='fl'>1</span>\n<span class='fl'>2</span>"
)
})

test_that("unparsed code is still escaped", {
scoped_package_context("test")

expect_equal(highlight_text("<"), "&lt;")
})

0 comments on commit 6310038

Please sign in to comment.