Skip to content

Commit 16e4e04

Browse files
authored
👌 IMPROVE: Admonition structure (#12)
* Change paragraph tag to a `header` (semantic HTML) * Add `block: true` so that the output HTML is more readable * Remove the `admonition-body` div. This was not used in the CSS, and is not based on the current output from sphinx (and current themes) * This simplifies the code and doesn't introduce another CSS selector * Make the CSS less specific about the header selector (remove the paragraph requirement) * Remove space in class names for admonitions * Update the tests, ensure that the CSS is visually similar to previous
1 parent ffaa12a commit 16e4e04

File tree

5 files changed

+26
-20
lines changed

5 files changed

+26
-20
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tests/fixtures/*.md

docs/index.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ <h1>markdown-it-docutils</h1>
3232
</p>
3333
<p>
3434
Simply write in the text box below, then click away, and the text will be
35-
rendered.<br />Note the color scheme is adaptive to browser settings,
36-
see
35+
rendered.<br />Note the color scheme is adaptive to browser settings, see
3736
<a
3837
href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme"
3938
>prefers-color-scheme</a

src/directives/admonitions.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ class BaseAdmonition extends Directive {
2323

2424
// we create an overall container, then individual containers for the title and body
2525

26-
const adToken = this.createToken("admonition_open", "aside", 1, { map: data.map })
27-
adToken.attrSet("class", `admonition ${this.title.toLowerCase()}`)
26+
const adToken = this.createToken("admonition_open", "aside", 1, {
27+
map: data.map,
28+
block: true
29+
})
30+
adToken.attrSet("class", "admonition")
31+
if (this.title) adToken.attrJoin("class", this.title.toLowerCase())
2832
if (data.options.class) {
2933
adToken.attrJoin("class", data.options.class.join(" "))
3034
}
3135
newTokens.push(adToken)
3236

33-
const adTokenTitle = this.createToken("admonition_title_open", "p", 1)
37+
const adTokenTitle = this.createToken("admonition_title_open", "header", 1)
3438
adTokenTitle.attrSet("class", "admonition-title")
3539
newTokens.push(adTokenTitle)
3640

@@ -44,20 +48,15 @@ class BaseAdmonition extends Directive {
4448
})
4549
)
4650

47-
newTokens.push(this.createToken("admonition_title_close", "p", -1))
51+
newTokens.push(
52+
this.createToken("admonition_title_close", "header", -1, { block: true })
53+
)
4854

49-
const adTokenBody = this.createToken("admonition_body_open", "div", 1, {
50-
map: data.bodyMap
51-
})
52-
adTokenBody.attrSet("class", "admonition-body")
53-
newTokens.push(adTokenBody)
5455
// run a recursive parse on the content of the admonition upto this stage
5556
const bodyTokens = this.nestedParse(data.body, data.bodyMap[0])
5657
newTokens.push(...bodyTokens)
5758

58-
newTokens.push(this.createToken("admonition_body_close", "div", -1))
59-
60-
newTokens.push(this.createToken("admonition_close", "aside", -1))
59+
newTokens.push(this.createToken("admonition_close", "aside", -1, { block: true }))
6160

6261
return newTokens
6362
}

src/style/_admonition.sass

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

2828
// Defaults for all admonitions
29-
p.admonition-title
29+
.admonition-title
3030
position: relative
3131
margin: 0 -0.5rem 0.5rem
3232
padding: 0.5rem 0.5rem 0.5rem 2rem

tests/fixtures/directives.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ admonition
1616
Some *content*
1717
```
1818
.
19-
<aside class="admonition "><p class="admonition-title">A <strong>Title</strong></p><div class="admonition-body"><p>Some <em>content</em></p>
20-
</div></aside>
19+
<aside class="admonition">
20+
<header class="admonition-title">A <strong>Title</strong></header>
21+
<p>Some <em>content</em></p>
22+
</aside>
2123
.
2224

2325
nested-admonition
@@ -27,9 +29,14 @@ nested-admonition
2729
```
2830
````
2931
.
30-
<aside class="admonition note"><p class="admonition-title">Note</p><div class="admonition-body"><p>This is a note</p>
31-
<aside class="admonition warning"><p class="admonition-title">Warning</p><div class="admonition-body"><p>This is a nested warning</p>
32-
</div></aside></div></aside>
32+
<aside class="admonition note">
33+
<header class="admonition-title">Note</header>
34+
<p>This is a note</p>
35+
<aside class="admonition warning">
36+
<header class="admonition-title">Warning</header>
37+
<p>This is a nested warning</p>
38+
</aside>
39+
</aside>
3340
.
3441

3542
image

0 commit comments

Comments
 (0)