From cbfb1a4b0e74db10f5d4f088ee7f4a8553e90eb2 Mon Sep 17 00:00:00 2001 From: MichaelHatherly Date: Tue, 16 Jan 2024 18:33:58 +0000 Subject: [PATCH] Handle trailing markdown content in qmd files correctly --- src/server.jl | 8 ++++++++ test/examples/trailing_content.qmd | 11 +++++++++++ test/runtests.jl | 17 +++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 test/examples/trailing_content.qmd diff --git a/src/server.jl b/src/server.jl index a84e687..1af77ba 100644 --- a/src/server.jl +++ b/src/server.jl @@ -176,6 +176,14 @@ function raw_markdown_chunks(path::String) push!(raw_chunks, (type = :code, source, file = path, line, evaluate)) end end + if terminal_line <= length(source_lines) + markdown = join(source_lines[terminal_line:end], "\n") + push!( + raw_chunks, + (type = :markdown, source = markdown, file = path, line = terminal_line), + ) + end + # The case where the notebook has no code cells. if isempty(raw_chunks) && !code_cells push!( diff --git a/test/examples/trailing_content.qmd b/test/examples/trailing_content.qmd new file mode 100644 index 0000000..be91933 --- /dev/null +++ b/test/examples/trailing_content.qmd @@ -0,0 +1,11 @@ +--- +title: Trailing content +--- + +A code block: + +```{julia} +1 + 1 +``` + +And some trailing content. \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index d39b3b3..7ee2928 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -538,6 +538,23 @@ end @test cell["execution_count"] == 0 @test isempty(cell["outputs"]) end + file("trailing_content") do json + cells = json["cells"] + @test length(cells) == 3 + + cell = cells[1] + @test cell["cell_type"] == "markdown" + @test any(line -> contains(line, "A code block:"), cell["source"]) + + cell = cells[2] + @test cell["cell_type"] == "code" + @test cell["execution_count"] == 1 + @test cell["outputs"][1]["data"]["text/plain"] == "2" + + cell = cells[3] + @test cell["cell_type"] == "markdown" + @test any(line -> contains(line, "And some trailing content."), cell["source"]) + end tests end