Skip to content

Commit cd70874

Browse files
committed
Add trailing newline to fenced code if a plugin forgets
1 parent 52313bc commit cd70874

File tree

2 files changed

+85
-7
lines changed

2 files changed

+85
-7
lines changed

src/mdformat/renderer/_context.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ def fence(node: RenderTreeNode, context: RenderContext) -> str:
151151
fence_char = "~" if "`" in info_str else "`"
152152

153153
# Format the code block using enabled codeformatter funcs
154-
if lang in context.options.get("codeformatters", {}):
155-
fmt_func = context.options["codeformatters"][lang]
154+
fmt_func = context.options.get("codeformatters", {}).get(lang)
155+
if fmt_func:
156156
try:
157157
code_block = fmt_func(code_block, info_str)
158158
except Exception:
@@ -167,6 +167,9 @@ def fence(node: RenderTreeNode, context: RenderContext) -> str:
167167
if filename:
168168
warn_msg += f". Filename: {filename}"
169169
LOGGER.warning(warn_msg)
170+
else:
171+
if code_block and code_block[-1] != "\n":
172+
code_block += "\n"
170173

171174
# The code block must not include as long or longer sequence of `fence_char`s
172175
# as the fence string itself

tests/test_plugins.py

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
from mdformat.renderer import MDRenderer, RenderContext, RenderTreeNode
1313

1414

15-
def example_formatter(code, info):
16-
return "dummy\n"
17-
18-
1915
def test_code_formatter(monkeypatch):
20-
monkeypatch.setitem(CODEFORMATTERS, "lang", example_formatter)
16+
def fmt_func(code, info):
17+
return "dummy\n"
18+
19+
monkeypatch.setitem(CODEFORMATTERS, "lang", fmt_func)
2120
text = mdformat.text(
2221
dedent(
2322
"""\
@@ -37,6 +36,82 @@ def test_code_formatter(monkeypatch):
3736
)
3837

3938

39+
def test_code_formatter__empty_str(monkeypatch):
40+
def fmt_func(code, info):
41+
return ""
42+
43+
monkeypatch.setitem(CODEFORMATTERS, "lang", fmt_func)
44+
text = mdformat.text(
45+
dedent(
46+
"""\
47+
~~~lang
48+
aag
49+
gw
50+
~~~
51+
"""
52+
),
53+
codeformatters={"lang"},
54+
)
55+
assert text == dedent(
56+
"""\
57+
```lang
58+
```
59+
"""
60+
)
61+
62+
63+
def test_code_formatter__no_end_newline(monkeypatch):
64+
def fmt_func(code, info):
65+
return "dummy\ndum"
66+
67+
monkeypatch.setitem(CODEFORMATTERS, "lang", fmt_func)
68+
text = mdformat.text(
69+
dedent(
70+
"""\
71+
```lang
72+
```
73+
"""
74+
),
75+
codeformatters={"lang"},
76+
)
77+
assert text == dedent(
78+
"""\
79+
```lang
80+
dummy
81+
dum
82+
```
83+
"""
84+
)
85+
86+
87+
def test_code_formatter__interface(monkeypatch):
88+
def fmt_func(code, info):
89+
return info + code * 2
90+
91+
monkeypatch.setitem(CODEFORMATTERS, "lang", fmt_func)
92+
text = mdformat.text(
93+
dedent(
94+
"""\
95+
``` lang long
96+
multi
97+
mul
98+
```
99+
"""
100+
),
101+
codeformatters={"lang"},
102+
)
103+
assert text == dedent(
104+
"""\
105+
```lang long
106+
lang longmulti
107+
mul
108+
multi
109+
mul
110+
```
111+
"""
112+
)
113+
114+
40115
class TextEditorPlugin:
41116
"""A plugin that makes all text the same."""
42117

0 commit comments

Comments
 (0)