Help with Blocks Extension #2477
Unanswered
facelessuser
asked this question in
Q&A
Replies: 1 comment 6 replies
-
@tiangolo I want to make sure I understand what is being asked. You want to have a single file with multiple examples, but have an include block that will grab multiple examples (specified by lines) and then highlight specific lines? So, this:
Might generate:
Is that what is being asked? |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Posted originally by @tiangolo in #1973 (comment)
I'm trying to build a blocks extension but I'm not sure it's the right approach. 😅 (sorry for the long post).
I want to include multiple source file examples inline in multiple tabs in a summary, from a new block (?) with a config.
Use Case
In my docs I have some sections that include external files inline. For that I use mdx-include.
Now, in many cases, I have several versions of the same file, with old syntax for Python 3.8+ and newer syntax in 3.9 and 3.10. I also have versions using
typing.Annotated
and without it (only in the FastAPI docs for now).So, it's a lot of different Python example files for the same example (a matrix of combinations). Then I include them in tab blocks.
Here's an example from SQLModel: https://github.com/fastapi/sqlmodel/blob/1eb40b1f33aaebd02f03fd1986e242b65c79d573/docs/tutorial/fastapi/read-one.md#L17
Currently, I include some lines of the file (not the entire file). And then at the end, I include a details block with the entire file (in case people want to copy-paste or check the entire thing), I currently do this only in the SQLModel docs.
I have been wanting to refactor it, to make it much less verbose to write in Markdown and automatize including the other versions (Python 3.10, 3.8, Annotated, etc).
My idea is to have one main file shown and include only the specific lines for that file, the highlighted lines only for that file, and then have a details block below including tabs with all the versions, with the entire files.
I imagine it could be something like this:
/// include
src=docs_src/tutorial/fastapi/read_one/tutorial001
lines=1-2
lines=59-65
hl_lines=59-60 62 65
///
Then the (block?) extension would look for that source file to include, check what versions are there, include inline the highest version available of the file (e.g. Python 3.10 with Annotated), only with the lines specified, add a comment in between the included lines like
# Code here omitted 👈
, and add the highlighted lines.I tried for a few days to follow the tutorials and read all the docs for extensions with the Markdown package, the pymdown blocks docs, and the docs for
xml.etree.ElementTree
, a bit of the internal code for mdx_include and pymdownx blocks. But I have been struggling a lot to figure out how to do it all, in particular, how to modify the content, and also make it processable by the other blocks later (tab, details). 😅Maybe my whole approach is wrong, because maybe don't need a block that wraps content, only has metadata that is then used to include new content on the fly. I also see that mdx_include is a preprocessor instead of a block processor and maybe that's what I should be doing instead of trying to do it with the block processors, not really sure.
Side note, do you do any freelance work @facelessuser? Would you be willing to help me out start this? If so, please contact me on my email (I didn't find a way to reach out to you). 🤓
Beta Was this translation helpful? Give feedback.
All reactions