Skip to content

Commit

Permalink
Merge pull request #1883 from rossellhayes/fix/md-as-character
Browse files Browse the repository at this point in the history
Support factors in `fmt_markdown()` with HTML output
  • Loading branch information
rich-iannone authored Sep 18, 2024
2 parents 72821a7 + 422f24c commit b4fa1b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
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>"
)
})

0 comments on commit b4fa1b3

Please sign in to comment.