r-lib / downlit

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

Multline strings formatted incorrectly? #139

Closed hadley closed 2 years ago

hadley commented 2 years ago
con |> 
  dbGetQuery("
    SELECT carat, cut, clarity, color, price 
    FROM diamonds 
    WHERE price > 15000
  ") |> 
  as_tibble()

looks like

Screen Shot 2022-05-26 at 10 28 00

Because, I think, the multi-line string is put in a single span.

cderv commented 2 years ago

We also have something related in Quarto which uses downlit: https://github.com/quarto-dev/quarto-cli/discussions/1165

Chunk to highlight

strwrap(
  "Volcano plot - Response Size and Significance of Differential
  Expression among all miRNA at Baseline", width = 80
)

We get a single <span> for the whole multiline

chunk_content <-
  c(
    "strwrap(\n  \"Volcano plot - Response Size and Significance of Differential\n  Expression among all miRNA at Baseline\", width = 80\n)"
  )

res <- downlit::highlight(chunk_content, downlit::classes_pandoc())

xfun::raw_string(res)
#> <span class='fu'><a href='https://rdrr.io/r/base/strwrap.html'>strwrap</a></span><span class='op'>(</span>
#>   <span class='st'>"Volcano plot - Response Size and Significance of Differential
#>   Expression among all miRNA at Baseline"</span>, width <span class='op'>=</span> <span class='fl'>80</span>
#> <span class='op'>)</span>

Created on 2022-06-17 by the reprex package (v2.0.1)

Output is like image

Related to https://github.com/r-lib/downlit/issues/133 as Pandoc correctly adds a <span> per line, but it is lost in the translation when applying downlit_html_path()

hadley commented 2 years ago

Yeah, I'm pretty sure the problem is multi-line strings.

hadley commented 2 years ago

Minimal reprex with rmd:

---
output: 
  html_document:
    highlight_downlit: true
---

```{r, results = FALSE}
paste("Line 1
  Line 2
  Line 3"
)

which generates:

```html
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste</a></span><span class="op">(</span><span class="st">"Line 1
  Line 2
  Line 3"</span>
<span class="op">)</span></code></pre></div>
hadley commented 2 years ago

And the messed appearance arises because pre > code.sourceCode > span is given display: inline-block;.

hadley commented 2 years ago

Minimal reprex just using downlit:

cat(downlit::highlight('paste("Line 1
  Line 2
  Line 3"
)'))
#> <span class='nf'><a href='https://rdrr.io/r/base/paste.html'>paste</a></span><span class='o'>(</span><span class='s'>"Line 1
#>   Line 2
#>   Line 3"</span>
#> <span class='o'>)</span>

Created on 2022-06-21 by the reprex package (v2.0.1)