Skip to content

Commit 439b71b

Browse files
committed
Some review and improvement
1 parent e096c57 commit 439b71b

File tree

1 file changed

+70
-30
lines changed

1 file changed

+70
-30
lines changed

vignettes/markdown-html-tables.qmd

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,90 @@ The main challenge when working with Markdown in HTML tables is that Quarto won'
1919

2020
See Quarto documentation about HTML tables parsing: <https://quarto.org/docs/authoring/tables.html#html-tables>.
2121

22+
## When to Use These Functions
23+
24+
Use the `tbl_qmd_*()` functions when:
25+
26+
- **Working with table packages that don't have built-in Quarto support** (like kableExtra)
27+
- **You need raw HTML control** over some Markdown content that needs to be processed by Quarto
28+
- **Migrating existing table code** to support Quarto's HTML table processing
29+
30+
**Don't use these functions** when:
31+
32+
- Your table package already has built-in Quarto support (like gt's `fmt_markdown()` or tinytable's `format_tt(quarto = TRUE)`)
33+
- You're working outside of Quarto documents (the functions will have no effect)
34+
- Simple formatting is easy enough to write in raw HTML and does not require Quarto Markdown processing.
35+
2236
## Basic Usage
2337

24-
The table helper functions create HTML elements (`<span>` or `<div>`) with the appropriate `data-qmd` or `qmd-base64` attributes. There are two main types of functions:
38+
The table helper functions create HTML elements (`<span>` or `<div>`) with the appropriate `data-qmd` or `data-qmd-base64` attributes. There are two main types of functions:
2539

2640
1. Functions for creating `<span>` elements:
27-
- main function is `tbl_qmd_span()`, defaulting to base64 encoding,
28-
- Two others are explicit versions: `tbl_qmd_span_base64()` and `tbl_qmd_span_raw()`
41+
- main function is `tbl_qmd_span()`, defaulting to base64 encoding
42+
- Two others are explicit versions: `tbl_qmd_span_base64()` and `tbl_qmd_span_raw()`
2943
2. Functions for creating `<div>` elements:
30-
- main function is `tbl_qmd_div()`, defaulting to base64 encoding,
31-
- Two others are explicit versions: `tbl_qmd_div_base64()` and `tbl_qmd_div_raw()`
44+
- main function is `tbl_qmd_div()`, defaulting to base64 encoding
45+
- Two others are explicit versions: `tbl_qmd_div_base64()` and `tbl_qmd_div_raw()`
3246

3347
Base64 encoding is useful when your Markdown content contains special characters or HTML tags, and this is used by default to avoid any escaping problems using this feature.
3448

35-
### Example with a Basic HTML Table
49+
### Before and After Comparison
3650

37-
Here's a simple example of creating an HTML table with Markdown content:
51+
Here's what happens when you don't use the helper functions:
3852

3953
```{r}
4054
library(quarto)
4155
42-
# Create a simple data frame
43-
data <- data.frame(
44-
Column1 = c("Row 1", "Row 2", "Row 3"),
45-
Column2 = c("Value 1", "Value 2", "Value 3")
56+
# Without helper functions - Markdown won't be processed
57+
basic_data <- data.frame(
58+
Item = c("Item 1", "Item 2", "Item 3"),
59+
Description = c("**Bold text**", "*Italic text*", "`Code text`")
60+
)
61+
62+
knitr::kable(
63+
basic_data,
64+
format = "html",
65+
escape = FALSE,
66+
caption = "Without Quarto processing"
67+
)
68+
```
69+
70+
And here's the same table with proper Quarto processing:
71+
72+
```{r}
73+
# With helper functions - Markdown will be processed
74+
enhanced_data <- data.frame(
75+
Item = c("Item 1", "Item 2", "Item 3"),
76+
Description = c(
77+
tbl_qmd_span("**Bold text**"),
78+
tbl_qmd_span("*Italic text*"),
79+
tbl_qmd_span("`Code text`")
80+
)
4681
)
4782
48-
# Function to add Markdown formatting to table cells
49-
add_markdown <- function(data) {
50-
data$Column1 <- sapply(data$Column1, function(x) {
51-
tbl_qmd_span(paste0("**", x, "**"))
52-
})
53-
data$Column2 <- sapply(data$Column2, function(x) {
54-
tbl_qmd_span(paste0("*", x, "*"))
55-
})
56-
return(data)
57-
}
58-
59-
# Apply Markdown formatting
60-
data_with_md <- add_markdown(data)
61-
62-
# Display the data frame as an HTML table
63-
knitr::kable(data_with_md, format = "html", escape = FALSE)
83+
knitr::kable(
84+
enhanced_data,
85+
format = "html",
86+
escape = FALSE,
87+
caption = "With Quarto processing"
88+
)
6489
```
6590

91+
**Key point**: Always remember to set `escape = FALSE` when using these functions with `knitr::kable()` or similar functions.
92+
93+
### What the HTML Output Looks Like
94+
95+
When you use `tbl_qmd_span("**Bold text**")`, it creates HTML like this:
96+
97+
```{r}
98+
#| echo: false
99+
#| output: asis
100+
xfun::fenced_block(attrs = ".html", tbl_qmd_span("**Bold text**")) |>
101+
cat(sep = "\n")
102+
```
103+
104+
Quarto sees the `data-qmd-base64` attribute and processes the base64-decoded content as Markdown.
105+
66106
### Using with knitr::kable()
67107

68108
The `knitr::kable()` function is a common way to create tables in R Markdown and Quarto. By setting `escape = FALSE`, we can include HTML in the table cells:
@@ -89,7 +129,7 @@ knitr::kable(tbl, format = "html", escape = FALSE)
89129

90130
### Display Text
91131

92-
Some feature are Quarto features only. If your table can be used in other context than Quarto, you might want to use the `display` argument to provide a text that will be shown in the table instead of the Markdown content, as it won't be processed outside of Quarto documents.
132+
Some features are Quarto features only. If your table can be used in other context than Quarto, you might want to use the `display` argument to provide a text that will be shown in the table instead of the Markdown content, as it won't be processed outside of Quarto documents.
93133

94134
For example, you might want to show a placeholder when using video shortcodes in a table, as the video player won't be rendered outside of Quarto:
95135

@@ -133,12 +173,12 @@ Using `data-qmd` or `data-qmd-base64` attributes is a Quarto-specific feature an
133173

134174
## Table package integration
135175

136-
To summarize the question of Markdown processing in HTML tables within Quarto,
176+
To summarize the question of Markdown processing in HTML tables within Quarto, currently, there are two ways this could be supported:
137177

138178
- This is possible thanks to Quarto HTML Table parsing
139179
- This is done using some `<span>` or `<div>` elements with `data-qmd` or `data-qmd-base64` attributes
140180

141-
Any R package for producing tables and providing raw HTML as output needs to support this Quarto feature to allow Markdown content in HTML tables.
181+
Any R package for producing tables and providing raw HTML as output needs to support this Quarto feature to allow Markdown content in HTML tables.In this case, they will create themselves the `<span>` or `<div>` elements with the `data-qmd` or `data-qmd-base64` attributes.
142182

143183
Currently, there is two ways this could be supported:
144184

0 commit comments

Comments
 (0)