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.

Preserve bracketed widget values in Markdown (#11385)

Trevor Manzc716635c0949
diff --git a/marimo/_plugins/core/web_component.py b/marimo/_plugins/core/web_component.py
index 2dfbda575f..e4be8d150a 100644
--- a/marimo/_plugins/core/web_component.py
+++ b/marimo/_plugins/core/web_component.py
@@ -54,7 +54,13 @@ def _build_attr(name: str, value: JSONType) -> str:
# when embedding in markdown
# - dollar sign, when unescaped can incorrectly be recognized as
# latex delimiter when embedding into markdown
- processed = processed.replace("\\", "\").replace("$", "$")
+ # - brackets can be interpreted as markdown links or images
+ processed = (
+ processed.replace("\\", "\")
+ .replace("$", "$")
+ .replace("[", "[")
+ .replace("]", "]")
+ )
return f"data-{name}='{processed}'"
diff --git a/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt b/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt
index 6804cd7cf4..7fac4dfd35 100644
--- a/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt
+++ b/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt
@@ -73,7 +73,7 @@
{
"data": {
"text/markdown": [
- "<marimo-slider data-initial-value='0' data-label='null' data-start='0' data-stop='10' data-steps='[]' data-debounce='false' data-disabled='false' data-orientation='"horizontal"' data-show-value='false' data-include-input='false' data-full-width='false'></marimo-slider>"
+ "<marimo-slider data-initial-value='0' data-label='null' data-start='0' data-stop='10' data-steps='[]' data-debounce='false' data-disabled='false' data-orientation='"horizontal"' data-show-value='false' data-include-input='false' data-full-width='false'></marimo-slider>"
]
},
"metadata": {},
diff --git a/tests/_islands/snapshots/island-mimetypes.txt b/tests/_islands/snapshots/island-mimetypes.txt
index 53369d3476..6a75b888c0 100644
--- a/tests/_islands/snapshots/island-mimetypes.txt
+++ b/tests/_islands/snapshots/island-mimetypes.txt
@@ -34,7 +34,7 @@
data-reactive="true"
>
<marimo-cell-output>
- <marimo-json-output data-json-data='[1,&quot;text/markdown:&#92;u003cspan class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&#92;u003e&#92;u003cspan class=&#92;&quot;paragraph&#92;&quot;&#92;u003eHello&#92;u003c/span&#92;u003e&#92;u003c/span&#92;u003e&quot;]' data-value-types='&quot;python&quot;'></marimo-json-output>
+ <marimo-json-output data-json-data='&#91;1,&quot;text/markdown:&#92;u003cspan class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&#92;u003e&#92;u003cspan class=&#92;&quot;paragraph&#92;&quot;&#92;u003eHello&#92;u003c/span&#92;u003e&#92;u003c/span&#92;u003e&quot;&#93;' data-value-types='&quot;python&quot;'></marimo-json-output>
</marimo-cell-output>
<marimo-cell-code hidden>%5B1%2C%20mo.md('Hello')%5D</marimo-cell-code>
</marimo-island>
\ No newline at end of file
diff --git a/tests/_output/formatters/test_formatters.py b/tests/_output/formatters/test_formatters.py
index c53a594858..bdda3c5317 100644
--- a/tests/_output/formatters/test_formatters.py
+++ b/tests/_output/formatters/test_formatters.py
@@ -707,7 +707,7 @@ def test_as_html_basic_types() -> None:
result = as_html([1, 2, 3])
assert (
result.text
- == "<marimo-json-output data-json-data='[1,2,3]' data-value-types='&quot;python&quot;'></marimo-json-output>"
+ == "<marimo-json-output data-json-data='&#91;1,2,3&#93;' data-value-types='&quot;python&quot;'></marimo-json-output>"
)
# Dict
@@ -833,7 +833,7 @@ def test_as_html_with_plain_wrapper() -> None:
result = as_html(plain_list)
assert (
result.text
- == "<marimo-json-output data-json-data='[1,2,3]' data-value-types='&quot;python&quot;'></marimo-json-output>"
+ == "<marimo-json-output data-json-data='&#91;1,2,3&#93;' data-value-types='&quot;python&quot;'></marimo-json-output>"
)
# Test with a dict that would normally get marimo-json formatting
diff --git a/tests/_plugins/core/test_web_component.py b/tests/_plugins/core/test_web_component.py
index 60b6eac13d..2678ddf1a0 100644
--- a/tests/_plugins/core/test_web_component.py
+++ b/tests/_plugins/core/test_web_component.py
@@ -3,6 +3,9 @@ from __future__ import annotations
import re
+import pytest
+
+import marimo as mo
from marimo._output.formatting import as_html
from marimo._output.md import md
from marimo._plugins.core.web_component import (
@@ -51,6 +54,22 @@ def test_embed_dollar_sign_in_md() -> None:
assert "arithmatex" not in markdown.text
+ "value",
+ [
+ "C[A](C)",
+ "[link](https://example.com)",
+ "![alt](image.png)",
+ "[a][b]",
+ ],
+)
+def test_embed_markdown_link_in_md(value: str) -> None:
+ element = mo.ui.text(value=value, label="**Smile String** :")
+ rendered = md(f"{element}\n\n[b]: https://example.com")
+
+ assert parse_initial_value(rendered.text) == value
+
+
# DOMPurify's SAFE_FOR_XML rule strips any attribute whose value matches
# `]>`, `-->`, `--!>`, or `</(style|script|title|xmp|textarea|noscript|iframe
# |noembed|noframes)`. `_build_attr` must emit JSON-escaped `<`/`>` so the
diff --git a/tests/_plugins/stateless/test_accordion.py b/tests/_plugins/stateless/test_accordion.py
index ea447488dd..68d583c76b 100644
--- a/tests/_plugins/stateless/test_accordion.py
+++ b/tests/_plugins/stateless/test_accordion.py
@@ -103,12 +103,16 @@ def test_accordion_expanded(
@pytest.mark.parametrize("multiple", [False, True])
def test_empty_accordion_expanded(multiple: bool) -> None:
result = accordion({}, multiple=multiple, expanded=True)
- assert "data-expanded='[]'" in result.text
+ match = re.search("data-expanded='(.*?)'", result.text)
+ assert match is not None
+ assert json.loads(html.unescape(match[1])) == []
def test_accordion_default_collapsed() -> None:
result = accordion({"Summary": "Overview"})
- assert "data-expanded='[]'" in result.text
+ match = re.search("data-expanded='(.*?)'", result.text)
+ assert match is not None
+ assert json.loads(html.unescape(match[1])) == []
@pytest.mark.parametrize("multiple", [False, True])
diff --git a/tests/_plugins/stateless/test_routes.py b/tests/_plugins/stateless/test_routes.py
index b37149bcae..624926b009 100644
--- a/tests/_plugins/stateless/test_routes.py
+++ b/tests/_plugins/stateless/test_routes.py
@@ -1,6 +1,10 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
+import html
+import json
+import re
+
from marimo._runtime.context import get_context
from marimo._runtime.context.types import RuntimeContext
from marimo._runtime.runtime import Kernel
@@ -74,4 +78,6 @@ async def test_routes_non_lazy(k: Kernel, exec_req: ExecReqProvider) -> None:
routes = k.globals["routes"]
children = "<span>42</span><span>45</span>"
assert children in routes.text
- assert "data-routes='[&quot;#/&quot;,&quot;{/*path}&quot;]'" in routes.text
+ match = re.search("data-routes='(.*?)'", routes.text)
+ assert match is not None
+ assert json.loads(html.unescape(match[1])) == ["#/", "{/*path}"]