r-lib / downlit

Syntax Highlighting and Automatic Linking
https://downlit.r-lib.org
Other
90 stars 22 forks source link

R6 class of local package does not get highlighted (inline) #140

Open pat-s opened 2 years ago

pat-s commented 2 years ago

The following works

`<pkg>::<class>$new()`

The following do not

`<class>`
`<class>$new()`

I've played around a bit with downlit::highlight() and found that

downlit::highlight("<class>$new()")

does not include the href part because packages in

https://github.com/r-lib/downlit/blob/main/R/highlight.R#L58

is empty. I've added the following to a fork

  if (is.null(packages)) {
    packages <- loadedNamespaces()
  }

which makes the href part getting added to downlit::highlight("<class>$new()") when executed directly. However, I still do not get highlighting when building a pkgdown site 🤔 (new session, fresh start with modified {downlit}).

Is there something else I am overlooking which is responsible for highlighting of local packages in {pkgdown}?

hadley commented 2 years ago

Can you please provide a reprex using a package that's on CRAN?

jemus42 commented 2 years ago

Reprex using mlr3 from CRAN, which I suspect is one of the reasons Patrick encountered this behavior:

library(downlit)
library(mlr3)

# Object in question: mlr3::Task

downlit::highlight("mlr3::Task$new()")
#> [1] "<span><span class='nf'>mlr3</span><span class='nf'>::</span><span class='nv'><a href='https://mlr3.mlr-org.com/reference/Task.html'>Task</a></span><span class='o'>$</span><span class='nf'>new</span><span class='o'>(</span><span class='o'>)</span></span>"

downlit::highlight("mlr3::Task")
#> [1] "<span><span class='nf'>mlr3</span><span class='nf'>::</span><span class='nv'><a href='https://mlr3.mlr-org.com/reference/Task.html'>Task</a></span></span>"

downlit::highlight("Task$new()")
#> [1] "<span><span class='nv'>Task</span><span class='o'>$</span><span class='nf'>new</span><span class='o'>(</span><span class='o'>)</span></span>"

downlit::highlight("Task")
#> [1] "<span><span class='nv'>Task</span></span>"

Created on 2022-09-14 with reprex v2.0.2