modal-labs / pytest-markdown-docs

Run pytest on markdown code fence blocks
MIT License
57 stars 7 forks source link

Additional markers like `notest` are not compatible with MkDocs #23

Open RafaelWO opened 8 months ago

RafaelWO commented 8 months ago

Hi,

first of all, thanks for your work on this project! I was really happy when I realized that I could test the code snippets in my docs :)

Unfortunately, I noticed that specifying additional verbs like notest in a code fence, e.g.

```python notest
foobar = "baz"

results in detection errors of code blocks when using [Material for MkDocs](https://squidfunk.github.io/mkdocs-material/):

![grafik](https://github.com/modal-labs/pytest-markdown-docs/assets/38643099/8e0426af-d379-4e27-a7dd-23ae8d2e3d52)

<details><summary>This is how it looks without `notest`</summary>
<p>

![grafik](https://github.com/modal-labs/pytest-markdown-docs/assets/38643099/a2ce22d0-b006-49a5-990d-78673f8aad44)

</p>
</details> 

A possible workaround would be allowing users to specify `notest` (or fixtures) within HTML comments above the code block, e.g.
foobar = "baz"


If this makes sense for you, I'm happy to give it a try in a PR :)
freider commented 8 months ago

Oh that's unfortunate! The renderers/parsers I've previously looked at has happily accepted additional data after the language spec.

notest are not the only "info" markers - we also use them for fixture injection and continuation blocks (fixture:blah)

If it doesn't complicate the parsing code we could certainly allow for a separate "comment" syntax, maybe something like: <!-- pmd-metadata: notest --> (good to have some sort of indicator that it is metadata.

According to this thread https://stackoverflow.com/questions/4823468/comments-in-markdown there doesn't seem to be an obvious "right" way to do comments in markdown, as some html renderers will actually render these comments as well. Some suggest abusing markdown link syntax instead. Anyway, happy to look at a PR if you can think of a clean way of implementing this :)

(I guess another option for you would be to patch mkdocs-material's parser to not break when looking at code blocks with multiple info fragments)

avandierast commented 6 months ago

Hello :)

I've found another solution. The extension PyMdown Superfences for Mkdocs has a special format using braces that can contain any arbitrary parameters: https://facelessuser.github.io/pymdown-extensions/extensions/superfences/#injecting-classes-ids-and-attributes

The only thing to do is to make this library compatible with the brace format and here is the PR: https://github.com/modal-labs/pytest-markdown-docs/pull/26.