From 307cdf6f4bd02b40bb8ef34af7ffaf36ab40f2a5 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 25 Mar 2025 12:18:14 +0100 Subject: [PATCH 1/3] knitr, markdown - Add new lines before creating cell output div Otherwise this is not compatible with Pandoc's Markdown expecting empty new lines before a div --- src/resources/rmd/hooks.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/resources/rmd/hooks.R b/src/resources/rmd/hooks.R index 530cbd11b35..2a74e57bddb 100644 --- a/src/resources/rmd/hooks.R +++ b/src/resources/rmd/hooks.R @@ -407,10 +407,11 @@ knitr_hooks <- function(format, resourceDir, handledLanguages) { if (identical(options[["results"]], "asis") && !needCell) { x } else { + # Newline first and after to ensure Pandoc Fenced Div is correctly parsed paste0( - options[["indent"]], "::: {", + options[["indent"]], "\n::: {", labelId(label), paste(classes, collapse = " ") ,forwardAttr, "}\n", x, "\n", cell.cap , - options[["indent"]], ":::" + options[["indent"]], ":::\n" ) } }) From 33a231f05ce4a890f560be2725e250b1c7064062 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 25 Mar 2025 12:19:14 +0100 Subject: [PATCH 2/3] tests - Add some simple intermediate markdown snapshot test cases This way we check that a basic markdown intermediate is as expected, and with no unprocessed cell div ::: --- .../output-cell-div.markdown.md.snapshot | 24 +++++++++++++++ .../output-cell-div.qmd | 12 ++++++++ .../output-cell-div.markdown.md.snapshot | 23 +++++++++++++++ .../output-cell-div.qmd | 11 +++++++ .../output-cell-div.markdown.md.snapshot | 25 ++++++++++++++++ .../output-cell-div.qmd | 11 +++++++ .../intermediate-output-markdown.test.ts | 29 +++++++++++++++++++ 7 files changed, 135 insertions(+) create mode 100644 tests/docs/julia/intermediate-markdown-output/output-cell-div.markdown.md.snapshot create mode 100644 tests/docs/julia/intermediate-markdown-output/output-cell-div.qmd create mode 100644 tests/docs/jupyter/intermediate-markdown-output/output-cell-div.markdown.md.snapshot create mode 100644 tests/docs/jupyter/intermediate-markdown-output/output-cell-div.qmd create mode 100644 tests/docs/knitr/intermediate-markdown-output/output-cell-div.markdown.md.snapshot create mode 100644 tests/docs/knitr/intermediate-markdown-output/output-cell-div.qmd create mode 100644 tests/smoke/engine/intermediate-output-markdown.test.ts diff --git a/tests/docs/julia/intermediate-markdown-output/output-cell-div.markdown.md.snapshot b/tests/docs/julia/intermediate-markdown-output/output-cell-div.markdown.md.snapshot new file mode 100644 index 00000000000..021ce163b21 --- /dev/null +++ b/tests/docs/julia/intermediate-markdown-output/output-cell-div.markdown.md.snapshot @@ -0,0 +1,24 @@ +--- +title: "Untitled" +format: markdown +keep-md: true +engine: julia +--- + +This does it: + +::: {.cell execution_count=1} +``` {.julia .cell-code} +1 + 1 +``` + +::: {.cell-output .cell-output-display execution_count=1} +``` +2 +``` +::: +::: + + +Other content + diff --git a/tests/docs/julia/intermediate-markdown-output/output-cell-div.qmd b/tests/docs/julia/intermediate-markdown-output/output-cell-div.qmd new file mode 100644 index 00000000000..26b2b224086 --- /dev/null +++ b/tests/docs/julia/intermediate-markdown-output/output-cell-div.qmd @@ -0,0 +1,12 @@ +--- +title: "Untitled" +format: markdown +keep-md: true +engine: julia +--- + +This does it: +```{julia} +1 + 1 +``` +Other content \ No newline at end of file diff --git a/tests/docs/jupyter/intermediate-markdown-output/output-cell-div.markdown.md.snapshot b/tests/docs/jupyter/intermediate-markdown-output/output-cell-div.markdown.md.snapshot new file mode 100644 index 00000000000..6eaa8f1ae08 --- /dev/null +++ b/tests/docs/jupyter/intermediate-markdown-output/output-cell-div.markdown.md.snapshot @@ -0,0 +1,23 @@ +--- +title: "Untitled" +format: markdown +keep-md: true +--- + +This does it: + +::: {.cell execution_count=1} +``` {.python .cell-code} +1 + 1 +``` + +::: {.cell-output .cell-output-display execution_count=1} +``` +2 +``` +::: +::: + + +Other content + diff --git a/tests/docs/jupyter/intermediate-markdown-output/output-cell-div.qmd b/tests/docs/jupyter/intermediate-markdown-output/output-cell-div.qmd new file mode 100644 index 00000000000..5dc2a07d971 --- /dev/null +++ b/tests/docs/jupyter/intermediate-markdown-output/output-cell-div.qmd @@ -0,0 +1,11 @@ +--- +title: "Untitled" +format: markdown +keep-md: true +--- + +This does it: +```{python} +1 + 1 +``` +Other content \ No newline at end of file diff --git a/tests/docs/knitr/intermediate-markdown-output/output-cell-div.markdown.md.snapshot b/tests/docs/knitr/intermediate-markdown-output/output-cell-div.markdown.md.snapshot new file mode 100644 index 00000000000..bf14fd82b83 --- /dev/null +++ b/tests/docs/knitr/intermediate-markdown-output/output-cell-div.markdown.md.snapshot @@ -0,0 +1,25 @@ +--- +title: "Untitled" +format: markdown +keep-md: true +--- + +This does it: + +::: {.cell} + +```{.r .cell-code} +1 + 1 +``` + +::: {.cell-output .cell-output-stdout} + +``` +[1] 2 +``` + + +::: +::: + +Other content \ No newline at end of file diff --git a/tests/docs/knitr/intermediate-markdown-output/output-cell-div.qmd b/tests/docs/knitr/intermediate-markdown-output/output-cell-div.qmd new file mode 100644 index 00000000000..7f564a65121 --- /dev/null +++ b/tests/docs/knitr/intermediate-markdown-output/output-cell-div.qmd @@ -0,0 +1,11 @@ +--- +title: "Untitled" +format: markdown +keep-md: true +--- + +This does it: +```{r} +1 + 1 +``` +Other content \ No newline at end of file diff --git a/tests/smoke/engine/intermediate-output-markdown.test.ts b/tests/smoke/engine/intermediate-output-markdown.test.ts new file mode 100644 index 00000000000..4b1036b36c9 --- /dev/null +++ b/tests/smoke/engine/intermediate-output-markdown.test.ts @@ -0,0 +1,29 @@ +import { dirname, join } from "path"; +import { ensureSnapshotMatches, noErrors, printsMessage } from "../../verify.ts"; +import { fileLoader } from "../../utils.ts"; +import { safeRemoveIfExists } from "../../../src/core/path.ts"; +import { testRender } from "../render/render.ts"; + +// Define engines to test +const engines = [ + { name: "knitr" }, + { name: "jupyter" }, + { name: "julia" } +]; + +// Run tests for each engine +engines.forEach(engine => { + // Test for engine + const inputQmd = fileLoader(engine.name, "intermediate-markdown-output")("output-cell-div.qmd", "markdown"); + const md = join(dirname(inputQmd.input), "output-cell-div.markdown.md"); + testRender(inputQmd.input, "markdown", true, [ + noErrors, + // Lua Warning are in INFO + printsMessage({ level: "INFO", regex: /WARNING \(.*\)\s+The following string was found in the document: :::/, negate: true}), + ensureSnapshotMatches(md) + ], { + teardown: async () => { + safeRemoveIfExists(md); + } }); +}); + From a5e28a595c0fede0a2c94e494046b6d58db79727 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 25 Mar 2025 14:07:14 +0100 Subject: [PATCH 3/3] Don't break the indentation --- src/resources/rmd/hooks.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/resources/rmd/hooks.R b/src/resources/rmd/hooks.R index 2a74e57bddb..e20577d359a 100644 --- a/src/resources/rmd/hooks.R +++ b/src/resources/rmd/hooks.R @@ -409,7 +409,8 @@ knitr_hooks <- function(format, resourceDir, handledLanguages) { } else { # Newline first and after to ensure Pandoc Fenced Div is correctly parsed paste0( - options[["indent"]], "\n::: {", + "\n", + options[["indent"]], "::: {", labelId(label), paste(classes, collapse = " ") ,forwardAttr, "}\n", x, "\n", cell.cap , options[["indent"]], ":::\n" )