Skip to content

Commit a1af643

Browse files
committed
convert - handle cells with both yaml and implicit titles
1 parent 31e496f commit a1af643

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/core/jupyter/jupyter-fixups.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Metadata } from "../../config/types.ts";
1212
import { lines } from "../lib/text.ts";
1313
import { markdownWithExtractedHeading } from "../pandoc/pandoc-partition.ts";
1414
import { partitionYamlFrontMatter, readYamlFromMarkdown } from "../yaml.ts";
15-
import { JupyterNotebook, JupyterOutput } from "./types.ts";
15+
import { JupyterCell, JupyterNotebook, JupyterOutput } from "./types.ts";
1616
import {
1717
jupyterCellSrcAsLines,
1818
jupyterCellSrcAsStr,
@@ -149,6 +149,34 @@ export function fixupFrontMatter(nb: JupyterNotebook): JupyterNotebook {
149149
return lns.map((line) => line.endsWith("\n") ? line : `${line}\n`);
150150
};
151151

152+
// https://github.com/quarto-dev/quarto-cli/issues/12440
153+
// first, we need to find cells that have front matter _and_ markdown content,
154+
// and split them into two cells.
155+
const newCells: JupyterCell[] = [];
156+
nb.cells.forEach((cell) => {
157+
if (cell.cell_type === "raw" || cell.cell_type === "markdown") {
158+
const partitioned = partitionYamlFrontMatter(jupyterCellSrcAsStr(cell)) ||
159+
undefined;
160+
if (partitioned?.yaml.trim()) {
161+
newCells.push({
162+
cell_type: "raw",
163+
source: nbLines(partitioned.yaml.split("\n")),
164+
metadata: {},
165+
});
166+
}
167+
if (partitioned?.markdown.trim()) {
168+
newCells.push({
169+
cell_type: "markdown",
170+
source: nbLines(partitioned.markdown.split("\n")),
171+
metadata: {},
172+
});
173+
}
174+
} else {
175+
newCells.push(cell);
176+
}
177+
});
178+
nb.cells = newCells;
179+
152180
// look for the first raw block that has a yaml object
153181
let partitioned: { yaml: string; markdown: string } | undefined;
154182
const frontMatterCellIndex = nb.cells.findIndex((cell) => {

src/core/pandoc/pandoc-partition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function markdownWithExtractedHeading(markdown: string) {
8484
const parsedHeading = parsePandocTitle(line);
8585
headingText = parsedHeading.heading;
8686
headingAttr = parsedHeading.attr;
87-
contentBeforeHeading = mdLines.length !== 0;
87+
contentBeforeHeading = (mdLines.join("").trim()).length !== 0;
8888
} else if (line.match(/^=+\s*$/) || line.match(/^-+\s*$/)) {
8989
const prevLine = mdLines[mdLines.length - 1];
9090
if (prevLine) {

0 commit comments

Comments
 (0)