Skip to content

convert - handle cells with both YAML and implicit titles #12549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/changelog-1.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ All changes included in 1.7:

- ([#12042](https://github.com/quarto-dev/quarto-cli/issues/12042)): Preserve Markdown content that follows YAML metadata in a `raw` .ipynb cell.
- ([#12318](https://github.com/quarto-dev/quarto-cli/issues/12318)): Ensure enough line breaks between cells that might be trimmed.
- ([#12440](https://github.com/quarto-dev/quarto-cli/issues/12440)): Support conversion of cells with both YAML and implicit titles.

### `quarto inspect`

Expand Down
30 changes: 29 additions & 1 deletion src/core/jupyter/jupyter-fixups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Metadata } from "../../config/types.ts";
import { lines } from "../lib/text.ts";
import { markdownWithExtractedHeading } from "../pandoc/pandoc-partition.ts";
import { partitionYamlFrontMatter, readYamlFromMarkdown } from "../yaml.ts";
import { JupyterNotebook, JupyterOutput } from "./types.ts";
import { JupyterCell, JupyterNotebook, JupyterOutput } from "./types.ts";
import {
jupyterCellSrcAsLines,
jupyterCellSrcAsStr,
Expand Down Expand Up @@ -149,6 +149,34 @@ export function fixupFrontMatter(nb: JupyterNotebook): JupyterNotebook {
return lns.map((line) => line.endsWith("\n") ? line : `${line}\n`);
};

// https://github.com/quarto-dev/quarto-cli/issues/12440
// first, we need to find cells that have front matter _and_ markdown content,
// and split them into two cells.
const newCells: JupyterCell[] = [];
nb.cells.forEach((cell) => {
if (cell.cell_type === "raw" || cell.cell_type === "markdown") {
const partitioned = partitionYamlFrontMatter(jupyterCellSrcAsStr(cell)) ||
undefined;
if (partitioned?.yaml.trim()) {
newCells.push({
cell_type: "raw",
source: nbLines(partitioned.yaml.split("\n")),
metadata: {},
});
}
if (partitioned?.markdown.trim()) {
newCells.push({
cell_type: "markdown",
source: nbLines(partitioned.markdown.split("\n")),
metadata: {},
});
}
} else {
newCells.push(cell);
}
});
nb.cells = newCells;

// look for the first raw block that has a yaml object
let partitioned: { yaml: string; markdown: string } | undefined;
const frontMatterCellIndex = nb.cells.findIndex((cell) => {
Expand Down
2 changes: 1 addition & 1 deletion src/core/pandoc/pandoc-partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function markdownWithExtractedHeading(markdown: string) {
const parsedHeading = parsePandocTitle(line);
headingText = parsedHeading.heading;
headingAttr = parsedHeading.attr;
contentBeforeHeading = mdLines.length !== 0;
contentBeforeHeading = (mdLines.join("").trim()).length !== 0;
} else if (line.match(/^=+\s*$/) || line.match(/^-+\s*$/)) {
const prevLine = mdLines[mdLines.length - 1];
if (prevLine) {
Expand Down
18 changes: 18 additions & 0 deletions tests/docs/convert/issue-12440.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
format: typst
---

# This is what happens when I don't set the title.

## This is a level 2 heading

This is some paragraph text.

```{python}
# This is an executable Python code block.
print("This is the output of an executable Python code block.")
```

## This is another level 2 heading

This is some more paragraph text.
3 changes: 1 addition & 2 deletions tests/smoke/convert/convert-issue-12042.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* convert-backticks.test.ts
* convert-issue-12042.test.ts
*
* Copyright (C) 2020-2024 Posit Software, PBC
*
Expand All @@ -12,7 +12,6 @@ import {
import { assert } from "testing/asserts";

(() => {
const input = "docs/convert/backticks.ipynb";
testQuartoCmd(
"convert",
["docs/convert/issue-12042.ipynb"],
Expand Down
41 changes: 41 additions & 0 deletions tests/smoke/convert/convert-issue-12440.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* docs/convert/issue-12440.test.ts
*
* Copyright (C) 2025 Posit Software, PBC
*
*/
import { existsSync } from "../../../src/deno_ral/fs.ts";
import { quarto } from "../../../src/quarto.ts";
import {
ExecuteOutput,
testQuartoCmd,
removeFilesTeardown,
} from "../../test.ts";
import { assert } from "testing/asserts";

(() => {
const input = "docs/convert/issue-12440.qmd";
testQuartoCmd(
"convert",
["docs/convert/issue-12440.qmd"],
[
{
name: "convert-mixed-yaml-markdown-cell",
verify: async (outputs: ExecuteOutput[]) => {
await quarto([
"convert",
"docs/convert/issue-12440.ipynb",
"--output",
"docs/convert/issue-12440-out.qmd",
]);
const txt = Deno.readTextFileSync("docs/convert/issue-12440-out.qmd");
assert(txt.includes("title: This is what happens when I don't set the title"), "Markdown text not found in output");
}
}
],
removeFilesTeardown([
"docs/convert/issue-12440-out.qmd",
"docs/convert/issue-12440.ipynb"
]),
);
})();
10 changes: 10 additions & 0 deletions tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,13 @@ function readExecuteOutput(log: string) {
return JSON.parse(line) as ExecuteOutput;
});
}

export const removeFilesTeardown = (fileList: string[]) => {
return {
teardown: async () => {
for (const file of fileList) {
safeRemoveSync(file);
}
}
};
}
Loading