-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor: support multiple markdown flavors in marimo export md
#9586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
peter-gy
wants to merge
20
commits into
marimo-team:main
Choose a base branch
from
peter-gy:ptr/flavored-markdown-exports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
374bddc
feat: add flavored markdown export rendering
peter-gy 8bd0daa
feat: expose markdown flavors through export surfaces
peter-gy 8304c68
fix: normalize qmd callout titles
peter-gy 9953a07
fix: preserve explicit qmd callout title quotes
peter-gy 02b4235
fix: harden flavored markdown rendering
peter-gy e77862f
fix: invalidate markdown auto-export by flavor
peter-gy baaf3db
fix: remove obsolete markdown flavor defaults
peter-gy f4249c0
feat: infer flavor from filename
peter-gy 574456d
fix: preserve authored markdown in flavor exports
peter-gy 5a79966
refactor: use mystmd naming for markdown import
peter-gy a1cd823
docs: cleanup
peter-gy 345f701
fix: keep markdown output inference explicit
peter-gy a55b737
style: format code
peter-gy aa5b1d1
fix: cover escape edge case
peter-gy f9c54e2
fix: preserve flavored markdown export artifacts
peter-gy 8d42a4f
fix: round-trip myst markdown config
peter-gy 8a434ec
chore: defer api support to a next pr
peter-gy fc0a8bc
refactor: isolate markdown import dialects
peter-gy c3df9c1
fix: preserve MyST markdown round trips
peter-gy 321266a
fix: keep markdown export targets from retitling notebooks
peter-gy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| # Copyright 2026 Marimo. All rights reserved. | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from pathlib import Path | ||
| from types import MappingProxyType | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from marimo._convert.markdown.flavor.base import ( | ||
| MarkdownFlavor, | ||
| MarkdownFlavorName, | ||
| MarkdownImportDialect, | ||
| ) | ||
| from marimo._convert.markdown.flavor.mystmd import ( | ||
| MystmdMarkdownFlavor, | ||
| _MystmdMarkdownImportDialect, | ||
| ) | ||
| from marimo._convert.markdown.flavor.pymdown import PymdownMarkdownFlavor | ||
| from marimo._convert.markdown.flavor.qmd import QmdMarkdownFlavor | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Mapping | ||
|
|
||
| _PYMDOWN_MARKDOWN = PymdownMarkdownFlavor() | ||
| _QMD_MARKDOWN = QmdMarkdownFlavor() | ||
| _MYSTMD_MARKDOWN = MystmdMarkdownFlavor() | ||
| _MYSTMD_MARKDOWN_IMPORT = _MystmdMarkdownImportDialect() | ||
| _MARKDOWN_FLAVORS: Mapping[MarkdownFlavorName, MarkdownFlavor] = ( | ||
| MappingProxyType( | ||
| { | ||
| _PYMDOWN_MARKDOWN.name: _PYMDOWN_MARKDOWN, | ||
| _QMD_MARKDOWN.name: _QMD_MARKDOWN, | ||
| _MYSTMD_MARKDOWN.name: _MYSTMD_MARKDOWN, | ||
| } | ||
| ) | ||
| ) | ||
| _MARKDOWN_IMPORT_DIALECTS: Mapping[ | ||
| MarkdownFlavorName, MarkdownImportDialect | ||
| ] = MappingProxyType({_MYSTMD_MARKDOWN_IMPORT.name: _MYSTMD_MARKDOWN_IMPORT}) | ||
| # Filename inference handles target-specific markdown extensions. | ||
| _MARKDOWN_FLAVORS_BY_EXTENSION: Mapping[str, MarkdownFlavor] = ( | ||
| MappingProxyType({".myst.md": _MYSTMD_MARKDOWN, ".qmd": _QMD_MARKDOWN}) | ||
| ) | ||
| # Download and auto-export filenames use the selected flavor's suffix. | ||
| _MARKDOWN_OUTPUT_EXTENSIONS: Mapping[MarkdownFlavorName, str] = ( | ||
| MappingProxyType( | ||
| { | ||
| "pymdown": "md", | ||
| "qmd": "qmd", | ||
| "mystmd": "myst.md", | ||
| } | ||
| ) | ||
| ) | ||
| # Strip known markdown suffixes before applying an output suffix. | ||
| _MARKDOWN_FILENAME_SUFFIXES = ( | ||
| ".myst.md", | ||
| ".markdown", | ||
| ".qmd", | ||
| ".md", | ||
| ) | ||
|
|
||
|
|
||
| def default_markdown_flavor() -> MarkdownFlavor: | ||
| return _PYMDOWN_MARKDOWN | ||
|
|
||
|
|
||
| def markdown_flavor_from_filename(filename: str) -> MarkdownFlavor: | ||
| """Infer the export flavor from a filename extension.""" | ||
| suffixes = Path(filename).suffixes | ||
| for suffix in ("".join(suffixes[-2:]), suffixes[-1] if suffixes else ""): | ||
| if suffix in _MARKDOWN_FLAVORS_BY_EXTENSION: | ||
| return _MARKDOWN_FLAVORS_BY_EXTENSION[suffix] | ||
| return default_markdown_flavor() | ||
|
|
||
|
|
||
| def normalize_markdown_flavor( | ||
| flavor: MarkdownFlavor | MarkdownFlavorName | None, | ||
| *, | ||
| filename: str, | ||
| ) -> MarkdownFlavor: | ||
| """Resolve an optional flavor name or instance to a concrete flavor.""" | ||
| if flavor is None: | ||
| return markdown_flavor_from_filename(filename) | ||
| if isinstance(flavor, MarkdownFlavor): | ||
| return flavor | ||
| try: | ||
| return _MARKDOWN_FLAVORS[flavor] | ||
| except KeyError as error: | ||
| raise ValueError(f"Unsupported markdown flavor: {flavor!r}") from error | ||
|
|
||
|
|
||
| def _markdown_import_dialects( | ||
| text: str, filepath: str | None | ||
| ) -> tuple[MarkdownImportDialect, ...]: | ||
| return tuple( | ||
| dialect | ||
| for dialect in _MARKDOWN_IMPORT_DIALECTS.values() | ||
| if dialect.matches(text, filepath) | ||
| ) | ||
|
|
||
|
|
||
| def _markdown_output_extension( | ||
| flavor: MarkdownFlavor | MarkdownFlavorName, | ||
| ) -> str: | ||
| flavor_name = flavor.name if isinstance(flavor, MarkdownFlavor) else flavor | ||
| return _MARKDOWN_OUTPUT_EXTENSIONS[flavor_name] | ||
|
|
||
|
|
||
| def markdown_output_filename( | ||
| filename: str | None, | ||
| flavor: MarkdownFlavor | MarkdownFlavorName, | ||
| ) -> str: | ||
| """Return the filename for a rendered markdown artifact. | ||
|
|
||
| Known markdown suffixes are replaced with the selected flavor's suffix. | ||
| For example, exporting `notebook.myst.md` as pymdown returns `notebook.md`. | ||
| """ | ||
| extension = _markdown_output_extension(flavor) | ||
| basename = os.path.basename(filename or f"notebook.{extension}") | ||
| for suffix in _MARKDOWN_FILENAME_SUFFIXES: | ||
| if basename.endswith(suffix): | ||
| return f"{basename[: -len(suffix)]}.{extension}" | ||
| return f"{os.path.splitext(basename)[0]}.{extension}" | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "default_markdown_flavor", | ||
| "markdown_flavor_from_filename", | ||
| "markdown_output_filename", | ||
| "normalize_markdown_flavor", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.