downlit icon indicating copy to clipboard operation
downlit copied to clipboard

`downlit_html_*` should support Pandoc's document with numbered line

Open cderv opened this issue 2 years ago • 2 comments

Pandoc allows to add number lined in code chunk by adding the class .number-lines to code chunk. This can be done in R Markdown using class.source knitr option, but is also possible in Quarto with code-line-numbers = true options. In Quarto this is also the default for revealjs format.

Trying to activate downlit for those (using highlight_downlit: true in Rmd or code-link: true in Quarto) will not apply the highlighting or the code linking.

Quarto example
---
title: demo line number
format: 
  html: default
code-link: true
---

# Line numbered

```{r}
#| code-line-numbers: true
library(glue)
glue("this is {i}", i = "me")
```


# Not line numbered

```{r}
library(glue)
glue("this is {i}", i = "me")
```
Rmd example
---
title: Rmd test
output: 
  html_document: 
    highlight_downlit: true
---


# Line numbered

```{r, class.source = ".number-lines"}
library(glue)
glue("this is {i}", i = "me")
```

# Not line numbered

```{r}
library(glue)
glue("this is {i}", i = "me")
```

Both are using downlit::downlit_html_path() on the result of Pandoc conversion.

This is due to two things mainly

  • <pre> will have more classes like <pre class="sourceCode numberSource r number-lines"> and detection does not account for that https://github.com/r-lib/downlit/blob/8ee71d8cba7e77e05cbf01de8f1ecff1536b3741/R/downlit-html.R#L47-L48

  • Number per line is done by Pandoc highlighting through the addition of spans like <span id="cb1-1"><a href="#cb1-1"></a> HTML from example above:

<div class="sourceCode" id="cb1">
    <pre class="sourceCode numberSource r number-lines">
        <code class="sourceCode r">
            <span id="cb1-1"><a href="#cb1-1"></a><span class="fu">library</span>(glue)</span>
            <span id="cb1-2"><a href="#cb1-2"></a><span class="fu">glue</span>(<span class="st">"this is {i}"</span>, <span class="at">i =</span> <span class="st">"me"</span>)</span>
        </code>
    </pre>
</div>

downlit is replacing all the the <pre> and it would require here to preserve the span at each line.

Opening this issue to document this limitation and to discuss how to handle that. Happy to help on this as this is needed for Quarto code-link to work in presentation where line numbers is the default currently.

cderv avatar Mar 18 '22 20:03 cderv

Also related to #13

hadley avatar Mar 19 '22 17:03 hadley

Related to https://github.com/r-lib/downlit/issues/139 in the sense that Pandoc a span per line in the input, and downlit does not keep this line wrapping.

cderv avatar Jun 17 '22 11:06 cderv