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

Add highlight.source to select highlighted line for remarkjs #309

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ moon_reader = function(
source = function(x, options) {
hook = hooks[['source']]
res = hook(x, options)
highlight_code(res)
highlight_code(res, options)
},
output = function(x, options) {
hook = hooks[['output']]
Expand Down
14 changes: 13 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ summon_remark = function(version = 'latest', to = 'libs/') {

# replace {{code}} with *code so that this line can be highlighted in remark.js;
# this also works with multiple lines
highlight_code = function(x) {
highlight_code = function(x, options) {
x = paste0('\n', x) # prepend \n and remove it later
r = '(\n)([ \t]*)\\{\\{(.+?)\\}\\}(?=(\n|$))'
m = gregexpr(r, x, perl = TRUE)
Expand All @@ -105,6 +105,18 @@ highlight_code = function(x) {
# catch `#<<` at end of the line but ignores lines that start with `*` since
# they came from above
x = gsub('^\\s?([^*].+?)\\s*#<<\\s*$', '*\\1', split_lines(x))
# allow to highlight line via option (useful for non R engine)
if (!is.null(i <- options$highlight.source)) {
if (!is.numeric(i)) {
w = sprintf("%s must be numeric. options will be ignored.", sQuote('highlight.source'))
warning(w, call. = FALSE)
} else {
s = grep('^`{3}', x) # find start of code output
i = s[1] + i
# ignore lines already marked as highlighted with `x`
x[i] = gsub('^\\s?([^*].+?)', '*\\1', x[i])
}
}
paste(x, collapse = '\n')
}

Expand Down