|
| 1 | +--- |
| 2 | +template: overrides/main.html |
| 3 | +icon: material/plus-circle |
| 4 | +--- |
| 5 | + |
| 6 | +# Annotations |
| 7 | + |
| 8 | +One of the flagship features of Material for MkDocs is the ability to inject |
| 9 | +annotations – little markers that can be added almost anywhere in a document |
| 10 | +and expand a tooltip containing arbitrary Markdown on click or keyboard focus. |
| 11 | + |
| 12 | +## Configuration |
| 13 | + |
| 14 | +This configuration allows to add annotations to all inline- and block-level |
| 15 | +elements, as well as code blocks, and nest annotations inside each other. Add |
| 16 | +the following lines to `mkdocs.yml`: |
| 17 | + |
| 18 | +``` yaml |
| 19 | +markdown_extensions: |
| 20 | + - attr_list |
| 21 | + - md_in_html |
| 22 | + - pymdownx.superfences |
| 23 | +``` |
| 24 | +
|
| 25 | +See additional configuration options: |
| 26 | +
|
| 27 | +- [Attribute Lists] |
| 28 | +- [Markdown in HTML] |
| 29 | +- [SuperFences] |
| 30 | +
|
| 31 | + [Attribute Lists]: ../setup/extensions/python-markdown.md#attribute-lists |
| 32 | + [Markdown in HTML]: ../setup/extensions/python-markdown.md#markdown-in-html |
| 33 | + [SuperFences]: ../setup/extensions/python-markdown-extensions.md#superfences |
| 34 | +
|
| 35 | +## Usage |
| 36 | +
|
| 37 | +### Using annotations |
| 38 | +
|
| 39 | +[:octicons-heart-fill-24:{ .mdx-heart } Insiders][Insiders]{ .mdx-insiders } · |
| 40 | +[:octicons-tag-24: insiders-4.6.0][Insiders] · |
| 41 | +:octicons-beaker-24: Experimental |
| 42 | +
|
| 43 | +Annotations consist of two parts: a marker, which can be placed anywhere in |
| 44 | +a block marked with the `annotate` class, and content located in a list below |
| 45 | +the block containing the marker: |
| 46 | + |
| 47 | +``` markdown title="Text with annotations" |
| 48 | +Lorem ipsum dolor sit amet, (1) consectetur adipiscing elit. |
| 49 | +{ .annotate } |
| 50 | +
|
| 51 | +1. :man_raising_hand: I'm an annotation! I can contain `code`, __formatted |
| 52 | + text__, images, ... basically anything that can be expressed in Markdown. |
| 53 | +``` |
| 54 | + |
| 55 | +<div class="result" markdown> |
| 56 | + |
| 57 | +Lorem ipsum dolor sit amet, (1) consectetur adipiscing elit. |
| 58 | +{ .annotate } |
| 59 | + |
| 60 | +1. :man_raising_hand: I'm an annotation! I can contain `code`, __formatted |
| 61 | + text__, images, ... basically anything that can be written in Markdown. |
| 62 | + |
| 63 | +</div> |
| 64 | + |
| 65 | +Note that the `annotate` class must only be added to the outermost block. All |
| 66 | +nested elements can use the same list to define annotations, except when |
| 67 | +annotations are nested themselves. |
| 68 | + |
| 69 | + [Insiders]: ../insiders/index.md |
| 70 | + |
| 71 | +#### in annotations |
| 72 | + |
| 73 | +When [SuperFences] is enabled, annotations can be nested inside annotations by |
| 74 | +adding the `annotate` class to the list item hosting the annotation content, |
| 75 | +repeating the process: |
| 76 | + |
| 77 | +``` markdown title="Text with nested annotations" |
| 78 | +Lorem ipsum dolor sit amet, (1) consectetur adipiscing elit. |
| 79 | +{ .annotate } |
| 80 | + |
| 81 | +1. :man_raising_hand: I'm an annotation! (1) |
| 82 | + { .annotate } |
| 83 | + |
| 84 | + 1. :woman_raising_hand: I'm an annotation as well! |
| 85 | +``` |
| 86 | + |
| 87 | +<div class="result" markdown> |
| 88 | + |
| 89 | +Lorem ipsum dolor sit amet, (1) consectetur adipiscing elit. |
| 90 | +{ .annotate } |
| 91 | + |
| 92 | +1. :man_raising_hand: I'm an annotation! (1) |
| 93 | + { .annotate style="margin-bottom: 0" } |
| 94 | + |
| 95 | + 1. :woman_raising_hand: I'm an annotation as well! |
| 96 | + |
| 97 | +</div> |
| 98 | + |
| 99 | +#### in admonitions |
| 100 | + |
| 101 | +The titles and bodies of [admonitions] can also host annotations by adding the |
| 102 | +`annotate` modifier after the type qualifier, which is similar to how |
| 103 | +[inline blocks] work: |
| 104 | + |
| 105 | +``` markdown title="Admonition with annotations" |
| 106 | +!!! note annotate "Phasellus posuere in sem ut cursus (1)" |
| 107 | + |
| 108 | + Lorem ipsum dolor sit amet, (2) consectetur adipiscing elit. Nulla et |
| 109 | + euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo |
| 110 | + purus auctor massa, nec semper lorem quam in massa. |
| 111 | + |
| 112 | +1. :man_raising_hand: I'm an annotation! |
| 113 | +2. :woman_raising_hand: I'm an annotation as well! |
| 114 | +``` |
| 115 | + |
| 116 | +<div class="result" markdown> |
| 117 | + |
| 118 | +!!! note annotate "Phasellus posuere in sem ut cursus (1)" |
| 119 | + |
| 120 | + Lorem ipsum dolor sit amet, (2) consectetur adipiscing elit. Nulla et |
| 121 | + euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo |
| 122 | + purus auctor massa, nec semper lorem quam in massa. |
| 123 | + |
| 124 | +1. :man_raising_hand: I'm an annotation! |
| 125 | +2. :woman_raising_hand: I'm an annotation as well! |
| 126 | + |
| 127 | +</div> |
| 128 | + |
| 129 | + [admonitions]: admonitions.md |
| 130 | + [inline blocks]: admonitions.md#inline-blocks |
| 131 | + |
| 132 | +#### in content tabs |
| 133 | + |
| 134 | +Content tabs can host annotations by adding the `annotate` class to the block |
| 135 | +of a dedicated content tab (and not to the container, which is not supported): |
| 136 | + |
| 137 | +``` markdown title="Content tabs with annotations" |
| 138 | +=== "Tab 1" |
| 139 | + |
| 140 | + Lorem ipsum dolor sit amet, (1) consectetur adipiscing elit. |
| 141 | + { .annotate } |
| 142 | + |
| 143 | + 1. :man_raising_hand: I'm an annotation! |
| 144 | + |
| 145 | +=== "Tab 2" |
| 146 | + |
| 147 | + Phasellus posuere in sem ut cursus (1) |
| 148 | + { .annotate } |
| 149 | + |
| 150 | + 1. :woman_raising_hand: I'm an annotation as well! |
| 151 | +``` |
| 152 | + |
| 153 | +<div class="result" markdown> |
| 154 | + |
| 155 | +=== "Tab 1" |
| 156 | + |
| 157 | + Lorem ipsum dolor sit amet, (1) consectetur adipiscing elit. |
| 158 | + { .annotate } |
| 159 | + |
| 160 | + 1. :man_raising_hand: I'm an annotation! |
| 161 | + |
| 162 | +=== "Tab 2" |
| 163 | + |
| 164 | + Phasellus posuere in sem ut cursus (1) |
| 165 | + { .annotate } |
| 166 | + |
| 167 | + 1. :woman_raising_hand: I'm an annotation as well! |
| 168 | + |
| 169 | +</div> |
| 170 | + |
| 171 | +#### in everything else |
| 172 | + |
| 173 | +The [Attribute Lists] extension is the key ingredient for adding annotations to |
| 174 | +most elements, but it has some [limitations]. However, it's always possible to |
| 175 | +leverage the [Markdown in HTML] extension to wrap arbitrary elements with a |
| 176 | +`div` with the `annotate` class: |
| 177 | + |
| 178 | +```` html title="HTML with annotations" |
| 179 | +<div class="annotate" markdown> |
| 180 | + |
| 181 | +> Lorem ipsum dolor sit amet, (1) consectetur adipiscing elit. |
| 182 | + |
| 183 | +</div> |
| 184 | + |
| 185 | +1. :man_raising_hand: I'm an annotation! |
| 186 | +```` |
| 187 | + |
| 188 | +<div class="result" markdown> |
| 189 | + <div class="annotate" markdown> |
| 190 | + |
| 191 | +> Lorem ipsum dolor sit amet, (1) consectetur adipiscing elit. |
| 192 | +
|
| 193 | + </div> |
| 194 | + |
| 195 | +1. :man_raising_hand: I'm an annotation! |
| 196 | + |
| 197 | +</div> |
| 198 | + |
| 199 | +With this trick, annotations can also be added to blockquotes, data tables, |
| 200 | +lists, and many other elements that are not supported by the [Attribute Lists] |
| 201 | +extension. Furthermore, note that [code blocks follow different semantics]. |
| 202 | + |
| 203 | + [limitations]: https://python-markdown.github.io/extensions/attr_list/#limitations |
| 204 | + [code blocks follow different semantics]: code-blocks.md#adding-annotations |
0 commit comments