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.

fix: strip stray <p></p> HTML tags from imported Jupyter markdown cells (#8674)

Giulio Leone442de784bbed
diff --git a/marimo/_convert/ipynb/to_ir.py b/marimo/_convert/ipynb/to_ir.py
index badac93634..c956588eea 100644
--- a/marimo/_convert/ipynb/to_ir.py
+++ b/marimo/_convert/ipynb/to_ir.py
@@ -9,6 +9,8 @@ from collections import defaultdict
from dataclasses import dataclass, field
from typing import Any, Callable, Union
+from pymdownx.superfences import RE_NESTED_FENCE_START # type: ignore
+
from marimo._ast.cell import CellConfig
from marimo._ast.compiler import compile_cell
from marimo._ast.names import DEFAULT_CELL_NAME
@@ -39,7 +41,100 @@ class CodeCell:
config: CellConfig = field(default_factory=CellConfig)
-# Define a type for transforms that add/remove cells
+# Regex patterns for stripping bare <p>/<\/p> tags from Jupyter markdown cells.
+# Only match bare <p> (no attributes) — styled tags like <p style="color: red">
+# carry semantic meaning and must be preserved.
+_BARE_OPEN_P_RE = re.compile(r"<p\s*>", re.IGNORECASE)
+_ANY_OPEN_P_RE = re.compile(r"<p(?:\s[^>]*)?>", re.IGNORECASE)
+_CLOSE_P_RE = re.compile(r"</p>", re.IGNORECASE)
+
+
+def _strip_paragraph_tags(source: str) -> str:
+ """Remove bare ``<p>`` / ``</p>`` HTML tags from markdown source.
+
+ Jupyter markdown cells often wrap content in ``<p>…</p>`` tags which are
+ redundant in plain markdown and can break LaTeX rendering inside
+ ``mo.md()``.
+
+ Only bare ``<p>`` tags (without attributes) are removed. Styled tags such
+ as ``<p style="color: red">`` are preserved because they carry semantic
+ meaning. The matching ``</p>`` is only removed when it closes a bare
+ ``<p>``.
+
+ Closing ``</p>`` tags for bare opens are replaced with a newline to
+ preserve paragraph separation. Content inside fenced code blocks is
+ left untouched.
+ """
+ # Collect fenced code block spans so we can skip them.
+ # Uses RE_NESTED_FENCE_START from pymdownx.superfences (already a marimo
+ # dependency) instead of a hand-rolled regex.
+ protected: list[tuple[int, int]] = []
+ fence_marker: str | None = None
+ block_start = 0
+ pos = 0
+ for line in source.split("\n"):
+ if fence_marker is None:
+ m = RE_NESTED_FENCE_START.match(line)
+ if m:
+ fence_marker = m.group("fence")
+ block_start = pos
+ else:
+ stripped = line.strip()
+ if stripped == fence_marker or (
+ stripped.startswith(fence_marker)
+ and stripped == stripped[0] * len(stripped)
+ ):
+ protected.append((block_start, pos + len(line)))
+ fence_marker = None
+ pos += len(line) + 1 # +1 for the newline
+
+ def _in_protected(pos: int) -> bool:
+ return any(s <= pos < e for s, e in protected)
+
+ # Pair opening <p> tags with closing </p> using a simple stack.
+ # Only mark pairs for removal when the opening tag is bare (no attrs).
+ opens = list(_ANY_OPEN_P_RE.finditer(source))
+ closes = list(_CLOSE_P_RE.finditer(source))
+
+ remove_opens: list[re.Match[str]] = []
+ remove_closes: list[re.Match[str]] = []
+ stack: list[re.Match[str]] = []
+
+ # Walk all tags in document order
+ all_tags: list[tuple[int, str, re.Match[str]]] = []
+ for m in opens:
+ all_tags.append((m.start(), "open", m))
+ for m in closes:
+ all_tags.append((m.start(), "close", m))
+ all_tags.sort(key=lambda t: t[0])
+
+ for _pos, kind, match in all_tags:
+ if _in_protected(match.start()):
+ continue
+ if kind == "open":
+ stack.append(match)
+ elif kind == "close" and stack:
+ opener = stack.pop()
+ if _BARE_OPEN_P_RE.fullmatch(opener.group()):
+ remove_opens.append(opener)
+ remove_closes.append(match)
+
+ # Build set of (start, end) spans to remove, sorted end-to-start
+ removals: list[tuple[int, int, str]] = []
+ for m in remove_opens:
+ removals.append((m.start(), m.end(), ""))
+ for m in remove_closes:
+ removals.append((m.start(), m.end(), "\n"))
+ removals.sort(key=lambda t: t[0], reverse=True)
+
+ for start, end, replacement in removals:
+ source = source[:start] + replacement + source[end:]
+
+ # Clean up excessive blank lines introduced by replacements
+ source = re.sub(r"\n{3,}", "\n\n", source)
+ return source.strip()
+
+
CellsTransform = Callable[[list[CodeCell]], list[CodeCell]]
@@ -1404,6 +1499,7 @@ def convert_from_ipynb_to_notebook_ir(
)
is_markdown: bool = cell["cell_type"] == "markdown"
if is_markdown:
+ source = _strip_paragraph_tags(source)
cell_meta = cell.get("metadata", {})
md_prefix = cell_meta.get("marimo", {}).get(
"md_prefix", DEFAULT_MARKDOWN_PREFIX
diff --git a/tests/_convert/ipynb/test_strip_paragraph_tags.py b/tests/_convert/ipynb/test_strip_paragraph_tags.py
new file mode 100644
index 0000000000..572e7b7801
--- /dev/null
+++ b/tests/_convert/ipynb/test_strip_paragraph_tags.py
@@ -0,0 +1,122 @@
+r"""Tests for _strip_paragraph_tags in to_ir.py.
+
+Covers the reviewer suggestions from PR #8674:
+- Basic <p>/<\/p> removal
+- Preservation of styled <p> tags (with attributes)
+- Preservation of content inside fenced code blocks
+- Paragraph separation when adjacent </p><p> tags are removed
+- Case-insensitive matching
+- Inline code protection
+"""
+
+from __future__ import annotations
+
+from marimo._convert.ipynb.to_ir import _strip_paragraph_tags
+
+
+class TestStripParagraphTags:
+ def test_basic_removal(self) -> None:
+ assert _strip_paragraph_tags("<p>Hello world</p>") == "Hello world"
+
+ def test_preserves_styled_p_tag(self) -> None:
+ """Styled <p> tags carry semantic meaning and must be preserved."""
+ source = '<p style="color: red">Red text</p>'
+ result = _strip_paragraph_tags(source)
+ assert '<p style="color: red">' in result
+ assert "</p>" in result
+
+ def test_preserves_p_with_class(self) -> None:
+ result = _strip_paragraph_tags('<p class="lead">Styled text</p>')
+ assert '<p class="lead">' in result
+ assert "</p>" in result
+
+ def test_preserves_p_with_id(self) -> None:
+ result = _strip_paragraph_tags('<p id="intro">Intro text</p>')
+ assert '<p id="intro">' in result
+
+ def test_mixed_bare_and_styled(self) -> None:
+ """Bare <p> stripped, styled <p> preserved in same source."""
+ source = '<p>Plain text</p>\n<p style="color: blue">Blue text</p>'
+ result = _strip_paragraph_tags(source)
+ assert "<p>" not in result
+ assert "Plain text" in result
+ assert '<p style="color: blue">' in result
+ assert "Blue text" in result
+
+ def test_case_insensitive(self) -> None:
+ assert _strip_paragraph_tags("<P>UPPER</P>") == "UPPER"
+ assert _strip_paragraph_tags("<p>Mixed</P>") == "Mixed"
+
+ def test_preserves_other_html_tags(self) -> None:
+ source = "<p>Text with <strong>bold</strong> content</p>"
+ result = _strip_paragraph_tags(source)
+ assert "<strong>bold</strong>" in result
+ assert "<p>" not in result
+
+ def test_adjacent_paragraphs_preserve_separation(self) -> None:
+ """Closing </p> should become a newline, not vanish."""
+ source = "<p>First paragraph</p><p>Second paragraph</p>"
+ result = _strip_paragraph_tags(source)
+ assert "First paragraph" in result
+ assert "Second paragraph" in result
+ # The two paragraphs must not be collapsed into one word
+ assert "First paragraphSecond" not in result
+
+ def test_fenced_code_block_preserved(self) -> None:
+ """<p> tags inside fenced code blocks must not be stripped."""
+ source = (
+ "<p>Before code</p>\n"
+ "```html\n"
+ "<p>This is an HTML example</p>\n"
+ "```\n"
+ "<p>After code</p>"
+ )
+ result = _strip_paragraph_tags(source)
+ # Tags outside code block are removed
+ assert result.startswith("Before code")
+ assert result.endswith("After code")
+ # Tags inside code block are kept
+ assert "<p>This is an HTML example</p>" in result
+
+ def test_tilde_fenced_code_block_preserved(self) -> None:
+ """Tilde-fenced code blocks are also protected."""
+ source = "<p>Intro</p>\n~~~\n<p>Code content</p>\n~~~\n<p>Outro</p>"
+ result = _strip_paragraph_tags(source)
+ assert "<p>Code content</p>" in result
+ assert "<p>Intro" not in result
+ assert "<p>Outro" not in result
+
+ def test_multiline_paragraph(self) -> None:
+ source = "<p>\nLine 1\nLine 2\n</p>"
+ result = _strip_paragraph_tags(source)
+ assert "Line 1" in result
+ assert "Line 2" in result
+ assert "<p>" not in result
+ assert "</p>" not in result
+
+ def test_no_tags_passthrough(self) -> None:
+ source = "Plain markdown with **bold** and `code`"
+ assert _strip_paragraph_tags(source) == source
+
+ def test_empty_string(self) -> None:
+ assert _strip_paragraph_tags("") == ""
+
+ def test_latex_not_broken(self) -> None:
+ """LaTeX content should pass through unmodified."""
+ source = "<p>$E = mc^2$</p>"
+ result = _strip_paragraph_tags(source)
+ assert "$E = mc^2$" in result
+ assert "<p>" not in result
+
+ def test_nested_html_preserved(self) -> None:
+ """Non-<p> HTML tags (div, span, etc.) must survive."""
+ source = '<p><div class="note">Important</div></p>'
+ result = _strip_paragraph_tags(source)
+ assert '<div class="note">Important</div>' in result
+
+ def test_adjacent_styled_paragraphs_fully_preserved(self) -> None:
+ """Adjacent styled <p> tags must keep both open and close."""
+ source = '<p style="color:red">A</p><p style="color:blue">B</p>'
+ result = _strip_paragraph_tags(source)
+ assert '<p style="color:red">A</p>' in result
+ assert '<p style="color:blue">B</p>' in result