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

Fix how kable_mark() measures column width when there are hyperlinks #2148

Merged
merged 6 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,13 @@ kable_mark = function(x, sep.row = c('=', '=', '='), sep.col = ' ', padding = 0
if (sep.col == '|') for (j in seq_len(ncol(x))) {
x[, j] = gsub('\\|', '|', x[, j])
}
l = if (prod(dim(x)) > 0) apply(x, 2, function(z) max(nchar(z, type = 'width'), na.rm = TRUE))
l = if (prod(dim(x)) > 0) apply(x, 2, function(z) max(nchar(remove_urls(z), type = 'width'), na.rm = TRUE))
cn = colnames(x)
if (length(cn) > 0) {
cn[is.na(cn)] = "NA"
if (sep.col == '|') cn = gsub('\\|', '|', cn)
if (grepl('^\\s*$', cn[1L])) cn[1L] = rownames.name # no empty cells for reST
l = pmax(if (length(l) == 0) 0 else l, nchar(cn, type = 'width'))
l = pmax(if (length(l) == 0) 0 else l, nchar(remove_urls(cn), type = 'width'))
}
align = attr(x, 'align')
padding = padding * if (length(align) == 0) 2 else {
Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1059,3 +1059,9 @@ make_unique = function(x) {
#' browseURL('logo.html') # you can check its HTML source
#' }
image_uri = function(f) xfun::base64_uri(f)

# Change all "[label](url)" to "label"
remove_urls <- function(x) {
jacobbien marked this conversation as resolved.
Show resolved Hide resolved
# regex adapted from https://dev.to/mattkenefick/regex-convert-markdown-links-to-html-anchors-f7j
gsub("\\[([^\\]]+)\\]\\(([^\\)]+)\\)", "\\1", x, perl = TRUE)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also use zero-width positive/negative lookbehind/lookahead assertions (see ?regex if you are not familiar with them) to avoid treating `[text](url)` as a URL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below are two examples of using positive lookbehind/lookahead for matching and replacing. Perhaps I'm missing something, but it seems like the fact that it excludes the brackets and parentheses from the match actually makes it harder to remove them. Both examples have the same regex, which is to look for [x]( where x is 0 or more non-] characters.

  1. Matching
stringr::str_match_all("[label1](url1) and [label2](url2).","(?<=\\[)[^\\]]*(?=\\]\\()")
[[1]]
     [,1]    
[1,] "label1"
[2,] "label2"
  1. Replacing
stringr::str_replace_all("[label1](url1) and [label2](url2).","(?<=\\[)[^\\]]*(?=\\]\\()", "MATCHED")
"[MATCHED](url1) and [MATCHED](url2)."

But I'm new to zero-width lookbehind/lookahead, so perhaps I'm missing something. Thanks so much for writing knitr, by the way -- it's such a valuable tool!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I guess I was not clear last time. I meant we should exclude the case of []() surrounded by backticks (i.e., (?<!`) and (?!`)).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see! Thanks for explaining that. I have updated the PR accordingly.

}
6 changes: 6 additions & 0 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,9 @@ assert('comment_out() add prefix and newlines if asked', {
(comment_out("a", prefix = "$") %==% "$ a\n")
(comment_out("a", prefix = NULL) %==% "a\n")
})

assert('remove_urls() removes the link', {
(remove_urls(c('[a](b)', '[a b](c)')) %==% c('a', 'a b'))
(remove_urls('a [b](c) d.') %==% 'a b d.')
(remove_urls('a [b](c) d [e f+g](h) i.') %==% 'a b d e f+g i.')
})