-
-
Notifications
You must be signed in to change notification settings - Fork 878
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
Conversation
…le (so that it doesn't count the URL).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot!
R/utils.R
Outdated
# Change all "[label](url)" to "label" | ||
remove_urls <- function(x) { | ||
# regex adapted from https://dev.to/mattkenefick/regex-convert-markdown-links-to-html-anchors-f7j | ||
gsub("\\[([^\\]]+)\\]\\(([^\\)]+)\\)", "\\1", x, perl = TRUE) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
- Matching
stringr::str_match_all("[label1](url1) and [label2](url2).","(?<=\\[)[^\\]]*(?=\\]\\()")
[[1]]
[,1]
[1,] "label1"
[2,] "label2"
- 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!
There was a problem hiding this comment.
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 (?!`)
).
There was a problem hiding this comment.
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.
Co-authored-by: Yihui Xie <xie@yihui.name>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect. Thanks!
This is intended to address #2147.
Recall the code example there:
It now generates a different output:
And here's what the table in the html file now looks like:
Recall that before it looked like