Skip to content

Commit

Permalink
Merge pull request #8936 from quarto-dev/improve/auto-max-backticks
Browse files Browse the repository at this point in the history
markdown - change markdown gen to automatically emit sufficiently-many : and `s
  • Loading branch information
cscheid authored Feb 29, 2024
2 parents e0b9b02 + 519a1bc commit dfbf871
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
28 changes: 18 additions & 10 deletions src/core/handlers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ import {
optionCommentPatternFromLanguage,
} from "../lib/partition-cell-options.ts";
import { ConcreteSchema } from "../lib/yaml-schema/types.ts";
import { pandocBlock, pandocList, pandocRawStr } from "../pandoc/codegen.ts";
import {
pandocCode,
pandocDiv,
pandocList,
pandocRawStr,
} from "../pandoc/codegen.ts";

import {
kCapLoc,
Expand Down Expand Up @@ -61,7 +66,13 @@ import {
kTblCapLoc,
} from "../../config/constants.ts";
import { DirectiveCell } from "../lib/break-quarto-md-types.ts";
import { basename, dirname, join, relative, resolve } from "../../deno_ral/path.ts";
import {
basename,
dirname,
join,
relative,
resolve,
} from "../../deno_ral/path.ts";
import { figuresDir, inputFilesDir } from "../render.ts";
import { ensureDirSync } from "fs/mod.ts";
import { mappedStringFromFile } from "../mapped-text.ts";
Expand Down Expand Up @@ -629,12 +640,9 @@ export const baseHandler: LanguageHandler = {

const unrolledOutput = isPowerpointOutput && !hasLayoutAttributes;

const t3 = pandocBlock("```");
const t4 = pandocBlock("````");

const cellBlock = unrolledOutput
? pandocList({ skipFirstLineBreak: true })
: pandocBlock(":::")({
: pandocDiv({
classes: ["cell", ...classes],
attrs,
});
Expand Down Expand Up @@ -667,7 +675,7 @@ export const baseHandler: LanguageHandler = {

switch (options.echo) {
case true: {
const cellInput = t3({
const cellInput = pandocCode({
classes: cellInputClasses,
attrs: cellInputAttrs,
});
Expand All @@ -676,11 +684,11 @@ export const baseHandler: LanguageHandler = {
break;
}
case "fenced": {
const cellInput = t4({
const cellInput = pandocCode({
classes: ["markdown", ...cellInputClasses.slice(1)], // replace the language class with markdown
attrs: cellInputAttrs,
});
const cellFence = t3({
const cellFence = pandocCode({
language: this.languageName,
skipFirstLineBreak: true,
});
Expand All @@ -695,7 +703,7 @@ export const baseHandler: LanguageHandler = {
}
}

const divBlock = pandocBlock(":::");
const divBlock = pandocDiv;

// PandocNodes ignore self-pushes (n.push(n))
// this makes it much easier to write the logic around "unrolled blocks"
Expand Down
35 changes: 28 additions & 7 deletions src/core/pandoc/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function pandocList(opts: {
};
}

export function pandocBlock(delimiter: string) {
export function pandocBlock(delimiterCharacter: ":" | "`") {
return function (
opts?: {
language?: string;
Expand Down Expand Up @@ -197,21 +197,42 @@ export function pandocBlock(delimiter: string) {
}
},
emit: function (ls: EitherString[]) {
const innerLs: EitherString[] = [];
const lb = skipFirstLineBreak ? "" : "\n";
ls.push(`${lb}${delimiter}${attrString()}\n`);

for (const entry of contents!) {
entry.emit(ls);
entry.emit(innerLs);
}
if (!asMappedString(ls[ls.length - 1] || "\n").value.endsWith("\n")) {
ls.push(`\n`);
if (
!asMappedString(innerLs[innerLs.length - 1] || "\n").value.endsWith(
"\n",
)
) {
innerLs.push(`\n`);
}

// now find the longest streak of delimiter characters in the innerLs
const longestStreak = Math.max(...innerLs.map((eitherS) => {
const s = asMappedString(eitherS).value;
return s.match(new RegExp(`${delimiterCharacter}+`, "g"))?.[0]
?.length || 0;
}));
const delimiter = delimiterCharacter.repeat(
Math.max(3, longestStreak + 1),
);
ls.push(`${lb}${delimiter}${attrString()}\n`);

// FIXME this will incur a runtime of eventually O(n * m) where n is the number of lines and m is the depth of
// the PandocNode tree.
ls.push(...innerLs);

ls.push(`${delimiter}\n`);
},
};
};
}

export const pandocDiv = pandocBlock(":::");
export const pandocCode = pandocBlock("```");
export const pandocDiv = pandocBlock(":");
export const pandocCode = pandocBlock("`");
export const pandocFigure = pandocHtmlBlock("figure");
export const pandocFigCaption = pandocHtmlBlock("figcaption");
4 changes: 2 additions & 2 deletions src/execute/ojs/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export async function ojsCompile(
);
ojsParseError(err, cellSrc);

const preDiv = pandocBlock("````")({
const preDiv = pandocCode({
classes: ["numberLines", "java"],
attrs: [
`startFrom="${cellStartingLoc - 1}"`,
Expand Down Expand Up @@ -543,7 +543,7 @@ export async function ojsCompile(
`startFrom="${cellStartingLoc - 1}"`,
`source-offset="${cell.sourceOffset}"`,
);
const srcDiv = pandocBlock("````")({
const srcDiv = pandocCode({
classes: ourClasses,
attrs: ourAttrs,
});
Expand Down
21 changes: 21 additions & 0 deletions tests/docs/smoke-all/mermaid/backticks.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: "mermaid backtick test"
keep-md: true
_quarto:
tests:
html:
ensureFileRegexMatches:
- []
- ["<p>This would have been"] # it can't be in a paragraph, it should be inside a code cell.
---

It's not important right now whether this syntax is correct for mermaid;
the relevant aspect is that our pandoc codegen wasn't taking the
inner number of backticks into account. This test checks for that.

````{mermaid}
%%| echo: true
```
This would have been a problem.
```
````

0 comments on commit dfbf871

Please sign in to comment.