From 552b402fa8909776a85c590307c3f13a8050395f Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Wed, 28 Feb 2024 09:55:29 -0700 Subject: [PATCH 1/7] convert - use enough backticks for the content --- src/command/convert/jupyter.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/command/convert/jupyter.ts b/src/command/convert/jupyter.ts index 6b02165cda..c1415ed457 100644 --- a/src/command/convert/jupyter.ts +++ b/src/command/convert/jupyter.ts @@ -144,8 +144,14 @@ async function mdFromCodeCell( return []; } + // determine the largest number of backticks in the cell + const maxBackticks = Math.max( + ...cell.source.map((line) => line.match(/^`+/g)?.[0].length || 0), + ); + const backticks = "`".repeat(maxBackticks + 1); + // begin code cell - const md: string[] = ["```{" + language + "}\n"]; + const md: string[] = [backticks + "{" + language + "}\n"]; // partition const { yaml, source } = await partitionCellOptions(language, cell.source); @@ -215,7 +221,7 @@ async function mdFromCodeCell( md.push(...mdEnsureTrailingNewline(source)); // end code cell - md.push("```\n"); + md.push(backticks + "\n"); return md; } From 1f604dbd8295fc107cbf70b98d8f55b080a9327f Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Wed, 28 Feb 2024 10:18:27 -0700 Subject: [PATCH 2/7] docusaurus code blocks - use enough backticks for the content --- .../quarto/docusaurus/docusaurus_utils.lua | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/resources/extensions/quarto/docusaurus/docusaurus_utils.lua b/src/resources/extensions/quarto/docusaurus/docusaurus_utils.lua index 49649b2db7..c8918cd03a 100644 --- a/src/resources/extensions/quarto/docusaurus/docusaurus_utils.lua +++ b/src/resources/extensions/quarto/docusaurus/docusaurus_utils.lua @@ -58,6 +58,14 @@ function render_folded_block(block) end end +local function max_backticks(el) + local v = 0 + for w in el.text:gmatch("`+") do + v = math.max(v, #w) + end + return v +end + function code_block(code_block_slot, filename) function process(el) local lang = el.attr.classes[1] @@ -68,7 +76,8 @@ function code_block(code_block_slot, filename) if not lang then lang = 'text' end - local code = "\n```" .. lang + local backticks = string.rep("`", max_backticks(el) + 1) + local code = "\n" .. backticks .. lang if codeLineNumbers then code = code .. " {" .. codeLineNumbers .. "}" end @@ -78,7 +87,7 @@ function code_block(code_block_slot, filename) if title then code = code .. " title=\"" .. title .. "\"" end - code = code .. "\n" .. el.text .. "\n```\n" + code = code .. "\n" .. el.text .. "\n" .. backticks .. "\n" -- docusaures code block attributes don't conform to any syntax -- that pandoc natively understands, so return the CodeBlock as From ecb6968921f415976e17d574eaea2dc7ae3b5aad Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Wed, 28 Feb 2024 10:22:37 -0700 Subject: [PATCH 3/7] regression test --- tests/docs/smoke-all/2024/02/28/8919-a.qmd | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/docs/smoke-all/2024/02/28/8919-a.qmd diff --git a/tests/docs/smoke-all/2024/02/28/8919-a.qmd b/tests/docs/smoke-all/2024/02/28/8919-a.qmd new file mode 100644 index 0000000000..ffa88a4115 --- /dev/null +++ b/tests/docs/smoke-all/2024/02/28/8919-a.qmd @@ -0,0 +1,18 @@ +--- +jupyter: python3 +_quarto: + tests: + docusaurus-md: + ensureFileRegexMatches: + - ["````"] +--- + +````{python} +message = """ +Content +```python +print("Hello, world!") +``` +""" +```` + From db696f6f2d584b14dfb58ba7acb4ce8db2b2cc2f Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Wed, 28 Feb 2024 10:33:17 -0700 Subject: [PATCH 4/7] regression test for convert --- tests/docs/convert/backticks.ipynb | 40 +++++++++++++++++++ tests/smoke/convert/convert-backticks.test.ts | 36 +++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 tests/docs/convert/backticks.ipynb create mode 100644 tests/smoke/convert/convert-backticks.test.ts diff --git a/tests/docs/convert/backticks.ipynb b/tests/docs/convert/backticks.ipynb new file mode 100644 index 0000000000..73e3bc8253 --- /dev/null +++ b/tests/docs/convert/backticks.ipynb @@ -0,0 +1,40 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "d02a6fa3", + "metadata": {}, + "outputs": [], + "source": [ + "message = \"\"\"\n", + "Content\n", + "```python\n", + "print(\"Hello, world!\")\n", + "```\n", + "\"\"\"" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tests/smoke/convert/convert-backticks.test.ts b/tests/smoke/convert/convert-backticks.test.ts new file mode 100644 index 0000000000..e3081fccbf --- /dev/null +++ b/tests/smoke/convert/convert-backticks.test.ts @@ -0,0 +1,36 @@ +/* +* convert-backticks.test.ts +* +* Copyright (C) 2020-2024 Posit Software, PBC +* +*/ +import { existsSync } from "../../../src/deno_ral/fs.ts"; +import { + ExecuteOutput, + testQuartoCmd, +} from "../../test.ts"; +import { assert } from "testing/asserts.ts"; + +(() => { + const input = "docs/convert/backticks.ipynb"; + testQuartoCmd( + "convert", + ["docs/convert/backticks.ipynb"], + [ + { + name: "convert-enough-backticks", + verify: async (outputs: ExecuteOutput[]) => { + const txt = Deno.readTextFileSync("docs/convert/backticks.qmd"); + assert(txt.includes("````"), "Not enough backticks in output"); + } + } + ], + { + teardown: async () => { + if (existsSync("docs/convert/backticks.qmd")) { + Deno.removeSync("docs/convert/backticks.qmd"); + } + } + }, + ); +})(); From 4bcd37502a6652c18601e93dc770ee8d0c6942ef Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Wed, 28 Feb 2024 10:37:18 -0700 Subject: [PATCH 5/7] changelog --- news/changelog-1.5.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/news/changelog-1.5.md b/news/changelog-1.5.md index 545d7aebe7..b9248f6e95 100644 --- a/news/changelog-1.5.md +++ b/news/changelog-1.5.md @@ -18,6 +18,10 @@ All changes included in 1.5: - ([#8382](https://github.com/quarto-dev/quarto-cli/issues/8382)): Strip whitespace from `div.columns` elements that might have been introduced by third-party processing. +## Docusaurus Format + +- ([#8919](https://github.com/quarto-dev/quarto-cli/issues/8919)): Ensure enough backticks in `quarto convert` from `.ipynb` to `.qmd` files. + ## Website - ([#6779](https://github.com/quarto-dev/quarto-cli/issues/6779)): Add support for `logo-href` and `logo-alt` in `sidebar` (books and websites) @@ -51,6 +55,7 @@ All changes included in 1.5: - ([#8433](https://github.com/quarto-dev/quarto-cli/issues/8433)): Escape jupyter widget states that contain `` so they can be embedded in HTML documents. - When searching for kernelspecs that match `python`, prefer one one that matches an active Python venv. - ([#8454](https://github.com/quarto-dev/quarto-cli/issues/8454)): Allow Jupyter engine to handle markdown files with mixed-case extensions. +- ([#8919](https://github.com/quarto-dev/quarto-cli/issues/8919)): Ensure enough backticks in `quarto convert` from `.ipynb` to `.qmd` files. ## Website Listings From 6573efd3a290aa4c04e844a8bd54f68583bad41f Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Wed, 28 Feb 2024 11:18:12 -0700 Subject: [PATCH 6/7] fix base case --- src/resources/extensions/quarto/docusaurus/docusaurus_utils.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resources/extensions/quarto/docusaurus/docusaurus_utils.lua b/src/resources/extensions/quarto/docusaurus/docusaurus_utils.lua index c8918cd03a..b03c0947ce 100644 --- a/src/resources/extensions/quarto/docusaurus/docusaurus_utils.lua +++ b/src/resources/extensions/quarto/docusaurus/docusaurus_utils.lua @@ -76,7 +76,7 @@ function code_block(code_block_slot, filename) if not lang then lang = 'text' end - local backticks = string.rep("`", max_backticks(el) + 1) + local backticks = string.rep("`", math.max(3, max_backticks(el) + 1)) local code = "\n" .. backticks .. lang if codeLineNumbers then code = code .. " {" .. codeLineNumbers .. "}" From 87c6259753e4f935d494c2ab64efe58cd0528cd1 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Wed, 28 Feb 2024 12:57:26 -0700 Subject: [PATCH 7/7] fix changelog --- news/changelog-1.5.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/changelog-1.5.md b/news/changelog-1.5.md index b9248f6e95..beb1f496d8 100644 --- a/news/changelog-1.5.md +++ b/news/changelog-1.5.md @@ -20,7 +20,7 @@ All changes included in 1.5: ## Docusaurus Format -- ([#8919](https://github.com/quarto-dev/quarto-cli/issues/8919)): Ensure enough backticks in `quarto convert` from `.ipynb` to `.qmd` files. +- ([#8919](https://github.com/quarto-dev/quarto-cli/issues/8919)): Ensure enough backticks in code cell declarations. ## Website