|
| 1 | +# markdown-include-variants |
| 2 | + |
| 3 | +<a href="https://github.com/tiangolo/markdown-include-variants/actions?query=workflow%3ATest" target="_blank"> |
| 4 | + <img src="https://github.com/tiangolo/markdown-include-variants/workflows/Test/badge.svg" alt="Test"> |
| 5 | +</a> |
| 6 | +<a href="https://github.com/tiangolo/markdown-include-variants/actions?query=workflow%3APublish" target="_blank"> |
| 7 | + <img src="https://github.com/tiangolo/markdown-include-variants/workflows/Publish/badge.svg" alt="Publish"> |
| 8 | +</a> |
| 9 | +<a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/tiangolo/markdown-include-variants" target="_blank"> |
| 10 | + <img src="https://coverage-badge.samuelcolvin.workers.dev/tiangolo/markdown-include-variants.svg" alt="Coverage"> |
| 11 | +<a href="https://pypi.org/project/markdown-include-variants" target="_blank"> |
| 12 | + <img src="https://img.shields.io/pypi/v/markdown-include-variants?color=%2334D058&label=pypi%20package" alt="Package version"> |
| 13 | +</a> |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 🚨 Warning: Internal Project |
| 18 | + |
| 19 | +This is an internal project, it is mainly useful for the docs in [@tiangolo](https://github.com/tiangolo)'s projects (e.g. all the [FastAPI](https://github.com/fastapi) projects). |
| 20 | + |
| 21 | +It is probably **not useful** to you. You should probably not use it. 😅 |
| 22 | + |
| 23 | +There are no guarantees about behavior, it is made to suit the needs of these projects, to simplify writing documentation. |
| 24 | + |
| 25 | +## How to Use |
| 26 | + |
| 27 | +If you're still here it's probably because you are getting involved in one of the projects that use it. 🤓 |
| 28 | + |
| 29 | +Here's how it works: |
| 30 | + |
| 31 | +* You add a Python source example to the `docs_src` directory, with the minimum Python version supported by the project, and using the old format without `Annotated`, if that applies, it would be named something like `tutorial001.py`. |
| 32 | +* Copy the file and update it to use `Annotated` (if that applies), and name the file with a "tag" (a prefix) of `an`, like: `tutorial001_an.py`. |
| 33 | +* Run the internal script to generate the variants of the files for superior versions of Python, like `3.9`, `3.10`, etc. (this internal script is in the project itself). This would generate files like `tutorial001_py39.py`, `tutorial001_py310.py`, etc. and `tutorial001_an_py39.py`, `tutorial001_an_py310.py`, etc. |
| 34 | +* In the Markdown file, include the simplest version of the file, using this syntax: |
| 35 | + |
| 36 | +```markdown |
| 37 | +{* ./docs_src/first_steps/tutorial001.py *} |
| 38 | +``` |
| 39 | + |
| 40 | +That will be automatically expanded with [mdx-include](https://github.com/neurobin/mdx_include) to include the recommended version of the file (the highest version of Python, using `Annotated`), and will include the variants below in a collapsed `details` box. |
| 41 | + |
| 42 | +### Include Lines |
| 43 | + |
| 44 | +To include specific line ranges, use the config `ln`: |
| 45 | + |
| 46 | +```markdown |
| 47 | +{* ./docs_src/first_steps/tutorial001.py ln[3:6,8,10:11] *} |
| 48 | +``` |
| 49 | + |
| 50 | +That will include only: |
| 51 | + |
| 52 | +* lines 3 to 6 |
| 53 | +* line 8 |
| 54 | +* lines 10 to 11 |
| 55 | + |
| 56 | +It will add blocks with comments in the middle like: |
| 57 | + |
| 58 | +```python |
| 59 | +# Code here omitted 👈️ |
| 60 | +``` |
| 61 | + |
| 62 | +If you include specific lines, it will also add another collapsed `details` box with the "Full file preview". |
| 63 | + |
| 64 | +### Highlight Lines |
| 65 | + |
| 66 | +You can highlight specific lines using the config `hl`: |
| 67 | + |
| 68 | +```markdown |
| 69 | +{* ./docs_src/first_steps/tutorial001.py hl[3,5:7,10] *} |
| 70 | +``` |
| 71 | + |
| 72 | +That will highlight: |
| 73 | + |
| 74 | +* line 3 |
| 75 | +* lines 5 to 7 |
| 76 | +* line 10 |
| 77 | + |
| 78 | +Have in mind that the file path points to the simplest version of the file, the one without the `_an` suffix. But the main file shown will be the highest (recommended) version, and the highlights will apply to that file. |
| 79 | + |
| 80 | +So, when deciding which lines to highlight, do that based on the highest version of the file. |
| 81 | + |
| 82 | +### Include Lines and Highlight |
| 83 | + |
| 84 | +You can combine both `ln` and `hl`: |
| 85 | + |
| 86 | +```markdown |
| 87 | +{* ./docs_src/first_steps/tutorial001.py ln[3:6,8,10:11] hl[3,5:6,10] *} |
| 88 | +``` |
| 89 | + |
| 90 | +The highlighted lines refer to the source file (for the highest/recommended version), not the final rendered code block. |
| 91 | + |
| 92 | +This makes it easier to decide which lines to highlight just by opening the source file, without having to calculate the actual lines in the rendered block after included, the extra lines for comments when omitting lines, etc. |
| 93 | + |
| 94 | +### Include Lines and Highlight - Example |
| 95 | + |
| 96 | +For example, you could have a source file with: |
| 97 | + |
| 98 | +```python |
| 99 | +print("line 1") |
| 100 | +print("line 2") |
| 101 | +print("line 3") |
| 102 | +print("line 4") |
| 103 | +print("line 5") |
| 104 | +print("line 6") # highlight this |
| 105 | +print("line 7") |
| 106 | +``` |
| 107 | + |
| 108 | +Using a declaration like this: |
| 109 | + |
| 110 | +```markdown |
| 111 | +{* ./docs_src/first_steps/tutorial001.py ln[5:7] hl[6] *} |
| 112 | +``` |
| 113 | + |
| 114 | +Could render something like: |
| 115 | + |
| 116 | +```python |
| 117 | +# Code above omitted 👈️ |
| 118 | + |
| 119 | +print("line 5") |
| 120 | +print("line 6") # highlight this |
| 121 | +print("line 7") |
| 122 | +``` |
| 123 | + |
| 124 | +Notice that the comment above adds 2 extra lines, and only the desired lines are included, the result is that the actual highlighted line is the rendered line 4, but the source line 6, it's all calculated automatically. 🤓 |
| 125 | + |
| 126 | +## License |
| 127 | + |
| 128 | +This project is licensed under the terms of the [MIT license](https://github.com/tiangolo/markdown-include-variants/blob/main/LICENSE). |
0 commit comments