-
Notifications
You must be signed in to change notification settings - Fork 196
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
Support for Adding MarkdownIt Plugins in conf.py
#632
Comments
Thanks for opening your first issue here! Engagement like this is essential for open source projects! 🤗 |
I tried adding support for this, it's about as far as loading the plugin I think, but when generating HTML output, I get the following warning:
I'd have assumed that https://github.com/BlueGlassBlock/mdit-py-emoji/blob/master/mdit_py_emoji/__init__.py#L49 takes care of that, but it seems not? To be fair, I'm not really a Python dev, so a lot of shooting in the dark. My current patch: diff --git a/myst_parser/config/main.py b/myst_parser/config/main.py
index a134ea7..1dc34fb 100644
--- a/myst_parser/config/main.py
+++ b/myst_parser/config/main.py
@@ -49,6 +49,10 @@ def check_extensions(_, __, value):
if diff:
raise ValueError(f"'enable_extensions' items not recognised: {diff}")
+# should probably see if we can load the extension?
+def check_loadable_extensions(_, __, value):
+ if not isinstance(value, Iterable):
+ raise TypeError(f"'load_extensions' not iterable: {value}")
def check_sub_delimiters(_, __, value):
if (not isinstance(value, (tuple, list))) or len(value) != 2:
@@ -196,6 +200,13 @@ class MdParserConfig:
},
)
+ load_extensions: Sequence[str] = dc.field(
+ default_factory=list,
+ metadata={
+ "validator": check_loadable_extensions,
+ "help": "Load additional extensions"},
+ )
+
# Extension specific
substitutions: Dict[str, Union[str, int, float]] = dc.field(
diff --git a/myst_parser/parsers/mdit.py b/myst_parser/parsers/mdit.py
index 8476495..2077314 100644
--- a/myst_parser/parsers/mdit.py
+++ b/myst_parser/parsers/mdit.py
@@ -3,6 +3,8 @@ which creates a parser from the config.
"""
from __future__ import annotations
+from importlib import import_module
+
from typing import Callable
from markdown_it import MarkdownIt
@@ -112,6 +114,11 @@ def create_md_parser(
for name in config.disable_syntax:
md.disable(name, True)
+ for name in config.load_extensions:
+ module, plugin = name.split('/', 1)
+ mod = import_module(module)
+ md.use(getattr(mod, plugin, None))
+
md.options.update(
{
"typographer": typographer,
My
|
This is not something I'm really willing to add: the fact that myst-parser uses markdown-it-py is really an implementation detail that is not exposed to the user:
|
Obviously, if you have ideas for new/improved syntaxes, then I welcome issues here, and also in https://github.com/executablebooks/myst-spec / https://github.com/executablebooks/myst-enhancement-proposals |
In other words, is it currently not possible to use custom extensions with MyST-Parser outside of a Sphinx build? I was working following the single page builds at https://myst-parser.readthedocs.io/en/latest/docutils.html#single-page-builds, specifically the code
which raises Based on the discussion in this thread I understand that this is not supported at this time. Is my understanding correct? |
@strefli3 that's correct, as it needs a docutils sub-tree, not an html one. |
If we consider the number of plugins this project currently have It will probably never happen.
It is a very good argument to allow to adding plugins in conf.py because it will never be added as standard code IMHO: there are at lease one case for which this issue should be implemented. Will the pull request for this issue be merged, If somebody implements it? |
I'm afraid not, for the reasons already outlined |
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-emoji
plugin.Unfortunately, the
myst-parser
parsers validate against a fixed set of syntax extensions (seemyst_parser/config/main.py
), preventing customization.Proposal
Similar to how
sphinx
supports built-in extensions and 3rd-party extensions viasphinxcontrib
(and others),myst-parser
would support built-in extensions and custom extensions viamarkdown-it-py
(and/or others).In
sphinx
: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:
In
myst-parser
, the situation would be much simpler:(NOTE: Users must install the extension they wish to use so it can be imported.)
extname
tocreate_md_parser
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 inconf.py
)MarkdownIt.use()
Tasks and updates
Update
myst_parser/parsers/mdit.py::create_md_parser()
with the following:try...except
logic to import the moduleMarkdownIt.use()
Modify
myst_parser/config/main.py::check_extensions()
to:ValueError
The text was updated successfully, but these errors were encountered: