From 41816826b7cb25412b7c237ecafa5bc0769f008d Mon Sep 17 00:00:00 2001 From: Leonidas Zhak <70497898+LeonidasZhak@users.noreply.github.com> Date: Sat, 24 Oct 2026 09:27:15 +0800 Subject: [PATCH] Fix duplicated Quarto markdown list text --- NEWS.md | 2 ++ R/quarto.R | 25 ++++++++++++++++++++++--- tests/testthat/test-fmt_markdown.R | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 635380c59..683a5b8b8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -20,6 +20,8 @@ ## Minor improvements and bug fixes +* Fixed duplicate text in Quarto output when `fmt_markdown()` cells contain a list without preceding text (#2140). + * Fixed LaTeX and RTF output for unformatted `Date` columns, which now show calendar dates instead of day counts since the Unix epoch (#2177). (@LeonidasZhak, #2180) * Rows in LaTeX output tables no longer overlap when cell text contains line breaks (#2178). (#2179) diff --git a/R/quarto.R b/R/quarto.R index 4980a9338..bc649de16 100644 --- a/R/quarto.R +++ b/R/quarto.R @@ -60,12 +60,31 @@ process_md_quarto <- function(text, md_engine_fn) { ) # Tweak start and end of non_na_text - non_na_text <- paste0("") + # Use block containers when Markdown contains block-level HTML; Quarto + # otherwise parses lists as inline content and duplicates their text. + block_tags <- + paste0( + "address|article|aside|blockquote|div|dl|fieldset|figure|footer|", + "h[1-6]|header|hr|main|nav|ol|p|pre|section|table|ul" + ) + + block_content <- + grepl( + paste0("<(", block_tags, ")([[:space:]>])"), + non_na_text_processed + ) + + container_tag <- ifelse(block_content, "div", "span") + + non_na_text <- + paste0( + "<", container_tag, " data-qmd-base64=\"", non_na_text, "\">" + ) non_na_text <- paste0( - non_na_text, "", - non_na_text_processed, "" + non_na_text, "<", container_tag, " class='gt_from_md'>", + non_na_text_processed, "", container_tag, ">", container_tag, ">" ) text[nzchar(text, keepNA = FALSE)] <- non_na_text diff --git a/tests/testthat/test-fmt_markdown.R b/tests/testthat/test-fmt_markdown.R index a48c561c3..8a89e625d 100644 --- a/tests/testthat/test-fmt_markdown.R +++ b/tests/testthat/test-fmt_markdown.R @@ -94,6 +94,26 @@ There's a quick reference [here](https://commonmark.org/help/). ) }) +test_that("fmt_markdown() uses block containers when rendered by Quarto", { + withr::local_envvar(QUARTO_BIN_PATH = "quarto") + + tab <- + tibble::tibble( + text = c("- bullet point 1\n- bullet point 2", "*inline text*") + ) |> + gt() |> + fmt_markdown() + + output <- (tab |> render_formats_test(context = "html"))[["text"]] + + expect_match(output[[1]], "^