| 1 |
--- |
| 2 |
icon: lucide/code-xml |
| 3 |
--- |
| 4 |
[:octicons-file-code-24:][_html_block]{: .source-link } |
| 5 |
|
| 6 |
# HTML |
| 7 |
|
| 8 |
## Overview |
| 9 |
|
| 10 |
The HTML block allows a user to wrap Markdown in arbitrary HTML elements. |
| 11 |
|
| 12 |
By default, the meta-plugin is registered when `pymdownx.blocks` is registered, but if you were customizing which |
| 13 |
meta-plugins get loaded, you can do so by doing the following: |
| 14 |
|
| 15 |
```py3 |
| 16 |
import markdown |
| 17 |
from pymdownx.blocks.html import HTML |
| 18 |
md = markdown.Markdown(extensions=['pymdownx.blocks.html']) |
| 19 |
``` |
| 20 |
|
| 21 |
## Usage |
| 22 |
|
| 23 |
Generally, HTML blocks can be defined simply by specifying the `html` generic block type followed by the tag name. |
| 24 |
|
| 25 |
```text title="HTML" |
| 26 |
/// html | div |
| 27 |
some *markdown* content |
| 28 |
/// |
| 29 |
``` |
| 30 |
|
| 31 |
/// html | div.result |
| 32 |
//// html | div |
| 33 |
some *markdown* content |
| 34 |
//// |
| 35 |
/// |
| 36 |
|
| 37 |
Classes and IDs and arbitrary attributes can be specified by using selector-like notation: |
| 38 |
|
| 39 |
- `#!css div.my-class` will add a class of name `my-class` to a tag of name `div`. |
| 40 |
- `#!css div#some-id` will add an ID of name `some-id` to a tag of name `div`. |
| 41 |
- `#!css div[attr]` will add an attribute of name `attr` to a tag of name `div`. You can also add values |
| 42 |
(`#!css div[attr=value]`), |
| 43 |
quoted values (`#!css div[attr="quoted value"]`), and even multiple values (`#!css div[attr1=value attr2="quoted value"]`). |
| 44 |
- Attributes can also be chained: `#!css div#some-id.class[attr=value]`. |
| 45 |
|
| 46 |
```text title="HTML" |
| 47 |
/// html | div[style='border: 1px solid red;'] |
| 48 |
some *markdown* content |
| 49 |
/// |
| 50 |
``` |
| 51 |
|
| 52 |
/// html | div.result |
| 53 |
//// html | div[style='border: 1px solid red;'] |
| 54 |
some *markdown* content |
| 55 |
//// |
| 56 |
/// |
| 57 |
|
| 58 |
By default HTML blocks will automatically have the content rendering determined from tag name, so `div` blocks will be |
| 59 |
treated as block elements, `span` will be treated as inline elements, and things like `pre` will treat the content as |
| 60 |
raw text that needs HTML escaping, and things like `script` will be treated as raw content does not need HTML escaping. |
| 61 |
With that said, there may be cases where an HTML element isn't properly recognized yet, or the user simply wants to |
| 62 |
control how the element processes its content, in these cases, the `markdown` option can be used to specify how Markdown |
| 63 |
content is handled. |
| 64 |
|
| 65 |
Markdown\ Modes | Description |
| 66 |
--------------- | ----------- |
| 67 |
`block` | Parsed block content will be handled by the Markdown parser as content under a block element. |
| 68 |
`inline` | Parsed block content will be handled by the Markdown parser as content under an inline element. |
| 69 |
`raw` | Parsed block content will be preserved. No additional Markdown parsing will be applied. Content will be HTML escaped to preserve the content as is. |
| 70 |
`auto` | Depending on whether the wrapping parent is a block element, inline element, or something like a code element, Blocks will choose the best approach for the content. Decision is made based on the element returned by the [`on_add` event](../api.md#on_add-event). |
| 71 |
`html` | Like `raw`, content will be preserved, but the content will _not_ be HTML escaped and will be passed through as unmodified HTML. Any required sanitizing should be provided by the user post Markdown processing. |
| 72 |
|
| 73 |
/// tip | Raw and HTML Mode |
| 74 |
When using _raw_ tags or forcing _raw_ mode with `markdown: raw` (HTML escaped) or `markdown: html` (no HTML escaping), |
| 75 |
code must be indented. This is because Python Markdown will look for and process raw HTML in non indented blocks. The |
| 76 |
only avoid this is to use indented code blocks. If content is not indented, the content may be missing at the end. |
| 77 |
|
| 78 |
Recognized raw block tags: `canvas`, `math`, `option`, `pre`, and `textarea`. |
| 79 |
|
| 80 |
Recognized raw HTML tags: `script` and `style`. |
| 81 |
|
| 82 |
Also, make sure to have a new line before indented content so it is not recognized as an attempt to specify YAML |
| 83 |
options. |
| 84 |
/// |
| 85 |
|
| 86 |
In the following example we force `pre` to handle content as Markdown block content instead of the usual raw content |
| 87 |
default. |
| 88 |
|
| 89 |
```text title="Pre as Block" |
| 90 |
/// html | pre |
| 91 |
|
| 92 |
some *markdown* content |
| 93 |
/// |
| 94 |
|
| 95 |
/// html | pre |
| 96 |
markdown: block |
| 97 |
|
| 98 |
some *markdown* content |
| 99 |
/// |
| 100 |
``` |
| 101 |
|
| 102 |
/// html | div.result |
| 103 |
//// html | pre |
| 104 |
|
| 105 |
some *markdown* content |
| 106 |
//// |
| 107 |
|
| 108 |
//// html | pre |
| 109 |
markdown: block |
| 110 |
|
| 111 |
some *markdown* content |
| 112 |
//// |
| 113 |
/// |
| 114 |
|
| 115 |
## Custom Tag Handling |
| 116 |
|
| 117 |
It is possible that in the future new tags could be added to the HTML spec, and it is also possible that the extension |
| 118 |
may not recognize such tags for some length of time. While such tags can be added in future releases, the HTML extension |
| 119 |
allows for users to specify such tags to navigate around such issues, or even make up their own tags. |
| 120 |
|
| 121 |
To teach the HTML extension about unknown tags, simply use the `custom` [option](#global-options) to specify a tag name |
| 122 |
and the assumed default `mode`. |
| 123 |
|
| 124 |
```py3 |
| 125 |
import markdown |
| 126 |
from pymdownx.blocks.html import HTML |
| 127 |
extensions = ['pymdownx.blocks.html'] |
| 128 |
extension_configs = { |
| 129 |
'pymdown.blocks.html': { |
| 130 |
'custom': [ |
| 131 |
{'tag': 'sometag', 'mode': 'block'} |
| 132 |
] |
| 133 |
} |
| 134 |
} |
| 135 |
md = markdown.Markdown(extensions=extensions, extension_configs=extension_configs) |
| 136 |
``` |
| 137 |
|
| 138 |
Then you can use your custom tag knowing it will be handled properly. |
| 139 |
|
| 140 |
``` |
| 141 |
/// html | sometag |
| 142 |
|
| 143 |
Some block content. |
| 144 |
|
| 145 |
- List item A. |
| 146 |
|
| 147 |
- List item B. |
| 148 |
/// |
| 149 |
|
| 150 |
``` |
| 151 |
|
| 152 |
## Global Options |
| 153 |
|
| 154 |
Options | Type | Descriptions |
| 155 |
-------- | --------------- | ------------ |
| 156 |
`custom` | \[dictionary\] | Specify custom tags with assumed default handling. |
| 157 |
|
| 158 |
## Per Block Options |
| 159 |
|
| 160 |
Options | Type | Descriptions |
| 161 |
------------ | ---------- | ------------ |
| 162 |
`markdown` | string | String value to control how Markdown content is processed. Valid options are: `auto`, `block`, `inline`, `html`, and `raw`. |
| 163 |
`attrs` | dictionary | A dictionary that defines attributes for the outer, wrapper element. |