Skip to content
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

Allow showing only a range of lines for code blocks #6353

Closed
4 tasks done
llucax opened this issue Nov 17, 2023 · 3 comments
Closed
4 tasks done

Allow showing only a range of lines for code blocks #6353

llucax opened this issue Nov 17, 2023 · 3 comments
Labels
change request Issue requests a new feature or improvement upstream Issue must be taken upstream

Comments

@llucax
Copy link

llucax commented Nov 17, 2023

Context

I'm using mkdocstrings and Sybil to extract code examples in the documentation and test by running pylint on them via pytest.

This forces me to sometimes add too much boilerplate to examples, which is distracting from the main idea I want to show in an example, but it is also really important that examples stay up to date and (mostly) working.

An alternative would be to move the examples to separate files and use the snippet extension line selection feature, but I want to keep the examples in the docstrings, so they are also accessible in code editors, as python include all the docs in the code itself.

Description

I would like code blocks to accept selecting which lines to show, similar to what the snippet extension or the highlight specific lines feature does. For example:

Example:
    ``` py show_lines="4:"
    # Bolierplate setup code
    # More boiler plate setup code
    # And some more
    this is the_juicy_part
    ```

I'm suggesting the snippet extension syntax (with :) instead of the highlight specific lines feature syntax because it is more natural for specifying open-ended ranges.

Related links

Related discussions:

Extra:

Use Cases

Being able to write examples that are verifiable using linting tools to ensure they stay up to date, but still only being able to show the relevant parts that make sense for the documentation, avoiding distracting, boilerplate code.

Visuals

It should look like a regular code block, just with less lines than the original code block.

Stretch goal: There could be some button to expand the example and show also the boilerplate code, so users could copy&paste the whole example and run it if they want.

Before submitting

@squidfunk
Copy link
Owner

Thanks for suggesting! Please propose this to Python Markdown Extensions, as this is the library that converts code blocks into Markdown ☺️ Material for MkDocs only renders what it gets.

@squidfunk squidfunk added change request Issue requests a new feature or improvement upstream Issue must be taken upstream labels Nov 17, 2023
@llucax
Copy link
Author

llucax commented Nov 17, 2023

OK, I will do that, thanks. One question though, if the "button to expand the boilerplate option" would get implemented, would that need support from Material, or it would still be all part of pymdown-extensions?

@squidfunk
Copy link
Owner

Maybe, depends on the outcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
change request Issue requests a new feature or improvement upstream Issue must be taken upstream
Projects
None yet
Development

No branches or pull requests

2 participants