Skip to content

Support for Adding MarkdownIt Plugins in conf.py #632

@adam-grant-hendry

Description

@adam-grant-hendry

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:

  1. This implementation could change in the future
  2. It makes myst-parser "responsible" for changes in the markdown-it extension API
  3. Its rare that markdown-it extensions can simply be added, without complimentary changes/addition to the base docutils renderer
  4. this all adds maintenance burden, for limited gain
  5. 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:

  1. 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.)

  1. Add a string parameter extname to create_md_parser
  2. 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)
  3. Enable it with MarkdownIt.use()

Tasks and updates

Update myst_parser/parsers/mdit.py::create_md_parser() with the following:

  • Add string parameter for extension module name
  • Write try...except logic to import the module
  • For importable extensions, enable them with MarkdownIt.use()

Modify myst_parser/config/main.py::check_extensions() to:

  • Separate built-in extensions from custom extensions rather than raise a ValueError

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions