| 1 |
--- |
| 2 |
icon: lucide/cuboid |
| 3 |
--- |
| 4 |
[:octicons-file-code-24:][_blocks]{: .source-link } |
| 5 |
|
| 6 |
# Blocks |
| 7 |
|
| 8 |
## Overview |
| 9 |
|
| 10 |
Blocks is an extension aimed at providing generic blocks inspired by reStructuredText directives. While inspired by |
| 11 |
directives, generic blocks are not meant to behave or mirror directives as a 1:1 solution. |
| 12 |
|
| 13 |
The idea behind blocks is to solve a few issues that have existed within Python Markdown block extensions. |
| 14 |
|
| 15 |
1. Markdown has numerous special syntaxes to define various elements, but as people try to add numerous extensions to |
| 16 |
perform specialized translations, it can become difficult to continuously come up with new, sensible syntax that does |
| 17 |
not conflict with some other extension that is desired by users. |
| 18 |
|
| 19 |
2. Traditionally, Python Markdown has implemented any block processors that behave more as containers for multiple child |
| 20 |
blocks using an indentation format (think Admonitions as an example). While this works, this can be tiring to some |
| 21 |
authors who'd like to not have so many nested indentation levels in the documentation. Additionally, most code |
| 22 |
editors will syntax highlight such nested constructs as indented code blocks which can make reading the source |
| 23 |
difficult. |
| 24 |
|
| 25 |
Blocks is an extension type that essentially allows for the creation of its own extension type that function as fenced |
| 26 |
containers. Each extension can do whatever it wants with the content inside the container and allow for a single generic |
| 27 |
format to create as many different block style extensions as desired. As the blocks are fenced, indentation is not |
| 28 |
required to determine the start and end of the blocks. |
| 29 |
|
| 30 |
Blocks also allows for per block options giving the extension authors an easy way to extend functionality and users an |
| 31 |
easy, predictable way to specify such options. |
| 32 |
|
| 33 |
Blocks itself isn't used directly, but there are a variety of extensions created with it. All of which can be registered |
| 34 |
in the traditional way. |
| 35 |
|
| 36 |
```py3 |
| 37 |
import markdown |
| 38 |
md = markdown.Markdown(extensions=['pymdownx.blocks.<extension>']) |
| 39 |
``` |
| 40 |
|
| 41 |
Pymdown Extensions provides the following extensions. |
| 42 |
|
| 43 |
Extension | Description |
| 44 |
--------------------------------------- | ----------- |
| 45 |
[`admonition`](./plugins/admonition.md) | The admonition block allows for the creation of admonitions. |
| 46 |
[`define`](./plugins/definition.md) | Allows for the creation of definition lists. |
| 47 |
[`details`](./plugins/details.md) | The details block allows for the creation of collapsible details/summary constructs. |
| 48 |
[`tab`](./plugins/tab.md) | Aims to replace the [Tabbed](../tabbed.md) extension and allows for the creation of tab containers. |
| 49 |
[`html`](./plugins/html.md) | HTML is a block that allows for the arbitrary creation of HTML elements of various types. |
| 50 |
|
| 51 |
## Syntax |
| 52 |
|
| 53 |
Syntax for Blocks requires the desired content to be included between fences. Fences are denoted by using three or |
| 54 |
more forward slashes. The opening fence must specify the name of the block to invoke it. |
| 55 |
|
| 56 |
``` |
| 57 |
/// name-of-block |
| 58 |
content |
| 59 |
/// |
| 60 |
``` |
| 61 |
|
| 62 |
/// tip | Content and New Lines |
| 63 |
While there is no hard rule stating that the first content block must have a new line after the header, it should be |
| 64 |
noted that some special content blocks may require an empty line before them, this may simply be due to how they are |
| 65 |
implemented. Simple paragraphs should not require an empty new line before them, but we cannot make a blanket |
| 66 |
statement about all blocks. If in doubt, use an empty line before the first content block. |
| 67 |
/// |
| 68 |
|
| 69 |
Some blocks may implement a special argument in the header for things such as, but not limited to, titles. This |
| 70 |
argument can be optional or sometimes enforced as a requirement. This is up to the given Blocks extension to decide. |
| 71 |
|
| 72 |
``` |
| 73 |
/// note | Did you know? |
| 74 |
You can create a note with Blocks! |
| 75 |
/// |
| 76 |
``` |
| 77 |
|
| 78 |
//// html | div.result |
| 79 |
/// note | Did you know? |
| 80 |
You can create a note with Blocks! |
| 81 |
/// |
| 82 |
//// |
| 83 |
|
| 84 |
Lastly, a given Block extension may allow for additional options that don't make sense in the first line declaration. |
| 85 |
This may be because they are rarely used, more complicated, or just make the first line signature more confusing. These |
| 86 |
options are per block specific use a YAML syntax. They must be part of the header, which means no new line between the |
| 87 |
block declaration and the options or between individual options. The options also must be indented at least four spaces. |
| 88 |
|
| 89 |
For instance, all extensions inherit an option `attrs` which allows you to set HTML attributes to the outer element of a |
| 90 |
generic block. |
| 91 |
|
| 92 |
``` |
| 93 |
/// html | div |
| 94 |
attrs: {style: 'font-size: xx-large'} |
| 95 |
|
| 96 |
Some content. |
| 97 |
/// |
| 98 |
``` |
| 99 |
|
| 100 |
//// html | div.result |
| 101 |
/// html | div |
| 102 |
attrs: {style: 'font-size: xx-large'} |
| 103 |
|
| 104 |
Some content. |
| 105 |
/// |
| 106 |
//// |
| 107 |
|
| 108 |
Some blocks may take raw content (and should note this in their documentation) which will avoid further Markdown |
| 109 |
processing on the content. This is done by requiring the content to be an indented code block. Due to the way Python |
| 110 |
Markdown works, these content blocks must be indented to avoid having the HTML processor from altering content. Raw |
| 111 |
blocks cannot shield content from all preprocessor transformations, but by requiring the content to be indented code |
| 112 |
blocks, the content will survive any alterations that a traditional code block would survive. |
| 113 |
|
| 114 |
``` |
| 115 |
/// html | pre |
| 116 |
|
| 117 |
Pre blocks are _raw_. |
| 118 |
Additional Markdown parsing is *avoided*. |
| 119 |
Content should be indented. |
| 120 |
/// |
| 121 |
``` |
| 122 |
|
| 123 |
//// html | div.result |
| 124 |
/// html | pre |
| 125 |
|
| 126 |
Pre blocks are _raw_. |
| 127 |
Additional Markdown parsing is *avoided*. |
| 128 |
Content should be indented. |
| 129 |
/// |
| 130 |
//// |
| 131 |
|
| 132 |
/// tip | Indented Content |
| 133 |
Indented content should always be separated from the block header by one empty line so that it is not confused as a YAML |
| 134 |
option block. |
| 135 |
/// |
| 136 |
|
| 137 |
## Nesting |
| 138 |
|
| 139 |
Generic blocks can be nested as long as the block fence differs in number of leading tokens. This is similar to how |
| 140 |
fenced code blocks work. The minimum requirement is that at least three tokens are used. |
| 141 |
|
| 142 |
``` |
| 143 |
//// note | Some title |
| 144 |
/// details | Summary |
| 145 |
type: warning |
| 146 |
content |
| 147 |
/// |
| 148 |
Content |
| 149 |
//// |
| 150 |
``` |
| 151 |
|
| 152 |
///// html | div.result |
| 153 |
//// note | Some title |
| 154 |
/// details | Summary |
| 155 |
type: warning |
| 156 |
content |
| 157 |
/// |
| 158 |
|
| 159 |
Content |
| 160 |
//// |
| 161 |
///// |