yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

Make kable() compatible with Quarto when outputing HTML #2289

Open cderv opened 9 months ago

cderv commented 9 months ago

Take this example

---
title: "Citations in tables"
format: html
keep-md: true

references:
- type: article-journal
  id: Lovelace1842
  author:
  - family: Lovelace
    given: Augusta Ada
  issued:
    date-parts:
    - - 1842
  title: >-
    Sketch of the analytical engine invented by Charles Babbage, by LF Menabrea, 
    officer of the military engineers, with notes upon the memoir by the translator
  title-short: Molecular structure of nucleic acids
  container-title: Taylor’s Scientific Memoirs
  volume: 3
  page: 666-731
  language: en-GB
---

## `gt::gt()`

```{r}
tibble::tribble(
  ~Thing, ~Citation,
  1234, "@Lovelace1842"
) |>
  knitr::kable(format = "html")

It produces this HTML table

````html
<table>
 <thead>
  <tr>
   <th style="text-align:right;"> Thing </th>
   <th style="text-align:left;"> Citation </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:right;"> 1234 </td>
   <td style="text-align:left;"> @Lovelace1842 </td>
  </tr>
</tbody>
</table>

This part <td style="text-align:left;"> @Lovelace1842 </td> will not be parsed as Markdown and so it will not be processed as citation.

However, Quarto offers a mechanism for this described in https://quarto.org/docs/authoring/tables.html#html-tables

In addition, Quarto supports the specification of embedded Markdown content in tables. This is done by providing a data attribute qmd or qmd-base64 in an embedded span or div node. These nodes can appear anywhere that such content is allowed: table headers, footers, cells, captions, etc.

To this would work

<table>
 <thead>
  <tr>
   <th style="text-align:right;"> Thing </th>
   <th style="text-align:left;"> Citation </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:right;"> 1234 </td>
   <td style="text-align:left;"> <span data-qmd="@Lovelace1842"></span> </td>
  </tr>
</tbody>
</table>

gt does it with this syntax

library(gt)
tibble::tribble(
  ~Thing, ~Citation,
  1234, "@Lovelace1842"
) |>
  gt() |> fmt_markdown("Citation")

We should probably have a way to escape for Quarto some column or values.

This could also be in kableExtra directly, but I think kable() should support it directly.

More context in