Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support factors in fmt_markdown() with HTML output #1883

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

* Improved footnote rendering in Quarto with `fmt_markdown()` (@olivroy, #1773)

* Fixed an issue where `md()` and `fmt_markdown()` would render factors as their numeric levels rather than their text labels (@rossellhayes, #1883).

# gt 0.11.0

## New features
Expand Down
3 changes: 2 additions & 1 deletion R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
#'
#' @export
md <- function(text) {

# Ensure input is text (e.g. for factors)
text <- as.character(text)
# Apply the `from_markdown` class
class(text) <- "from_markdown"
text
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-fmt_markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,23 @@ test_that("LaTeX formulas render correctly in HTML", {
# Take a snapshot of `gt_tbl`
expect_snapshot_html(gt_tbl)
})

test_that("fmt_markdown() works correctly with factors", {

text <- "This is Markdown *text*."

# Create a `gt_tbl` object with `gt()`
# and a tibble; format all columns with
# `fmt_markdown()`
tab <-
dplyr::tibble(column_1 = factor(text)) %>%
gt() %>%
fmt_markdown(columns = everything())

# Compare output of cell to the expected HTML output strings
expect_equal(
(tab %>%
render_formats_test(context = "html"))[["column_1"]][[1]],
"<span class='gt_from_md'>This is Markdown <em>text</em>.</span>"
)
})
Loading