Skip to content

Commit 19ff403

Browse files
committed
fix: finish implementation and unit tests
1 parent 5218c55 commit 19ff403

File tree

7 files changed

+79
-60
lines changed

7 files changed

+79
-60
lines changed

mdformat_mkdocs/mdit_plugins/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
)
88
from ._material_deflist import (
99
escape_deflist,
10+
material_deflist_plugin,
1011
render_material_definition_body,
1112
render_material_definition_list,
1213
render_material_definition_term,
@@ -42,6 +43,7 @@
4243
"escape_deflist",
4344
"material_admon_plugin",
4445
"material_content_tabs_plugin",
46+
"material_deflist_plugin",
4547
"mkdocstrings_autorefs_plugin",
4648
"mkdocstrings_crossreference_plugin",
4749
"pymd_abbreviations_plugin",

mdformat_mkdocs/mdit_plugins/_material_deflist.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,20 @@
2828

2929
from __future__ import annotations
3030

31-
from mdformat.renderer import RenderContext, RenderTreeNode
32-
from mdformat.renderer.typing import Render
31+
from typing import TYPE_CHECKING
32+
33+
from markdown_it import MarkdownIt
34+
from mdit_py_plugins.deflist import deflist_plugin
35+
36+
if TYPE_CHECKING:
37+
from markdown_it import MarkdownIt
38+
from mdformat.renderer import RenderContext, RenderTreeNode
39+
from mdformat.renderer.typing import Render
40+
41+
42+
def material_deflist_plugin(md: MarkdownIt) -> None:
43+
"""Add mkdocs-material definition list support to markdown-it parser."""
44+
md.use(deflist_plugin)
3345

3446

3547
def make_render_children(separator: str) -> Render:
@@ -44,14 +56,20 @@ def render_children(
4456
return render_children
4557

4658

47-
def render_material_definition_list() -> str:
59+
def render_material_definition_list(
60+
node: RenderTreeNode,
61+
context: RenderContext,
62+
) -> str:
4863
"""Render Material Definition List."""
49-
return make_render_children("\n")
64+
return make_render_children("\n")(node, context)
5065

5166

52-
def render_material_definition_term() -> str:
67+
def render_material_definition_term(
68+
node: RenderTreeNode,
69+
context: RenderContext,
70+
) -> str:
5371
"""Render Material Definition Term."""
54-
return make_render_children("\n")
72+
return make_render_children("\n")(node, context)
5573

5674

5775
def render_material_definition_body(

mdformat_mkdocs/plugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from typing import TYPE_CHECKING
88

99
from mdformat.renderer import DEFAULT_RENDERERS, RenderContext, RenderTreeNode
10-
from mdit_py_plugins.deflist import deflist_plugin
1110

1211
from ._helpers import ContextOptions, get_conf
1312
from ._normalize_list import normalize_list as unbounded_normalize_list
@@ -20,8 +19,10 @@
2019
PYMD_CAPTIONS_PREFIX,
2120
PYMD_SNIPPET_PREFIX,
2221
PYTHON_MARKDOWN_ATTR_LIST_PREFIX,
22+
escape_deflist,
2323
material_admon_plugin,
2424
material_content_tabs_plugin,
25+
material_deflist_plugin,
2526
mkdocstrings_autorefs_plugin,
2627
mkdocstrings_crossreference_plugin,
2728
pymd_abbreviations_plugin,
@@ -86,7 +87,7 @@ def update_mdit(mdit: MarkdownIt) -> None:
8687
mdit.use(material_admon_plugin)
8788
mdit.use(pymd_captions_plugin)
8889
mdit.use(material_content_tabs_plugin)
89-
mdit.use(deflist_plugin) # TODO: convert material_deflist to normal plugin
90+
mdit.use(material_deflist_plugin)
9091
mdit.use(mkdocstrings_autorefs_plugin)
9192
mdit.use(pymd_abbreviations_plugin)
9293
mdit.use(pymd_admon_plugin)

tests/format/fixtures/material_deflist.md

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,36 @@ paragraph
2020

2121
Term 1
2222

23-
: Definition 1
23+
: Definition 1
2424

2525
Term 2 with *inline markup*
2626

27-
: Definition 2
27+
: Definition 2
2828

29-
```
30-
{ some code, part of Definition 2 }
31-
```
29+
```
30+
{ some code, part of Definition 2 }
31+
```
3232

33-
Third paragraph of definition 2.
33+
Third paragraph of definition 2.
3434

3535
paragraph
3636
.
3737

3838
Pandoc 2:
39-
4039
.
4140
Term 1
4241

43-
: Definition
42+
: Definition
4443
with lazy continuation.
4544

46-
Second paragraph of the definition.
45+
Second paragraph of the definition.
4746
.
4847
Term 1
4948

50-
: Definition
51-
with lazy continuation.
49+
: Definition
50+
with lazy continuation.
5251

53-
Second paragraph of the definition.
52+
Second paragraph of the definition.
5453
.
5554

5655
Pandoc 3
@@ -69,11 +68,11 @@ paragraph
6968
paragraph
7069

7170
Term 1
72-
: Definition 1
71+
: Definition 1
7372

7473
Term 2
75-
: Definition 2a
76-
: Definition 2b
74+
: Definition 2a
75+
: Definition 2b
7776

7877
paragraph
7978
.
@@ -87,12 +86,12 @@ Term 2
8786
: code block
8887
.
8988
Term 1
90-
: paragraph
89+
: paragraph
9190

9291
Term 2
93-
: ```
94-
code block
95-
```
92+
: ```
93+
code block
94+
```
9695
.
9796

9897
List is tight, only if all dts are tight:
@@ -108,15 +107,15 @@ Term 2
108107
.
109108
Term 1
110109

111-
: foo
110+
: foo
112111

113-
: bar
112+
: bar
114113

115114
Term 2
116115

117-
: foo
116+
: foo
118117

119-
: bar
118+
: bar
120119
.
121120

122121

@@ -132,13 +131,13 @@ Term 2
132131
.
133132
Term 1
134133

135-
: foo
134+
: foo
136135

137-
bar
136+
bar
138137

139138
Term 2
140139

141-
: foo
140+
: foo
142141
.
143142

144143
Nested definition lists:
@@ -152,11 +151,11 @@ test
152151
: foo
153152
.
154153
test
155-
: foo
156-
: bar
157-
: baz
158-
: bar
159-
: foo
154+
: foo
155+
: bar
156+
: baz
157+
: bar
158+
: foo
160159
.
161160

162161
Regression test (blockquote inside deflist)
@@ -166,12 +165,11 @@ foo
166165
: baz
167166
.
168167
foo
169-
: > bar
170-
: baz
168+
: > bar
169+
: baz
171170
.
172171

173172
Escaped deflist
174-
175173
.
176174
Term 1
177175
\: Definition

tests/pre-commit-test.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,6 @@ graph TD;
331331
C-->D;
332332
```
333333

334-
[external link]: https://github.com/czuli/github-markdown-example/tree/7326f19c94be992319394e5bfeaa07b30f858e46
335-
336334
______________________________________________________________________
337335

338336
# Deflist Test
@@ -343,16 +341,18 @@ paragraph
343341

344342
Term 1
345343

346-
: Definition 1
344+
: Definition 1
347345

348346
Term 2 with *inline markup*
349347

350-
: Definition 2
348+
: Definition 2
351349

352-
```
353-
{ some code, part of Definition 2 }
354-
```
350+
```
351+
{ some code, part of Definition 2 }
352+
```
355353

356-
Third paragraph of definition 2.
354+
Third paragraph of definition 2.
357355

358356
paragraph
357+
358+
[external link]: https://github.com/czuli/github-markdown-example/tree/7326f19c94be992319394e5bfeaa07b30f858e46

tests/render/fixtures/material_deflist.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ Example definition list <https://squidfunk.github.io/mkdocs-material/reference/l
1515
Nullam dignissim ultrices urna non auctor.
1616
.
1717
<dl>
18-
<dt>
19-
<code>Lorem ipsum dolor sit amet</code>
20-
</dt>
18+
<dt><code>Lorem ipsum dolor sit amet</code></dt>
2119
<dd>
22-
<p>Sed sagittis eleifend rutrum. Donec vitae suscipit est. Nullam tempus tellus non sem sollicitudin, quis rutrum leo facilisis.</p>
20+
<p>Sed sagittis eleifend rutrum. Donec vitae suscipit est. Nullam tempus
21+
tellus non sem sollicitudin, quis rutrum leo facilisis.</p>
2322
</dd>
24-
<dt>
25-
<code>Cras arcu libero</code>
26-
</dt>
23+
<dt><code>Cras arcu libero</code></dt>
2724
<dd>
28-
<p>Aliquam metus eros, pretium sed nulla venenatis, faucibus auctor ex. Proin ut eros sed sapien ullamcorper consequat. Nunc ligula ante.</p>
29-
<p>Duis mollis est eget nibh volutpat, fermentum aliquet dui mollis. Nam vulputate tincidunt fringilla. Nullam dignissim ultrices urna non auctor.</p>
25+
<p>Aliquam metus eros, pretium sed nulla venenatis, faucibus auctor ex. Proin
26+
ut eros sed sapien ullamcorper consequat. Nunc ligula ante.</p>
27+
<p>Duis mollis est eget nibh volutpat, fermentum aliquet dui mollis.
28+
Nam vulputate tincidunt fringilla.
29+
Nullam dignissim ultrices urna non auctor.</p>
3030
</dd>
3131
</dl>
3232
.

tests/render/test_render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def with_plugin(filename, plugins):
3232
"material_content_tabs.md",
3333
[material_admon_plugin, material_content_tabs_plugin],
3434
),
35-
*with_plugin("mkdocs_deflist.md", [material_deflist_plugin]),
35+
*with_plugin("material_deflist.md", [material_deflist_plugin]),
3636
*with_plugin("mkdocstrings_autorefs.md", [mkdocstrings_autorefs_plugin]),
3737
*with_plugin("pymd_abbreviations.md", [pymd_abbreviations_plugin]),
3838
*with_plugin("pymd_captions.md", [pymd_captions_plugin]),

0 commit comments

Comments
 (0)