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.

improvement: add details block to pymdown (#3533)

Myles Scolnick51fcc5ea03fd
diff --git a/frontend/src/components/data-table/__tests__/url-detector.test.tsx b/frontend/src/components/data-table/__tests__/url-detector.test.tsx
index 4dc652c9ad..1b85920d5d 100644
--- a/frontend/src/components/data-table/__tests__/url-detector.test.tsx
+++ b/frontend/src/components/data-table/__tests__/url-detector.test.tsx
@@ -1,3 +1,4 @@
+/* Copyright 2024 Marimo. All rights reserved. */
import { render, screen, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import { UrlDetector } from "../url-detector";
diff --git a/frontend/src/css/md.css b/frontend/src/css/md.css
index 7af99087fe..d579343451 100644
--- a/frontend/src/css/md.css
+++ b/frontend/src/css/md.css
@@ -281,3 +281,76 @@ button .prose.prose {
.prose .keys {
@apply space-x-1;
}
+
+/* Details */
+
+.prose .details {
+ @apply space-y-1;
+}
+
+.markdown details {
+ @apply rounded-lg border bg-card;
+
+ margin: 1rem 0;
+ border-left-width: 6px;
+}
+
+.markdown details.warn,
+.markdown details.warning {
+ border-left-color: var(--yellow-8);
+}
+
+.markdown details.info,
+.markdown details.note {
+ border-left-color: var(--blue-8);
+}
+
+.markdown details.error,
+.markdown details.danger,
+.markdown details.caution {
+ border-left-color: var(--red-8);
+}
+
+.markdown details.success,
+.markdown details.tip {
+ border-left-color: var(--grass-8);
+}
+
+.markdown details.important {
+ border-left-color: var(--cyan-8);
+}
+
+.markdown details.hint {
+ border-left-color: var(--gray-8);
+}
+
+.markdown details summary {
+ @apply flex cursor-pointer items-center justify-between p-4 font-medium;
+
+ list-style: none;
+ user-select: none;
+}
+
+.markdown details summary::-webkit-details-marker {
+ display: none;
+}
+
+.markdown details summary::after {
+ content: "";
+ border-style: solid;
+ border-color: var(--slate-8);
+ border-width: 0.15rem 0.15rem 0 0;
+ display: inline-block;
+ padding: 0.2rem;
+ transform: rotate(135deg);
+ transition: transform 0.2s;
+ vertical-align: middle;
+}
+
+.markdown details[open] summary::after {
+ transform: rotate(-45deg);
+}
+
+.markdown details > :not(summary) {
+ @apply p-4 pt-0;
+}
diff --git a/marimo/_output/md.py b/marimo/_output/md.py
index afb719d99b..3e9c8d8291 100644
--- a/marimo/_output/md.py
+++ b/marimo/_output/md.py
@@ -1,8 +1,10 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
+from functools import cache
+from importlib.util import find_spec
from inspect import cleandoc
-from typing import Literal, Optional
+from typing import Any, Literal, Optional, Union
import markdown # type: ignore
import pymdownx.emoji # type: ignore
@@ -12,7 +14,7 @@ from marimo._output.md_extensions.external_links import ExternalLinksExtension
from marimo._output.md_extensions.iconify import IconifyExtension
from marimo._output.rich_help import mddoc
-extension_configs = {
+extension_configs: dict[str, dict[str, Any]] = {
"pymdownx.arithmatex": {
# Use "generic" mode, no preview, since we don't use MathJax
"preview": False,
@@ -42,6 +44,67 @@ extension_configs = {
MarkdownSize = Literal["sm", "base", "lg", "xl", "2xl"]
+def _has_module(module_name: str) -> bool:
+ try:
+ return find_spec(module_name) is not None
+ except Exception:
+ return False
+
+
+@cache
+def get_extensions() -> list[Union[str, markdown.Extension]]:
+ return [
+ # Syntax highlighting
+ "codehilite",
+ # Markdown tables
+ "tables",
+ # LaTeX
+ "pymdownx.arithmatex",
+ # Base64 is not enabled, since app users could potentially
+ # use it to grab files they shouldn't have access to.
+ # "pymdownx.b64",
+ # Subscripts and strikethrough
+ "pymdownx.tilde",
+ # Better code blocks
+ "pymdownx.superfences",
+ # Task lists
+ "pymdownx.tasklist",
+ # Caption, Tabs, Details
+ *(
+ [
+ module
+ for module in [
+ "pymdownx.blocks.caption",
+ "pymdownx.blocks.tab",
+ "pymdownx.blocks.details",
+ ]
+ if _has_module(module)
+ ]
+ ),
+ # Critic - color-coded markup
+ "pymdownx.critic",
+ # Emoji - :emoji:
+ "pymdownx.emoji",
+ # Keys - <kbd> support
+ "pymdownx.keys",
+ # Magic links - auto-link URLs
+ "pymdownx.magiclink",
+ # Table of contents
+ # This adds ids to the HTML headers
+ "toc",
+ # Footnotes
+ "footnotes",
+ # Admonitions
+ "admonition",
+ # Sane lists, to include <ol start="n">
+ "sane_lists",
+ # Links
+ ExternalLinksExtension(),
+ # Iconify
+ IconifyExtension(),
+ ]
+
+
class _md(Html):
def __init__(
self,
@@ -66,49 +129,7 @@ class _md(Html):
# markdown.markdown appends a newline, hence strip
html_text = markdown.markdown(
text,
- extensions=[
- # Syntax highlighting
- "codehilite",
- # Markdown tables
- "tables",
- # LaTeX
- "pymdownx.arithmatex",
- # Base64 is not enabled, since app users could potentially
- # use it to grab files they shouldn't have access to.
- # "pymdownx.b64",
- # Subscripts and strikethrough
- "pymdownx.tilde",
- # Better code blocks
- "pymdownx.superfences",
- # Task lists
- "pymdownx.tasklist",
- # Require 10.12, but go as low as 10.x
- # # Caption
- # "pymdownx.blocks.caption",
- # # Tabs
- # "pymdownx.blocks.tab",
- # Critic - color-coded markup
- "pymdownx.critic",
- # Emoji - :emoji:
- "pymdownx.emoji",
- # Keys - <kbd> support
- "pymdownx.keys",
- # Magic links - auto-link URLs
- "pymdownx.magiclink",
- # Table of contents
- # This adds ids to the HTML headers
- "toc",
- # Footnotes
- "footnotes",
- # Admonitions
- "admonition",
- # Sane lists, to include <ol start="n">
- "sane_lists",
- # Links
- ExternalLinksExtension(),
- # Iconify
- IconifyExtension(),
- ],
+ extensions=get_extensions(),
extension_configs=extension_configs, # type: ignore[arg-type]
).strip()
# replace <p> tags with <span> as HTML doesn't allow nested <div>s in <p>s
diff --git a/marimo/_smoke_tests/markdown_pymdownx.py b/marimo/_smoke_tests/markdown_pymdownx.py
index 85e22e9aec..261c9b4b64 100644
--- a/marimo/_smoke_tests/markdown_pymdownx.py
+++ b/marimo/_smoke_tests/markdown_pymdownx.py
@@ -1,6 +1,6 @@
import marimo
-__generated_with = "0.9.34"
+__generated_with = "0.10.15"
app = marimo.App(width="medium")
@@ -91,6 +91,44 @@ def _(mo):
return
+def _(mo):
+ mo.md(
+ """
+ ## Details
+
+ /// details | Basic details
+ This is a basic details section
+ ///
+
+ /// details | Info details
+ type: info
+
+ This shows important information
+ ///
+
+ /// details | Warning details
+ type: warn
+
+ This highlights something to watch out for
+ ///
+
+ /// details | Danger details
+ type: danger
+
+ This indicates a critical warning or dangerous situation
+ ///
+
+ /// details | Success details
+ type: success
+
+ This indicates a successful outcome or positive note
+ ///
+ """
+ )
+ return
+
+
@app.cell
def _(mo):
mo.md(