Skip to content

Commit ce581d3

Browse files
authored
Bugfix/issue 3179 (#4038)
Closes #3179
1 parent e2b5aba commit ce581d3

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

news/changelog-1.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Add support for embedding cell outputs in quarto documents using `{{< embed >}}`. You can address cells by Id, Tag, or label, such as `{{< embed mynotebook.ipynb#fig-output >}}` which would embed the output of a cell with the label `fig-output`). You can also provide a list of ids like `{{< embed mynotebook.ipynb#fig-output,tbl-out >}}`.
44
- Only attempt to postprocess `text/plain` output if it's nonempty ([#3896](https://github.com/quarto-dev/quarto-cli/issues/3896)).
55
- Fix output of bokeh plots so the right number of cells is generated ([#2107](https://github.com/quarto-dev/quarto-cli/issues/2107)).
6+
- Fix output of code cells that contain triple backticks (or more) ([#3179](https://github.com/quarto-dev/quarto-cli/issues/3179)).
67

78
## Code Annotation
89

src/core/jupyter/jupyter.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,25 @@ export interface JupyterOutputError extends JupyterOutput {
243243
traceback: string[];
244244
}
245245

246+
const countTicks = (code: string[]) => {
247+
// FIXME do we need trim() here?
248+
const countLeadingTicks = (s: string) => {
249+
// count leading ticks using regexps
250+
const m = s.match(/^`+/);
251+
if (m) {
252+
return m[0].length;
253+
} else {
254+
return 0;
255+
}
256+
};
257+
return Math.max(0, ...code.map((s) => countLeadingTicks(s)));
258+
};
259+
260+
const ticksForCode = (code: string[]) => {
261+
const n = Math.max(3, countTicks(code) + 1);
262+
return "`".repeat(n);
263+
};
264+
246265
export async function quartoMdToJupyter(
247266
markdown: string,
248267
includeIds: boolean,
@@ -1222,7 +1241,9 @@ async function mdFromCodeCell(
12221241
// write code if appropriate
12231242
if (includeCode(cell, options)) {
12241243
const fenced = echoFenced(cell, options);
1225-
const ticks = fenced ? "````" : "```";
1244+
const ticks = "`".repeat(
1245+
Math.max(countTicks(cell.source) + 1, fenced ? 4 : 3),
1246+
);
12261247

12271248
md.push(ticks + " {");
12281249
if (typeof cell.options[kCellLstLabel] === "string") {
@@ -1727,7 +1748,8 @@ function mdMarkdownOutput(md: string[]) {
17271748
}
17281749

17291750
function mdFormatOutput(format: string, source: string[]) {
1730-
return mdEnclosedOutput("```{=" + format + "}", source, "```");
1751+
const ticks = ticksForCode(source);
1752+
return mdEnclosedOutput(ticks + "{=" + format + "}", source, ticks);
17311753
}
17321754

17331755
function mdLatexOutput(latex: string[]) {
@@ -1790,8 +1812,9 @@ function mdTrimEmptyLines(
17901812
}
17911813

17921814
function mdCodeOutput(code: string[], clz?: string) {
1793-
const open = "```" + (clz ? `{.${clz}}` : "");
1794-
return mdEnclosedOutput(open, code, "```");
1815+
const ticks = ticksForCode(code);
1816+
const open = ticks + (clz ? `{.${clz}}` : "");
1817+
return mdEnclosedOutput(open, code, ticks);
17951818
}
17961819

17971820
function mdEnclosedOutput(begin: string, text: string[], end: string) {

tests/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function outputForInput(input: string, to: string) {
2929
if (baseFormat === "revealjs") {
3030
outputExt = "html";
3131
}
32-
if (baseFormat === "commonmark") {
32+
if (baseFormat === "commonmark" || baseFormat === "gfm") {
3333
outputExt = "md";
3434
}
3535
if (baseFormat === "csljson") {

0 commit comments

Comments
 (0)