mdformat-myst
mdformat-myst is a plugin for the mdformat Markdown formatter, providing compatibility with MyST (Markedly Structured Text) Markdown syntax. It enables mdformat to correctly parse and format MyST-specific elements like directives, roles, and math blocks. The current version is 0.3.0, and it follows an active release cadence, frequently updating to support new mdformat features and MyST specifications.
Common errors
-
Error: Unknown plugin: myst
cause The `mdformat-myst` package is not installed or `mdformat` cannot find it.fixRun `pip install mdformat-myst` to ensure the plugin is installed in the same environment as `mdformat`. -
AttributeError: 'RootRenderer' object has no attribute 'render_dollarmath'
cause This usually indicates an incompatibility between `mdformat-myst` and the core `mdformat` version, or a missing required dependency.fixEnsure `mdformat` and all its direct dependencies (e.g., `mdit-py-plugins`) are compatible with your `mdformat-myst` version. Try `pip install --upgrade mdformat mdformat-myst`. -
mdformat: error: unrecognized arguments: --plugin myst
cause This error means mdformat itself is installed, but `mdformat-myst` is not, or the installed `mdformat` version is too old to support the `--plugin` flag or dynamic plugin loading, or the plugin name 'myst' is not recognized.fixVerify `mdformat-myst` is installed and `mdformat` is up-to-date (`pip install --upgrade mdformat mdformat-myst`). Also ensure the correct plugin name `myst` is used. -
SyntaxError: invalid syntax (mdformat_myst.py, line XX)
cause You are likely running an older Python version than required. `mdformat-myst>=0.2.2` requires Python 3.10+.fixUpgrade your Python environment to 3.10 or newer. If you must use an older Python, downgrade `mdformat-myst` to `<0.2.2`.
Warnings
- breaking Python 3.8 support was dropped in version 0.2.2. Users on Python 3.8 or older will need to upgrade their Python version to 3.10+ or pin mdformat-myst to a version prior to 0.2.2.
- breaking In version 0.3.0, the internal dependency for table formatting changed from `mdformat-tables` to `mdformat-gfm`. If your project explicitly relied on `mdformat-tables` for MyST table formatting, you should now ensure `mdformat-gfm` is installed and properly configured if you were not already using it. This is automatically handled if you just `pip install mdformat-myst`.
- breaking Version 0.3.0 replaced `mdformat-frontmatter` with `mdformat-front-matters` for handling front matter. This is a name change of a required dependency. Users with specific pinning of `mdformat-frontmatter` might experience issues.
- gotcha mdformat-myst is an extension for mdformat; it does not provide a standalone executable. To use it, you must install `mdformat` separately and then invoke `mdformat` with the `--plugin myst` flag or `extensions=('myst',)` in the Python API.
Install
-
pip install mdformat-myst
Imports
- mdformat.text
from mdformat import text
Quickstart
import os
from mdformat import text
# Example Markdown with MyST syntax
myst_markdown = """
---
title: MyST Example
author: Me
---
# MyST Title
````{myst-role}
This is a myst role content.
````
$$a^2 + b^2 = c^2$$
:::{note}
This is a MyST directive.
:::
"""
# --- Command Line Usage ---
# For command line, save the content to a file, e.g., 'example.md'
# Then run: mdformat example.md --plugin myst
# --- Python API Usage ---
formatted_text = text(myst_markdown, extensions=("myst",))
print(formatted_text)
# To verify it's formatted by mdformat-myst
# Note: This is a simplified check, actual verification requires inspecting output
assert ':::{note}' in formatted_text or '$a^2 + b^2 = c^2$' in formatted_text, \
"MyST specific elements should be preserved/formatted."