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

Unescaped underscores in code rendering as italics in xaringan #32

Open
machow opened this issue Nov 20, 2020 · 3 comments
Open

Unescaped underscores in code rendering as italics in xaringan #32

machow opened this issue Nov 20, 2020 · 3 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed knit formats issue flair being weird with certain doc types

Comments

@machow
Copy link

machow commented Nov 20, 2020

I think this may be more likely to pop up in python, since you can use _ as a variable name 😬. I'm using knitr v1.30.

Rmarkdown code:

---
title: "code examples"
output:
  xaringan::moon_reader:
    lib_dir: libs
    nature:
      ratio: '16:9'
      countIncrementalSlides: false

---


```{python test}
a = 1
_ = 2
_ = 3
```

```{r}
library(flair)
decorate("test") %>% flair("a")
```

Output (note italics in very bottom 2 lines):

image

Looking at the source, it seems that html tags are escaped (for remarkjs?), but underscores are left as is.

@machow
Copy link
Author

machow commented Nov 20, 2020

It looks like this traces back to htmltools::htmlEscape (used by xaringan).

> htmltools::htmlEscape("< _")
[1] "&lt; _"

Underscores don't need to be escaped in html, but I'm guessing remarkjs does markdown rendering early on?

See these issues:

@machow
Copy link
Author

machow commented Nov 20, 2020

I just tossed in a quick gsub. Not pretty, but seems to work!

Changed here:

flair/R/with_flair.R

Lines 96 to 99 in ab67c7d

} else if (doc_type == "xaringan::moon_reader") {
x <- paste0("<code class ='r hljs remark-code'>", x, "</code>")

Original

    x <- paste0("<code class ='r hljs remark-code'>", x, "</code>")

New

    x <- paste0("<code class ='r hljs remark-code'>",  gsub("([_*])", "\\\\\\1", x), "</code>")

@shannonpileggi
Copy link

@kbodwin kbodwin self-assigned this Jan 11, 2023
@kbodwin kbodwin added bug Something isn't working knit formats issue flair being weird with certain doc types help wanted Extra attention is needed labels Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed knit formats issue flair being weird with certain doc types
Projects
None yet
Development

No branches or pull requests

3 participants