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
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
@@ -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`

30 changes: 29 additions & 1 deletion src/core/jupyter/jupyter-fixups.ts
Original file line number Diff line number Diff line change
@@ -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,
@@ -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) => {
2 changes: 1 addition & 1 deletion src/core/pandoc/pandoc-partition.ts
Original file line number Diff line number Diff line change
@@ -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) {
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
*
@@ -12,7 +12,6 @@ import {
import { assert } from "testing/asserts";

(() => {
const input = "docs/convert/backticks.ipynb";
testQuartoCmd(
"convert",
["docs/convert/issue-12042.ipynb"],
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
@@ -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);
}
}
};
}

Unchanged files with check annotations Beta

test('Jupyter - Creates working tabsets from for loops', async ({ page }) => {
await page.goto('/html/tabsets/jupyter-tabsets.html');
const tab1 = page.getByRole('tab', { name: 'tab1 inside for loop:' });
await expect(tab1).toHaveClass(/active/);

Check failure on line 6 in tests/integration/playwright/tests/html-tabsets.spec.ts

GitHub Actions / Running Tests buckets 03 / Run smoke (ubuntu-latest)

[chromium] › html-tabsets.spec.ts:3:5 › Jupyter - Creates working tabsets from for loops

1) [chromium] › html-tabsets.spec.ts:3:5 › Jupyter - Creates working tabsets from for loops ────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveClass(expected) Locator: getByRole('tab', { name: 'tab1 inside for loop:' }) Expected pattern: /active/ Received: <element(s) not found> Call log: - expect.toHaveClass with timeout 5000ms - waiting for getByRole('tab', { name: 'tab1 inside for loop:' }) 4 | await page.goto('/html/tabsets/jupyter-tabsets.html'); 5 | const tab1 = page.getByRole('tab', { name: 'tab1 inside for loop:' }); > 6 | await expect(tab1).toHaveClass(/active/); | ^ 7 | const tabContent = page.locator('div.tab-content') 8 | await expect(tabContent).toBeVisible(); 9 | const tab1Content = tabContent.locator('div.tab-pane').first(); at /home/runner/work/quarto-cli/quarto-cli/tests/integration/playwright/tests/html-tabsets.spec.ts:6:22

Check failure on line 6 in tests/integration/playwright/tests/html-tabsets.spec.ts

GitHub Actions / Running Tests buckets 03 / Run smoke (ubuntu-latest)

[webkit] › html-tabsets.spec.ts:3:5 › Jupyter - Creates working tabsets from for loops

3) [webkit] › html-tabsets.spec.ts:3:5 › Jupyter - Creates working tabsets from for loops ──────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveClass(expected) Locator: getByRole('tab', { name: 'tab1 inside for loop:' }) Expected pattern: /active/ Received: <element(s) not found> Call log: - expect.toHaveClass with timeout 5000ms - waiting for getByRole('tab', { name: 'tab1 inside for loop:' }) 4 | await page.goto('/html/tabsets/jupyter-tabsets.html'); 5 | const tab1 = page.getByRole('tab', { name: 'tab1 inside for loop:' }); > 6 | await expect(tab1).toHaveClass(/active/); | ^ 7 | const tabContent = page.locator('div.tab-content') 8 | await expect(tabContent).toBeVisible(); 9 | const tab1Content = tabContent.locator('div.tab-pane').first(); at /home/runner/work/quarto-cli/quarto-cli/tests/integration/playwright/tests/html-tabsets.spec.ts:6:22
const tabContent = page.locator('div.tab-content')
await expect(tabContent).toBeVisible();
const tab1Content = tabContent.locator('div.tab-pane').first();