-
Notifications
You must be signed in to change notification settings - Fork 80
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
Improve html preview #470
Improve html preview #470
Conversation
The reason we have had syntax highlighting in the html preview is because of some styling added at the end of the github css.
Positron doesn't support dark vs. light mode detection from R yet (posit-dev/positron#2986), but reprex will just use light mode unconditionally for now. Which is still better than the current fugliness.
"--highlight-style", "pygments", | ||
"--template", template, | ||
"--variable", css, | ||
"--standalone", "--embed-resources", |
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.
Apparently this is just how modern Pandoc calls should look.
"--template", template, | ||
"--variable", css, | ||
"--standalone", "--embed-resources", | ||
"--highlight-style", path(res_dir, glue("starry-nights-{mode}.theme")), |
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.
Previously, the --highlight-style
call wasn't actually doing anything! But now this one does, because we've also eliminated the use of an html template.
"--css", path(res_dir, glue("github-{mode}.css")), | ||
"--syntax-definition", path(res_dir, "r.xml"), |
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.
No more template. I'm providing the github css in a way that works with the default html template. Plus a new custom syntax definition.
theme_info <- rstudioapi::getThemeInfo() | ||
theme_info$dark | ||
} else { | ||
FALSE |
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.
Positron ends up here unconditionally today (FALSE
), but in the fullness of time, it will provide theme info.
@hadley @cderv I'm happy to get feedback, but I mostly tagged you here for awareness. A minimal fix for the target issue here in reprex is relatively small. The main value of the PR might be identifying the changes we should try to make elsewhere: the KDE syntax highlighting definition file for R (affects all R highlighted by Pandoc) and how GitHub approaches highlighting for R. |
Thanks for sharing all this ! I think this will be useful to improve Github Preview format in R Markdown and Quarto. |
FWIW re GitHub updating to (presumably) use TreeLights for R:
So probably not in the cards anytime soon, but at least its on their radar now |
Thanks for the update @DavisVaughan. Given that forecast, maybe we should consider upgrading the textmate grammar for R? That might dovetail with making some upgrades to the KDE syntax definition (the Pandoc side of the house). The vendored grammar appears to live here: with the true grammar source being regarded as this: I see both @hadley and Jim Hester have contributed there at some point in the past, so it's possible it's already as good as it gets (?). But I get better highlighting of R in VS Code than on GitHub and I think that's also using a textmate grammar, so that gives me hope. Updated: I see that the true grammar source for some languages smells associated with VS Code or VS Code extensions. So that might be another low effort improvement, i.e. convince GitHub to consult a differen source for the R grammar. |
Fixes #468
Several changes to reprex's html preview:
rstudioapi::getThemeInfo()
, with light mode as the fall back.rstudioapi::getThemeInfo()
yet (Supportrstudioapi::getThemeInfo()
posit-dev/positron#2986), but it will. In the meantime, reprex html preview will be in light mode, but that's still an improvement on the status quo.reprex:::preview()
#468, we now set both thecolor
andbackground-color
for<body>
, plus a few other tweaks 11eca0d.rmarkdown::pandoc_convert()
(for md-to-html conversion) in a more canonical way, which allows us to eliminate the use of a template.#468 could be fixed just by making sure to specify both the
color
andbackground-color
for<body>
. But while I was working on this, I noticed various weird things about how R code is highlighted both on GitHub and by Pandoc! So I dug into that, which explains my forays into a custom theme and syntax definition. Hopefully this will lead to some improvements outside of reprex for syntax highlighting of R.GitHub side quest
I said that PrettyLights is the current highlighter for R. According to https://github.com/wooorm/starry-night:
The textmate grammar that Github + PrettyLights uses for R has some shortcomings, such as not properly tokenizing / classing function calls. This is related to a separate problem @DavisVaughan has noticed, which is that click-on-a-reference doesn't pull up the symbols pane (whereas click-on-a-definition does). Davis poked at this a bit and now he's got a ticket into the right system to see if his recent tree-sitter + R successes at GitHub could possibly lead to R becoming one of the languages handled with TreeLights. I think this would lead to better syntax highlighting of R, among other benefits.
KDE Syntax-Highlighting side quest
Source: https://pandoc.org/chunkedhtml-demo/13-syntax-highlighting.html
Indeed I was not satisfied with the built-in highlighting, so I did have a look at KDE's repository of syntax definitions. I think the R syntax definition used by Pandoc has some strange, suboptimal choices. Here it is in various places, in increasing order of importance:
I engaged @cderv in some discussions around this XML and he's in agreement that we could improve it. And he has made a successful merge request before.
I opened an issue to discuss with maintainers: https://invent.kde.org/frameworks/syntax-highlighting/-/issues/32
Here are a few easy tweaks in this PR: 5679f01