Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ py-snapshots:
uv run --group test pytest \
tests/_server/templates/test_templates.py \
tests/_server/api/endpoints/test_export.py \
tests/test_api.py
tests/test_api.py \
tests/test_project_dependencies.py

##############
# Packaging #
Expand Down
26 changes: 16 additions & 10 deletions marimo/_output/md.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PyconDetectorExtension(markdown.Extension):
def extendMarkdown(self, md: markdown.Markdown) -> None:
"""Add the preprocessor to the markdown instance."""
processor = PyconDetectorPreprocessor(md)
md.preprocessors.register(processor, "pycon_detector", 30)
md.preprocessors.register(processor, "pycon_detector", 175)


class PyconDetectorPreprocessor(markdown.preprocessors.Preprocessor):
Expand All @@ -53,9 +53,12 @@ class PyconDetectorPreprocessor(markdown.preprocessors.Preprocessor):

def __init__(self, md: markdown.Markdown) -> None:
super().__init__(md)
# Pattern to match fenced code blocks
# Pattern to match fenced code blocks (backreference \2 ensures the
# closing fence matches the opening fence marker, preventing greedy
# over-consumption of adjacent fenced blocks)
self.fence_pattern = re.compile(
r"^(\s*)```(\w*)\s*\n(.*?)^(\s*)```\s*$", re.MULTILINE | re.DOTALL
r"^(\s*)(```+|~~~+)(\w*)\s*\n(.*?)^\s*\2\s*$",
re.MULTILINE | re.DOTALL,
)

def run(self, lines: list[str]) -> list[str]:
Expand All @@ -64,14 +67,14 @@ def run(self, lines: list[str]) -> list[str]:

def replace_fence(match: re.Match[str]) -> str:
indent = match.group(1)
language = match.group(2) or ""
code = match.group(3)
fence = match.group(2)
language = match.group(3) or ""
code = match.group(4)

# Only process if no language is specified
if not language:
if self._detect_pycon(code):
# Replace with pycon language
return f"{indent}```pycon\n{code}{indent}```"
if not language and self._detect_pycon(code):
# Replace with pycon language
return f"{indent}{fence}pycon\n{code}{indent}{fence}"

# Return original
return match.group(0)
Expand Down Expand Up @@ -250,7 +253,10 @@ def __init__(
text,
extensions=_get_extensions(),
extension_configs=_get_extension_configs(),
).strip()
)
if html_text is None:
html_text = ""
html_text = html_text.strip()
# replace <p> tags with <span> as HTML doesn't allow nested <div>s in <p>s
html_text = html_text.replace(
"<p>", '<span class="paragraph">'
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ dependencies = [
# compile markdown to html
"markdown>=3.6,<4",
# add features to markdown
# Pinned to specific version for introduction of codeblock handling.
"pymdown-extensions>=10.15,<11",
# 10.21.2+ required for compatibility with pygments>=2.20 (filename=None fix).
"pymdown-extensions>=10.21.2,<11",
# syntax highlighting of code in markdown
"pygments>=2.19,<3",
"pygments>=2.20,<3",
Comment thread
tschm marked this conversation as resolved.
# for reading, writing configs
"tomlkit>=0.12.0",
# for managing frontmatter headers in markdown
Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ msgspec>=0.20.0
narwhals>=2.0.0
packaging
psutil>=5.0
pygments>=2.19,<3
pymdown-extensions>=10.15,<11
pygments>=2.20,<3
pymdown-extensions>=10.21.2,<11
pyyaml>=6.0.1
pyzmq>=27.1.0; python_version < '3.15'
starlette>=0.37.2
Expand Down
Loading