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

Conversation

jacobbien
Copy link
Contributor

This is intended to address #2147.

Recall the code example there:

very_long_url <- paste(c("a", rep("very", 20), "long-url"), collapse = "-")
tbl <- data.frame(
  flavor = sprintf("[Mint chip](%s)", very_long_url),
  description = "Of all the many flavors of ice cream, this is my favorite."
  )
knitr::kable(tbl, format = "pipe")

It now generates a different output:

|flavor    |description                                                |
|:---------|:----------------------------------------------------------|
|[Mint chip](a-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-very-long-url)|Of all the many flavors of ice cream, this is my favorite. |

And here's what the table in the html file now looks like:

Screen Shot 2022-07-08 at 12 30 14 AM

Recall that before it looked like

Screen Shot 2022-07-07 at 11 04 19 PM

@CLAassistant
Copy link

CLAassistant commented Jul 8, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Owner

@yihui yihui left a 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 Show resolved Hide resolved
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)
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.

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

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

Perfect. Thanks!

R/utils.R Outdated Show resolved Hide resolved
@yihui yihui linked an issue Jul 12, 2022 that may be closed by this pull request
3 tasks
@yihui yihui merged commit 6d6b930 into yihui:master Jul 12, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cell width of kable when hyperlinks are included
3 participants