Adds pytest_markdown_docs_markdown_it for custom markdownit parser - #47
Conversation
|
Thanks for the contribution! I like the pytest hook pattern for this, since the markdown flavor is probably going to be the same across a codebase anyways. |
|
|
||
|
|
||
| def extract_fence_tests( | ||
| markdown_it_parsers: typing.List["MarkdownIt"], |
There was a problem hiding this comment.
I might be missing something here, but why is this a list? (seems like we are throwing away anything except the first element anyway)
There was a problem hiding this comment.
Pytest returns a list because the hook can be defined multiple times. For example:
tests/
conftest.py <- hook can be defined here
io/
conftest.py <- same hook can be defined here
my_test.py
The default is to have a list that contains all the hooks and the nested conftest.py gets called first so they are first in the hooks list.
I simplified the code by using @pytest.hookspec(firstresult=True), which returns the first one by default: 0be6a10
Also, 5fe7f6f, which defines a default MarkdownIt parser using the hook.
There was a problem hiding this comment.
Ah I see, yeah this makes sense! Thanks for the explanation and I like the firstresult and default implementation
| or "<Unnamed obj>" | ||
| ) | ||
| fence_syntax = FenceSyntax(self.config.option.markdowndocs_syntax) | ||
| markdown_it_parsers = ( |
There was a problem hiding this comment.
Regarding the list comment above - maybe it's better to validate the hook return value here (making sure it's at most one specification, since that's what we support) and then just pass in the single MarkdownIt we want to use?
There was a problem hiding this comment.
With the other changes I don't think we need to check for anything here 👍
Issue: Fixes #31
This PR adds a
pytest_markdown_docs_markdown_itpytesthook to allow developers to customize the markdown_it parser. This means they can add plugins such asadmon_pluginto the parser.An alternative is to add something like https://github.com/modal-labs/pytest-markdown-docs/blob/main/src/pytest_markdown_docs/_runners.py, but using a
pytesthook feels a little more natural with how pytest does things. For example,pytest-xdisthas uses apytest_xdist_make_schedulerto configure a custom scheduler:https://github.com/pytest-dev/pytest-xdist/blob/f83299cd646965ac0560adddcf7204adde9f486e/src/xdist/newhooks.py#L96-L100