Skip to content

Commit cf93dc0

Browse files
committed
Add HTML-in-md page
1 parent 9bed08d commit cf93dc0

3 files changed

Lines changed: 40 additions & 150 deletions

File tree

docs/content/markdown/html.md

Lines changed: 27 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -3,118 +3,31 @@ title: HTML
33
icon: icons/html.svg
44
---
55

6-
Markdown is a subset of HTML; anything that cannot be expressed in Markdown can always be expressed directly with raw HTML.
7-
HTML is much less readable than plain Markdown, so you should only use it as a last resort.
6+
Markdown is a subset of HTML; anything that cannot be expressed in Markdown can always be expressed directly with raw HTML. HTML is much less readable than plain Markdown, so you should only use it as a last resort.
87

9-
Use blank lines to separate block-level HTML elements, like `<div>`, `<table>`, `<p>`, etc., from the surrounding content.
10-
11-
## Markdown in HTML
12-
13-
By default, Markdown ignores any content within a raw HTML block-level element. However, you can enable parsing of the content inside a raw HTML block-level element as Markdown by including a `markdown` attribute on the opening tag. The markdown attribute will be stripped from the output, while all other attributes will be preserved.
14-
15-
The markdown attribute can be assigned one of three values: "1", "block", or "span".
16-
17-
### `markdown="1"`
18-
19-
When the markdown attribute is set to "1", the parser will use the default behavior for that specific tag.
20-
21-
The following tags have block behavior by default: `article`, `aside`, `blockquote`, `body`, `colgroup`, `details`, `div`, `dl`, `fieldset`, `figcaption`, `figure`, `footer`, `form`, `group`, `header`, `hgroup`, `hr`, `iframe`, `main`, `map`, `menu`, `nav`, `noscript`, `object`, `ol`, `output`, `progress`, `section`, `table`, `tbody`, `tfoot`, `thead`, `tr`, `ul`, and `video`.
22-
23-
/// example | Default "block" markdown parsing
24-
25-
```md
26-
<div markdown="1">
27-
This is a *Markdown* Paragraph.
28-
</div>
29-
```
30-
31-
renders as:
32-
33-
```html
34-
<div>
35-
<p>This is a <em>Markdown</em> Paragraph.</p>
36-
</div>
37-
```
8+
The content inside HTML tags is treated differently depending if the HTML tags are inline or blocks.
389

39-
///
10+
## Inline syntax
4011

41-
The following tags have span behavior by default: `address`, `dd`, `dt`, `h[1-6]`, `legend`, `li`, `p`, `td`, and `th`.
42-
43-
/// example | Default "span" markdown parsing
12+
Only inline syntax, such as links, strong, emphasis, etc., is rendered as regular Markdown code. For example:
4413

4514
```md
46-
<p markdown="1">
47-
This is not a *Markdown* Paragraph.
48-
</p>
15+
So <span class="special">**many** _books_</span>. So little time.
4916
```
5017

5118
renders as:
5219

5320
```html
54-
<p>
55-
This is not a <em>Markdown</em> Paragraph.
56-
</p>
21+
<p>So <span class="special"><strong>>many<strong>
22+
<em>books</em></span>. So little time.<p>
5723
```
5824

59-
///
60-
61-
Note how an implicit paragraph was added in the first example but not in the second.
62-
63-
### `markdown="block"`
64-
65-
When the markdown attribute is set to "block", the parser will force block behavior on the contents of the element, as long as it is one of the block or span tags.
25+
## Block syntax
6626

67-
The content of a block element is parsed into block-level content. In other words, the text is rendered as paragraphs, headers, lists, blockquotes, etc. Any inline syntax within those elements is processed as well.
68-
69-
:::: div example
70-
**Example: Forced "block" markdown parsing**
27+
For any block-level element everything inside that element is ignored, including child elements. For example:
7128

7229
```md
73-
<section markdown="block">
74-
# A header.
75-
76-
A *Markdown* paragraph.
77-
78-
* A list item.
79-
* A second list item.
80-
81-
</section>
82-
```
83-
84-
renders as:
85-
86-
```html
87-
<section>
88-
<h1>A header.</h1>
89-
<p>A <em>Markdown</em> paragraph.</p>
90-
<ul>
91-
<li>A list item.</li>
92-
<li>A second list item.</li>
93-
</ul>
94-
</section>
95-
```
96-
97-
::::
98-
99-
::: warning
100-
Forcing elements to be parsed as `block` elements when they are not by default could result in invalid HTML.
101-
102-
For example, one could force a `<p>` element to be nested within another `<p>` element. In most cases, it is
103-
recommended to use the default behavior of `markdown="1"`.
104-
:::
105-
106-
107-
### `markdown="span"`
108-
109-
When the markdown attribute is set to "span", the parser will force span behavior on the contents of the element, as long as it is one of the block or span tags.
110-
111-
The content of a span element is not parsed into block-level content. In other words, the content will not be rendered as paragraphs, headers, etc. Only inline syntax will be rendered, such as links, strong, emphasis, etc.
112-
113-
:::: div example
114-
**Example: Forced "span" markdown parsing**
115-
116-
```md
117-
<div markdown="span">
30+
<div>
11831
# *Not* a header
11932
</div>
12033
```
@@ -127,53 +40,30 @@ renders as:
12740
</div>
12841
```
12942

130-
::::
131-
132-
133-
## Nesting
43+
Use blank lines to separate block-level HTML elements, like `<div>`, `<table>`, `<p>`, etc., from the surrounding content.
13444

135-
When nesting multiple levels of raw HTML elements, a markdown attribute must be defined for each block-level element. For any block-level element that does not have a markdown attribute, everything inside that element is ignored, including child elements with markdown attributes.
45+
### Indentation
13646

137-
:::: div example
138-
**Example: Markdown in nested HTML
47+
Block-level HTML elements must have no indentation at all. Unless they are inside a list elementy, in which case must be indented only the same as the list text.
13948

14049
```md
141-
<article id="my-article" markdown="1">
142-
# Article Title
143-
144-
A Markdown paragraph.
145-
146-
<section id="section-1" markdown="1">
147-
## Section 1 Title
148-
149-
<p>Custom raw **HTML** which gets ignored.</p>
50+
<p class="a">Not indented</p>
15051

151-
</section>
152-
153-
<section id="section-2" markdown="1">
154-
## Section 2 Title
155-
156-
<p markdown="1">**Markdown** content.</p>
157-
158-
</section>
159-
160-
</article>
52+
- lorem
53+
- ipsum
54+
<p class="b">barely indented</p>
55+
- sit amet
16156
```
16257

163-
renders as:
58+
renders (approximately) as:
16459

16560
```html
166-
<article id="my-article">
167-
<h1>Article Title</h1>
168-
<p>A Markdown paragraph.</p>
169-
<section id="section-1">
170-
<h2>Section 1 Title</h2>
171-
<p>Custom raw **HTML** which gets ignored.</p>
172-
</section>
173-
<section id="section-2">
174-
<h2>Section 2 Title</h2>
175-
<p><strong>Markdown</strong> content.</p>
176-
</section>
177-
</article>
61+
<p class="a">Not indented</p>
62+
<ul>
63+
<li>lorem</li>
64+
<li>ipsum
65+
<p class="b">barely indented</p>
66+
</li>
67+
<li>sit amet</li>
68+
</ul>
17869
```
179-
::::

docs/docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"markdown/code.md",
3131
"markdown/tables.md",
3232
"markdown/attributes.md",
33-
# "markdown/html.md",
33+
"markdown/html.md",
3434
],
3535
},
3636
{

tests/test_mdjx.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# <Test>This **is** a test</Test>
1717

1818
# <Test class="hi">Hello world</Test>
19-
# """.strip())
19+
# """.rstrip())
2020

2121
# docs = Docs(tmp_root, pages=["test.md"])
2222
# docs.catalog.add_folder(tmp_root / "comp")
@@ -27,7 +27,7 @@
2727
# <h2 >This **is** a test</h2>
2828

2929
# <h2 class="hi">Hello world</h2>
30-
# """.strip()
30+
# """
3131
# result = (tmp_root / "build" / "docs" / "test" / "index.html").read_text()
3232
# print(result)
3333
# assert result == expected
@@ -44,7 +44,7 @@
4444
# "Test": "test.jinja"
4545
# ---
4646
# <Test markdown="span" class="hi">This **is** a test</Test>
47-
# """.strip())
47+
# """.rstrip())
4848

4949
# docs = Docs(tmp_root, pages=["test.md"])
5050
# docs.catalog.add_folder(tmp_root / "comp")
@@ -53,7 +53,7 @@
5353
# expected = """
5454
# <h1>Test Page</h1>
5555
# <h2 class="hi">This <strong>is</strong> a test</h2>
56-
# """.strip()
56+
# """
5757
# result = (tmp_root / "build" / "docs" / "test" / "index.html").read_text()
5858
# print(result)
5959
# assert result == expected
@@ -70,7 +70,7 @@
7070
# "Test": "test.jinja"
7171
# ---
7272
# <Test markdown="1" class="hi">This **is** a test</Test>
73-
# """.strip())
73+
# """.rstrip())
7474

7575
# docs = Docs(tmp_root, pages=["test.md"])
7676
# docs.catalog.add_folder(tmp_root / "comp")
@@ -79,7 +79,7 @@
7979
# expected = """
8080
# <h1>Test Page</h1>
8181
# <h2 class="hi"><p>This <strong>is</strong> a test</p></h2>
82-
# """.strip()
82+
# """
8383
# result = (tmp_root / "build" / "docs" / "test" / "index.html").read_text()
8484
# print(result)
8585
# assert result == expected
@@ -96,7 +96,7 @@
9696
# "Test": "test.jinja"
9797
# ---
9898
# <Test class="hi" />
99-
# """.strip())
99+
# """.rstrip())
100100

101101
# docs = Docs(tmp_root, pages=["test.md"])
102102
# docs.catalog.add_folder(tmp_root / "comp")
@@ -105,7 +105,7 @@
105105
# expected = """
106106
# <h1>Test Page</h1>
107107
# <h2 class="hi">Hello</h2>
108-
# """.strip()
108+
# """
109109
# result = (tmp_root / "build" / "docs" / "test" / "index.html").read_text()
110110
# print(result)
111111
# assert result == expected
@@ -127,7 +127,7 @@
127127
# <Test />
128128
# <Test></Test>
129129
# ```
130-
# """.strip())
130+
# """.rstrip())
131131

132132
# docs = Docs(tmp_root, pages=["test.md"])
133133
# docs.catalog.add_folder(tmp_root / "comp")
@@ -140,7 +140,7 @@
140140
# <div class="language-text highlight"><pre><code>&lt;Test /&gt;
141141
# &lt;Test&gt;&lt;/Test&gt;
142142
# </code></pre></div>
143-
# """.strip()
143+
# """
144144
# result = (tmp_root / "build" / "docs" / "test" / "index.html").read_text()
145145
# print(result)
146146
# assert result == expected
@@ -163,7 +163,7 @@
163163
# {% if test %}Nor this {%- endif %}
164164

165165
# {# or this #}
166-
# """.strip())
166+
# """.rstrip())
167167

168168
# docs = Docs(tmp_root, pages=["test.md"])
169169
# docs.catalog.add_folder(tmp_root / "comp")
@@ -176,7 +176,7 @@
176176
# <p>{{ this is not a variable }}</p>
177177
# <p>{% if test %}Nor this {%- endif %}</p>
178178
# <p>{# or this #}</p>
179-
# """.strip()
179+
# """
180180
# result = (tmp_root / "build" / "docs" / "test" / "index.html").read_text()
181181
# print(result)
182182
# assert result == expected

0 commit comments

Comments
 (0)