Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: quarto specific code block (#7623)

Dylan Madisetti5810436766ee
diff --git a/marimo/_convert/markdown/__init__.py b/marimo/_convert/markdown/__init__.py
index 8342a2e187..5e078ad009 100644
--- a/marimo/_convert/markdown/__init__.py
+++ b/marimo/_convert/markdown/__init__.py
@@ -66,7 +66,8 @@ def convert_from_ir_to_markdown(
metadata["header"] = notebook.header.value.strip()
# Add the expected qmd filter to the metadata.
- if filename.endswith(".qmd"):
+ is_qmd = filename.endswith(".qmd")
+ if is_qmd:
if "filters" not in metadata:
metadata["filters"] = []
if "marimo" not in str(metadata["filters"]):
@@ -165,7 +166,7 @@ def convert_from_ir_to_markdown(
if previous_was_markdown:
document.append("")
previous_was_markdown = False
- document.append(formatted_code_block(code, attributes))
+ document.append(formatted_code_block(code, attributes, is_qmd=is_qmd))
return "\n".join(document).strip()
diff --git a/marimo/_convert/markdown/markdown.py b/marimo/_convert/markdown/markdown.py
index 02d1051bea..d25d21b1dc 100644
--- a/marimo/_convert/markdown/markdown.py
+++ b/marimo/_convert/markdown/markdown.py
@@ -94,7 +94,9 @@ def _get_language(text: str) -> str:
def formatted_code_block(
- code: str, attributes: Optional[dict[str, str]] = None
+ code: str,
+ attributes: Optional[dict[str, str]] = None,
+ is_qmd: bool = False,
) -> str:
"""Wraps code in a fenced code block with marimo attributes."""
if attributes is None:
@@ -106,18 +108,19 @@ def formatted_code_block(
guard = "```"
while guard in code:
guard += "`"
- if DependencyManager.new_superfences.has_required_version(quiet=True):
- return "\n".join(
- [
- f"""{guard}{language} {{.marimo{attribute_str}}}""",
- code,
- guard,
- "",
- ]
- )
- return "\n".join(
- [f"""{guard}{{.{language}.marimo{attribute_str}}}""", code, guard, ""]
- )
+
+ # Quarto executable syntax with claimsLanguage() support
+ # ```{marimo .python attr=...}
+ if is_qmd:
+ head = f"""{guard}{{marimo .{language}{attribute_str}}}"""
+ # Compatible with GitHub syntax highlighting
+ # ```python {.marimo attr=...}
+ elif DependencyManager.new_superfences.has_required_version(quiet=True):
+ head = f"""{guard}{language} {{.marimo{attribute_str}}}"""
+ # ```{.python.marimo attr=...}
+ else:
+ head = f"""{guard}{{.{language}.marimo{attribute_str}}}"""
+ return "\n".join([head, code, guard, ""])
def app_config_from_root(root: Element) -> dict[str, Any]:
diff --git a/marimo/_session/notebook/file_manager.py b/marimo/_session/notebook/file_manager.py
index 838004d302..5c8d01c8ee 100644
--- a/marimo/_session/notebook/file_manager.py
+++ b/marimo/_session/notebook/file_manager.py
@@ -186,15 +186,15 @@ class AppFileManager:
elif path.exists():
header = handler.extract_header(path)
- if header:
- notebook = NotebookSerializationV1(
- app=notebook.app,
- header=Header(value=header) if header else notebook.header,
- cells=notebook.cells,
- violations=notebook.violations,
- valid=notebook.valid,
- filename=str(path),
- )
+ # Rewrap with header if relevant and set filename.
+ notebook = NotebookSerializationV1(
+ app=notebook.app,
+ header=Header(value=header) if header else notebook.header,
+ cells=notebook.cells,
+ violations=notebook.violations,
+ valid=notebook.valid,
+ filename=str(path),
+ )
contents = handler.serialize(notebook)
if persist:
diff --git a/tests/_convert/test_markdown_module.py b/tests/_convert/test_markdown_module.py
index c22a7e9aad..78247ff687 100644
--- a/tests/_convert/test_markdown_module.py
+++ b/tests/_convert/test_markdown_module.py
@@ -286,3 +286,32 @@ def test_convert_from_ir_to_markdown_unparsable_cell():
# Should mark as unparsable
assert 'unparsable="true"' in markdown or "unparsable=true" in markdown
assert "this is { not valid python" in markdown
+
+
+def test_convert_from_ir_to_markdown_qmd_format():
+ """Test that .qmd files use Quarto-compatible fence format."""
+ app = App()
+
+ @app.cell()
+ def test_cell():
+ x = 1
+ return (x,)
+
+ internal_app = InternalApp(app)
+ notebook = internal_app.to_ir()
+
+ # Test .qmd filename produces Quarto format
+ markdown_qmd = convert_from_ir_to_markdown(
+ notebook, filename="notebook.qmd"
+ )
+ assert "```{marimo .python" in markdown_qmd
+ assert "filters:" in markdown_qmd # qmd should have marimo filter
+
+ # Test .md filename produces standard format
+ markdown_md = convert_from_ir_to_markdown(notebook, filename="notebook.md")
+ assert "```{marimo .python" not in markdown_md
+ # Should use either superfences or fallback format
+ assert (
+ "```python {.marimo" in markdown_md
+ or "```{.python.marimo" in markdown_md
+ )