note by @chrisjsewell
This is not something that will be added: the fact that myst-parser uses markdown-it-py is really an implementation detail that is not exposed to the user:
- This implementation could change in the future
- It makes myst-parser "responsible" for changes in the markdown-it extension API
- Its rare that markdown-it extensions can simply be added, without complimentary changes/addition to the base docutils renderer
- this all adds maintenance burden, for limited gain
- MyST also has a clear specification, allowing for arbitrary change to the parser means it is no longer myst that is being parsed
Context
Originally asked in Discussion #515, it would be nice to add support for using custom MarkdownIt plugins by specifying them in conf.py. One simple use case is discussed in Issue #565 where short code emoji syntax could be utilized with the mdit-py-emoji plugin.
Unfortunately, the myst-parser parsers validate against a fixed set of syntax extensions (see myst_parser/config/main.py), preventing customization.
Proposal
Similar to how sphinx supports built-in extensions and 3rd-party extensions via sphinxcontrib (and others), myst-parser would support built-in extensions and custom extensions via markdown-it-py (and/or others).
In sphinx:
- The
application reads the configuration file and loads built-in and custom extensions. It does so by
(a) attempting to import the extension as a module with importlib.import_module(),
(b) run its setup.py, and
(c) add it to the list of app extensions
Per the docs:
When sphinx-build is executed, Sphinx will attempt to import each module that is listed, and execute yourmodule.setup(app). This function is used to prepare the extension (e.g., by executing Python code), linking resources that Sphinx uses in the build process (like CSS or HTML files), and notifying Sphinx of everything the extension offers (such as directive or role definitions). The app argument is an instance of Sphinx and gives you control over most aspects of the Sphinx build.
In myst-parser, the situation would be much simpler:
(NOTE: Users must install the extension they wish to use so it can be imported.)
- Add a string parameter
extname to create_md_parser
- Try to import the extension with
importlib.import_module() and issue a warning if the extension cannot be loaded (also helpful in the event a user mispells the name of the extension in conf.py)
- Enable it with
MarkdownIt.use()
Tasks and updates
Update myst_parser/parsers/mdit.py::create_md_parser() with the following:
Modify myst_parser/config/main.py::check_extensions() to:
note by @chrisjsewell
This is not something that will be added: the fact that myst-parser uses markdown-it-py is really an implementation detail that is not exposed to the user:
Context
Originally asked in Discussion #515, it would be nice to add support for using custom MarkdownIt plugins by specifying them in
conf.py. One simple use case is discussed in Issue #565 where short code emoji syntax could be utilized with themdit-py-emojiplugin.Unfortunately, the
myst-parserparsers validate against a fixed set of syntax extensions (seemyst_parser/config/main.py), preventing customization.Proposal
Similar to how
sphinxsupports built-in extensions and 3rd-party extensions viasphinxcontrib(and others),myst-parserwould support built-in extensions and custom extensions viamarkdown-it-py(and/or others).In
sphinx:applicationreads the configuration file and loads built-in and custom extensions. It does so by(a) attempting to import the extension as a module with
importlib.import_module(),(b) run its
setup.py, and(c) add it to the list of app extensions
Per the docs:
In
myst-parser, the situation would be much simpler:(NOTE: Users must install the extension they wish to use so it can be imported.)
extnametocreate_md_parserimportlib.import_module()and issue a warning if the extension cannot be loaded (also helpful in the event a user mispells the name of the extension inconf.py)MarkdownIt.use()Tasks and updates
Update
myst_parser/parsers/mdit.py::create_md_parser()with the following:try...exceptlogic to import the moduleMarkdownIt.use()Modify
myst_parser/config/main.py::check_extensions()to:ValueError