Debian Codegraph @

docs/extensions/md_in_html.md

raw
1 title: Markdown in HTML Extension
2
3 # Markdown in HTML
4
5 ## Summary
6
7 An extension that parses Markdown inside of HTML tags.
8
9 !!! Note
10
11 This extension is in __maintenance mode__. We will continue to fix bugs
12 and keep it up-to-date with the core parser, but no new features or
13 changes in behavior will be made. If you need a feature that this
14 extension does not offer, then you have three options (1) find an
15 existing [third-party extension] which meets your needs, (2) [build your
16 own extension], or (3) fork this extension (pursuant to its licensing
17 requirements) and maintain it as a third-party extension.
18
19 We recommend [PyMdown HTML] as an alternative solution.
20
21 [third-party extension]: index.md#third-party-extensions
22 [build your own extension]: api.md
23 [PyMdown HTML]: https://facelessuser.github.io/pymdown-extensions/extensions/blocks/plugins/html/
24
25 ## Syntax
26
27 By default, Markdown ignores any content within a raw HTML block-level element. With the `md-in-html` extension
28 enabled, the content of a raw HTML block-level element can be parsed as Markdown by including a `markdown` attribute
29 on the opening tag. The `markdown` attribute will be stripped from the output, while all other attributes will be
30 preserved.
31
32 The `markdown` attribute can be assigned one of three values: [`"1"`](#markdown1), [`"block"`](#block), or [`"span"`](#span).
33
34 !!! note
35
36 The expressions "block-level" and "span-level" as used in this document refer to an element's designation
37 according to the HTML specification. Whereas the `"span"` and `"block"` values assigned to the `markdown`
38 attribute refer to the Markdown parser's behavior.
39
40 ### `markdown="1"` { #markdown1 }
41
42 When the `markdown` attribute is set to `"1"`, then the parser will use the default behavior for that specific tag.
43
44 The following tags have the `block` behavior by default: `article`, `aside`, `blockquote`, `body`, `colgroup`,
45 `details`, `div`, `dl`, `fieldset`, `figcaption`, `figure`, `footer`, `form`, `group`, `header`, `hgroup`, `hr`,
46 `iframe`, `main`, `map`, `menu`, `nav`, `noscript`, `object`, `ol`, `output`, `progress`, `section`, `table`,
47 `tbody`, `tfoot`, `thead`, `tr`, `ul` and `video`.
48
49 For example, the following:
50
51 ```
52 <div markdown="1">
53 This is a *Markdown* Paragraph.
54 </div>
55 ```
56
57 ... is rendered as:
58
59 ``` html
60 <div>
61 <p>This is a <em>Markdown</em> Paragraph.</p>
62 </div>
63 ```
64
65 The following tags have the `span` behavior by default: `address`, `dd`, `dt`, `h[1-6]`, `legend`, `li`, `p`, `td`,
66 and `th`.
67
68 For example, the following:
69
70 ```
71 <p markdown="1">
72 This is not a *Markdown* Paragraph.
73 </p>
74 ```
75
76 ... is rendered as:
77
78 ``` html
79 <p>
80 This is not a <em>Markdown</em> Paragraph.
81 </p>
82 ```
83
84 ### `markdown="block"` { #block }
85
86 When the `markdown` attribute is set to `"block"`, then the parser will force the `block` behavior on the contents of
87 the element so long as it is one of the `block` or `span` tags.
88
89 The content of a `block` element is parsed into block-level content. In other words, the text is rendered as
90 paragraphs, headers, lists, blockquotes, etc. Any inline syntax within those elements is processed as well.
91
92 For example, the following:
93
94 ```
95 <section markdown="block">
96 # A header.
97
98 A *Markdown* paragraph.
99
100 * A list item.
101 * A second list item.
102
103 </section>
104 ```
105
106 ... is rendered as:
107
108 ``` html
109 <section>
110 <h1>A header.</h1>
111 <p>A <em>Markdown</em> paragraph.</p>
112 <ul>
113 <li>A list item.</li>
114 <li>A second list item.</li>
115 </ul>
116 </section>
117 ```
118
119 !!! warning
120
121 Forcing elements to be parsed as `block` elements when they are not by default could result in invalid HTML.
122 For example, one could force a `<p>` element to be nested within another `<p>` element. In most cases it is
123 recommended to use the default behavior of `markdown="1"`. Explicitly setting `markdown="block"` should be
124 reserved for advanced users who understand the HTML specification and how browsers parse and render HTML.
125
126 ### `markdown="span"` { #span }
127
128 When the `markdown` attribute is set to `"span"`, then the parser will force the `span` behavior on the contents
129 of the element so long as it is one of the `block` or `span` tags.
130
131 The content of a `span` element is not parsed into block-level content. In other words, the content will not be
132 rendered as paragraphs, headers, etc. Only inline syntax will be rendered, such as links, strong, emphasis, etc.
133
134 For example, the following:
135
136 ```
137 <div markdown="span">
138 # *Not* a header
139 </div>
140 ```
141
142 ... is rendered as:
143
144 ``` html
145 <div>
146 # <em>Not</em> a header
147 </div>
148 ```
149
150 ### Ignored Elements
151
152 The following tags are always ignored, regardless of any `markdown` attribute: `canvas`, `math`, `option`, `pre`,
153 `script`, `style`, and `textarea`. All other raw HTML tags are treated as span-level tags and are not affected by this
154 extension.
155
156 ### Nesting
157
158 When nesting multiple levels of raw HTML elements, a `markdown` attribute must be defined for each block-level
159 element. For any block-level element which does not have a `markdown` attribute, everything inside that element is
160 ignored, including child elements with `markdown` attributes.
161
162 For example, the following:
163
164 ```
165 <article id="my-article" markdown="1">
166 # Article Title
167
168 A Markdown paragraph.
169
170 <section id="section-1" markdown="1">
171 ## Section 1 Title
172
173 <p>Custom raw **HTML** which gets ignored.</p>
174
175 </section>
176
177 <section id="section-2" markdown="1">
178 ## Section 2 Title
179
180 <p markdown="1">**Markdown** content.</p>
181
182 </section>
183
184 </article>
185 ```
186
187 ... is rendered as:
188
189 ```html
190 <article id="my-article">
191 <h1>Article Title</h1>
192 <p>A Markdown paragraph.</p>
193 <section id="section-1">
194 <h2>Section 1 Title</h2>
195 <p>Custom raw **HTML** which gets ignored.</p>
196 </section>
197 <section id="section-2">
198 <h2>Section 2 Title</h2>
199 <p><strong>Markdown</strong> content.</p>
200 </section>
201 </article>
202 ```
203
204 When the value of an element's `markdown` attribute is more permissive that its parent, then the parent's stricter
205 behavior is enforced. For example, a `block` element nested within a `span` element will be parsed using the `span`
206 behavior. However, if the value of an element's `markdown` attribute is the same as, or more restrictive than, its
207 parent, the the child element's behavior is observed. For example, a `block` element may contain either `block`
208 elements or `span` elements as children and each element will be parsed using the specified behavior.
209
210 ### Tag Normalization
211
212 While the default behavior is for Markdown to not alter raw HTML, as this extension is parsing the content of raw HTML elements, it will do some normalization of the tags of block-level elements. For example, the following raw HTML:
213
214 ```
215 <div markdown="1">
216 <p markdown="1">A Markdown paragraph with *no* closing tag.
217 <p>A raw paragraph with *no* closing tag.
218 </div>
219 ```
220
221 ... is rendered as:
222
223 ``` html
224 <div>
225 <p>A Markdown paragraph with <em>no</em> closing tag.
226 </p>
227 <p>A raw paragraph with *no* closing tag.
228 </p>
229 </div>
230 ```
231
232 Notice that the parser properly recognizes that an unclosed `<p>` tag ends when another `<p>` tag begins or when the
233 parent element ends. In both cases, a closing `</p>` was added to the end of the element, regardless of whether a
234 `markdown` attribute was assigned to the element.
235
236 To avoid any normalization, an element must not be a descendant of any block-level element which has a `markdown`
237 attribute defined.
238
239 !!! warning
240
241 The normalization behavior is only documented here so that document authors are not surprised when their carefully
242 crafted raw HTML is altered by Markdown. This extension should not be relied on to normalize and generate valid
243 HTML. For the best results, always include valid raw HTML (with both opening and closing tags) in your Markdown
244 documents.
245
246 ## Usage
247
248 From the Python interpreter:
249
250 ``` pycon
251 >>> import markdown
252 >>> html = markdown.markdown(text, extensions=['md_in_html'])
253 ```