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.

return original markdown text, override format (#6464)

Shahmir Varqhabf635f5067e3
diff --git a/marimo/_output/md.py b/marimo/_output/md.py
index 21e4ddff25..62b6d6266b 100644
--- a/marimo/_output/md.py
+++ b/marimo/_output/md.py
@@ -248,6 +248,22 @@ class _md(Html):
def _repr_markdown_(self) -> str:
return self._markdown_text
+ def __format__(self, spec: str) -> str:
+ """
+ This overrides the default HTML formatting behavior to return the original
+ markdown text instead of the processed HTML. This prevents multiline code
+ blocks from being flattened when nested mo.md() calls are interpolated
+ into other mo.md() calls.
+
+ Args:
+ spec: Format specification (ignored)
+
+ Returns:
+ The original markdown text (self._markdown_text)
+ """
+ del spec
+ return self._markdown_text
+
@mddoc
def md(text: str) -> Html:
diff --git a/marimo/_smoke_tests/markdown/codeblocks.py b/marimo/_smoke_tests/markdown/codeblocks.py
index 959f26fbae..10fd64aba7 100644
--- a/marimo/_smoke_tests/markdown/codeblocks.py
+++ b/marimo/_smoke_tests/markdown/codeblocks.py
@@ -20,39 +20,53 @@ def _(mo):
def _(mo):
mo.md(
r"""
- ```python
- def fibonacci(n):
- if n <= 0:
- return []
- elif n == 1:
- return [0]
- elif n == 2:
- return [0, 1]
-
- fib_sequence = [0, 1]
- for i in range(2, n):
- fib_sequence.append(fib_sequence[i-1] + fib_sequence[i-2])
-
- return fib_sequence
- ```
- """
+ ```python
+ def fibonacci(n):
+ if n <= 0:
+ return []
+ elif n == 1:
+ return [0]
+ elif n == 2:
+ return [0, 1]
+
+ fib_sequence = [0, 1]
+ for i in range(2, n):
+ fib_sequence.append(fib_sequence[i-1] + fib_sequence[i-2])
+
+ return fib_sequence
+ ```
+ """
)
return
@app.cell(hide_code=True)
-def _(mo):
+def _(code_block, mo):
mo.md(
- r"""
- ```
- # Example usage:
- # print(fibonacci(10))
- ```
- """
+ rf"""
+ ```
+ # Example usage:
+ # print(fibonacci(10))
+ ```
+
+ **Nested code block**
+ {code_block}
+ """
)
return
[email protected](hide_code=True)
+def _(mo):
+ code_block = mo.md("""
+ ```python
+ def add(x: int, y: int):
+ return x + y
+ ```
+ """)
+ return (code_block,)
+
+
@app.cell
def _():
import marimo as mo
diff --git a/marimo/_smoke_tests/markdown/latex_highlighting.py b/marimo/_smoke_tests/markdown/latex_highlighting.py
index 4f7068b110..df8a0d697e 100644
--- a/marimo/_smoke_tests/markdown/latex_highlighting.py
+++ b/marimo/_smoke_tests/markdown/latex_highlighting.py
@@ -8,36 +8,36 @@ app = marimo.App(width="medium")
def _(mo):
mo.md(
"""
- ## Markdown / Latex Highlighting
+ ## Markdown / Latex Highlighting
- ```ts
- console.log("highlight code fences")
- ```
+ ```ts
+ console.log("highlight code fences")
+ ```
- ```python
- def lang_python():
- pass
- ```
+ ```python
+ def lang_python():
+ pass
+ ```
- ```
- def no_language():
- pass
- ```
+ ```
+ def no_language():
+ pass
+ ```
- **bold**
+ **bold**
- _italic_
+ _italic_
- $\sigma\sqrt{100}$
+ $\sigma\sqrt{100}$
- $$
- \sigma\sqrt{100}
- $$
+ $$
+ \sigma\sqrt{100}
+ $$
- \[ \sigma\sqrt{100} \]
+ \[ \sigma\sqrt{100} \]
- \( \sigma\sqrt{100} \)
- """
+ \( \sigma\sqrt{100} \)
+ """
)
return
diff --git a/marimo/_smoke_tests/markdown/markdown_size.py b/marimo/_smoke_tests/markdown/markdown_size.py
index 9cbda76514..9bb0649500 100644
--- a/marimo/_smoke_tests/markdown/markdown_size.py
+++ b/marimo/_smoke_tests/markdown/markdown_size.py
@@ -27,7 +27,7 @@ def _(mo):
@app.cell
def _(mo, size, sizes):
- mo.md(f"{size} **{sizes[size.value]}**")
+ mo.md(f"""{size} **{sizes[size.value]}**""")
return
diff --git a/tests/_output/test_hypertext.py b/tests/_output/test_hypertext.py
index 77510286ea..4c662444ca 100644
--- a/tests/_output/test_hypertext.py
+++ b/tests/_output/test_hypertext.py
@@ -249,3 +249,44 @@ def test_html_rich_elems():
"mimetype": "text/html",
"data": df,
}
+
+
+def test_nested_md_preserves_multiline_code_blocks():
+ """Test that nested mo.md() calls preserve multiline code blocks."""
+ from marimo._output.md import _md
+
+ # Create a markdown object with a multiline code block
+ inner_md = _md("""
+ ```python
+ multiline
+ text
+ ```
+ """)
+
+ # Create a nested markdown object that includes the inner one
+ outer_md = _md(f"{inner_md}")
+
+ # The nested markdown should preserve the multiline formatting
+ # The HTML should contain the newlines in the code block
+ assert 'multiline</span>\n<span class="n">text' in outer_md.text
+ assert (
+ 'multiline</span> <span class="n">text' not in outer_md.text
+ ) # Should not be flattened
+
+
+def test_nested_md_format_method():
+ """Test that the __format__ method of _md returns markdown text."""
+ from marimo._output.md import _md
+
+ # Create a markdown object
+ md_obj = _md("""
+ ```python
+ line1
+ line2
+ ```
+ """)
+
+ # When formatted (e.g., in f-strings), it should return the original markdown
+ formatted = f"{md_obj}"
+ assert "line1\nline2" in formatted
+ assert "line1 line2" not in formatted # Should not be flattened
diff --git a/tests/_output/test_md.py b/tests/_output/test_md.py
index 21e9dcd7f9..a667271f3e 100644
--- a/tests/_output/test_md.py
+++ b/tests/_output/test_md.py
@@ -257,6 +257,16 @@ def test_md_repr_markdown():
assert md._repr_markdown_() == input_text
+def test_md_format():
+ input_text = "This is **bold** and this is _italic_."
+ md = _md(input_text)
+ assert f"{md}" == input_text
+
+ input_text = "```python\nprint('Hello, world!')\n```"
+ md = _md(input_text)
+ assert f"{md}" == input_text
+
+
@patch("marimo._runtime.output")
def test_latex_via_path(output: MagicMock, tmp_path: Path) -> None:
filename = tmp_path / "macros.tex"