Skip to content

Support code fences within comments #46

Open

Description

Often it is useful to run some code without having it display in the rendered markdown. Two such cases:

  1. Running some setup code. Yes, fixtures are a workaround but sometimes each file needs its own setup code so it doesn't make sense having a bunch of fixtures in conftest.py.
  2. Running assertions to test our code. Often, these assertions should be hidden from those reading the documentation.

I tried placing the following in my documentation but it isn't being detected?

<!--
```python continuation
assert something == something_else
```
-->

Any workarounds or fixes?

Activity

  1. freider commented on Apr 8, 2025

    @freider
    Collaborator

    I agree something like this would be very useful. Similarly being able to inline fixture definitions in a markdown file itself (but having it hidden) would be quite nice.

    The code fence is probably not being detected as a code fence in this case since it's in a comment block and just a block of text to the markdown parser, and I guess it could be ambiguous to a user if the comment should hide it from pytest-markdown-docs as well.

    To enable it, we'd have to adjust the markdown parser to do a secondary pass on content within comment blocks. Maybe this could be selectively enabled using some --markdown-docs-comments flag, in case it would break something for someone who actually wants it to remain a comment

    If you want to give it a stab I'd be happy to look at a contribution

  2. freider commented on Apr 9, 2025

    @freider
    Collaborator

    Not sure, but maybe this is possible to do via custom MarkdownIt configuration? If so, you should be able to use the new pytest_markdown_docs_markdown_it hook in v0.9.0 for this. See README.md - let us know if that works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions