Skip to content

Commit a96f2e2

Browse files
authored
🐛 FIX: Parsing directive content, when body followed by arguments (#28)
This fixes a bug (inherited from myst-parser), whereby a directive with no arguments would omit the first line, if proceeded by options.
1 parent fa3eb17 commit a96f2e2

File tree

5 files changed

+231
-52
lines changed

5 files changed

+231
-52
lines changed

src/directives/admonitions.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ class BaseAdmonition extends Directive {
2727
map: data.map,
2828
block: true
2929
})
30-
adToken.attrSet("class", "admonition")
31-
if (this.title)
30+
if (data.options.class?.length >= 1) {
31+
// Custom class information must go first for styling
32+
// For example, `class=tip, kind=seealso` should be styled as a `tip`
33+
adToken.attrSet("class", data.options.class.join(" "))
34+
adToken.attrJoin("class", "admonition")
35+
} else {
36+
adToken.attrSet("class", "admonition")
37+
}
38+
if (this.title) {
3239
adToken.attrJoin("class", this.title.toLowerCase().replace(/ /g, ""))
33-
if (data.options.class) {
34-
adToken.attrJoin("class", data.options.class.join(" "))
3540
}
3641
newTokens.push(adToken)
3742

src/directives/main.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,7 @@ export default function directiveToData(
182182
;[body, options, bodyOffset] = parseDirectiveOptions(body, directive)
183183
}
184184
let args: string[] = []
185-
if (
186-
!directive.required_arguments &&
187-
!directive.optional_arguments &&
188-
!Object.keys(options).length
189-
) {
185+
if (!directive.required_arguments && !directive.optional_arguments) {
190186
if (firstLine) {
191187
bodyOffset = 0
192188
body = [firstLine].concat(body)
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
Admonition:
2+
.
3+
```{admonition} This is a **title**
4+
An example of an admonition with custom _title_.
5+
```
6+
.
7+
<aside class="admonition">
8+
<header class="admonition-title">This is a <strong>title</strong></header>
9+
<p>An example of an admonition with custom <em>title</em>.</p>
10+
</aside>
11+
.
12+
13+
Note on split lines:
14+
.
15+
```{note} An example
16+
of an admonition on two lines.
17+
```
18+
.
19+
<aside class="admonition note">
20+
<header class="admonition-title">Note</header>
21+
<p>An example
22+
of an admonition on two lines.</p>
23+
</aside>
24+
.
25+
26+
[FIX] Note on a single line [See #154](https://github.com/executablebooks/MyST-Parser/issues/154)
27+
.
28+
```{danger} An example of an admonition on a single line.
29+
```
30+
.
31+
<aside class="admonition danger">
32+
<header class="admonition-title">Danger</header>
33+
<p>An example of an admonition on a single line.</p>
34+
</aside>
35+
.
36+
37+
Admonition with overridding class name
38+
.
39+
```{admonition} This is a title
40+
:class: tip
41+
An example of a `tip` with a custom _title_.
42+
```
43+
.
44+
<aside class="tip admonition">
45+
<header class="admonition-title">This is a title</header>
46+
<p>An example of a <code>tip</code> with a custom <em>title</em>.</p>
47+
</aside>
48+
.
49+
50+
nested-admonition
51+
.
52+
````{note} This is a note
53+
```{warning} This is a nested warning
54+
```
55+
````
56+
.
57+
<aside class="admonition note">
58+
<header class="admonition-title">Note</header>
59+
<p>This is a note</p>
60+
<aside class="admonition warning">
61+
<header class="admonition-title">Warning</header>
62+
<p>This is a nested warning</p>
63+
</aside>
64+
</aside>
65+
.
66+
67+
`attention` admonition:
68+
.
69+
```{attention}
70+
An example of a attention admonition.
71+
```
72+
.
73+
<aside class="admonition attention">
74+
<header class="admonition-title">Attention</header>
75+
<p>An example of a attention admonition.</p>
76+
</aside>
77+
.
78+
79+
`caution` admonition:
80+
.
81+
```{caution}
82+
An example of a caution admonition.
83+
```
84+
.
85+
<aside class="admonition caution">
86+
<header class="admonition-title">Caution</header>
87+
<p>An example of a caution admonition.</p>
88+
</aside>
89+
.
90+
91+
`danger` admonition:
92+
.
93+
```{danger}
94+
An example of a danger admonition.
95+
```
96+
.
97+
<aside class="admonition danger">
98+
<header class="admonition-title">Danger</header>
99+
<p>An example of a danger admonition.</p>
100+
</aside>
101+
.
102+
103+
`error` admonition:
104+
.
105+
```{error}
106+
An example of an error admonition.
107+
```
108+
.
109+
<aside class="admonition error">
110+
<header class="admonition-title">Error</header>
111+
<p>An example of an error admonition.</p>
112+
</aside>
113+
.
114+
115+
`hint` admonition:
116+
.
117+
```{hint}
118+
An example of a hint admonition.
119+
```
120+
.
121+
<aside class="admonition hint">
122+
<header class="admonition-title">Hint</header>
123+
<p>An example of a hint admonition.</p>
124+
</aside>
125+
.
126+
127+
`important` admonition:
128+
.
129+
```{important}
130+
An example of an important admonition.
131+
```
132+
.
133+
<aside class="admonition important">
134+
<header class="admonition-title">Important</header>
135+
<p>An example of an important admonition.</p>
136+
</aside>
137+
.
138+
139+
`note` admonition:
140+
.
141+
```{note}
142+
An example of a note admonition.
143+
```
144+
.
145+
<aside class="admonition note">
146+
<header class="admonition-title">Note</header>
147+
<p>An example of a note admonition.</p>
148+
</aside>
149+
.
150+
151+
`tip` admonition:
152+
.
153+
```{tip}
154+
An example of a tip admonition.
155+
```
156+
.
157+
<aside class="admonition tip">
158+
<header class="admonition-title">Tip</header>
159+
<p>An example of a tip admonition.</p>
160+
</aside>
161+
.
162+
163+
`warning` admonition:
164+
.
165+
```{warning}
166+
An example of a warning admonition.
167+
```
168+
.
169+
<aside class="admonition warning">
170+
<header class="admonition-title">Warning</header>
171+
<p>An example of a warning admonition.</p>
172+
</aside>
173+
.
174+
175+
`see also` admonition:
176+
.
177+
```{seealso}
178+
See other things here!
179+
```
180+
.
181+
<aside class="admonition seealso">
182+
<header class="admonition-title">See Also</header>
183+
<p>See other things here!</p>
184+
</aside>
185+
.
186+
187+
188+
`see also` admonition with class, bump title
189+
.
190+
```{seealso} Not a title
191+
:class: tip
192+
See other things here!
193+
```
194+
.
195+
<aside class="tip admonition seealso">
196+
<header class="admonition-title">See Also</header>
197+
<p>Not a title
198+
See other things here!</p>
199+
</aside>
200+
.
201+
202+
203+
`see also` admonition with class, bump title new paragraph
204+
.
205+
```{seealso} Not a title
206+
:class: tip
207+
208+
See other things here!
209+
```
210+
.
211+
<aside class="tip admonition seealso">
212+
<header class="admonition-title">See Also</header>
213+
<p>Not a title</p>
214+
<p>See other things here!</p>
215+
</aside>
216+
.

tests/fixtures/directives.md

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,6 @@ content
1010
</pre></aside>
1111
.
1212

13-
admonition
14-
.
15-
```{admonition} A **Title**
16-
Some *content*
17-
```
18-
.
19-
<aside class="admonition">
20-
<header class="admonition-title">A <strong>Title</strong></header>
21-
<p>Some <em>content</em></p>
22-
</aside>
23-
.
24-
25-
admonition
26-
.
27-
```{seealso}
28-
See other things here!
29-
```
30-
.
31-
<aside class="admonition seealso">
32-
<header class="admonition-title">See Also</header>
33-
<p>See other things here!</p>
34-
</aside>
35-
.
36-
37-
nested-admonition
38-
.
39-
````{note} This is a note
40-
```{warning} This is a nested warning
41-
```
42-
````
43-
.
44-
<aside class="admonition note">
45-
<header class="admonition-title">Note</header>
46-
<p>This is a note</p>
47-
<aside class="admonition warning">
48-
<header class="admonition-title">Warning</header>
49-
<p>This is a nested warning</p>
50-
</aside>
51-
</aside>
52-
.
53-
5413
image
5514
.
5615
```{image} https://via.placeholder.com/150

tests/fixturesDirectives.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ describe("Parses directives", () => {
77
readFixtures("directives").forEach(([name, text, expected]) => {
88
const mdit = MarkdownIt().use(docutils_plugin)
99
const rendered = mdit.render(text)
10-
// console.log(rendered)
10+
it(name, () => expect(rendered.trim()).toEqual((expected || "").trim()))
11+
})
12+
readFixtures("directives.admonitions").forEach(([name, text, expected]) => {
13+
const mdit = MarkdownIt().use(docutils_plugin)
14+
const rendered = mdit.render(text)
1115
it(name, () => expect(rendered.trim()).toEqual((expected || "").trim()))
1216
})
1317
})
@@ -20,7 +24,6 @@ describe("Parses math directives", () => {
2024
return `<div class="math block">\n${tokens[idx].content.trimRight()}\n</div>`
2125
}
2226
const rendered = mdit.render(text)
23-
// console.log(rendered)
2427
it(name, () => expect(rendered.trim()).toEqual((expected || "").trim()))
2528
})
2629
})

0 commit comments

Comments
 (0)