Skip to content

Commit

Permalink
Add trailing newline to fenced code if a plugin forgets (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin authored Oct 16, 2024
1 parent 52313bc commit dad3d12
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/mdformat/renderer/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ def fence(node: RenderTreeNode, context: RenderContext) -> str:
fence_char = "~" if "`" in info_str else "`"

# Format the code block using enabled codeformatter funcs
if lang in context.options.get("codeformatters", {}):
fmt_func = context.options["codeformatters"][lang]
fmt_func = context.options.get("codeformatters", {}).get(lang)
if fmt_func:
try:
code_block = fmt_func(code_block, info_str)
except Exception:
Expand All @@ -167,6 +167,9 @@ def fence(node: RenderTreeNode, context: RenderContext) -> str:
if filename:
warn_msg += f". Filename: {filename}"
LOGGER.warning(warn_msg)
else:
if code_block and code_block[-1] != "\n":
code_block += "\n"

# The code block must not include as long or longer sequence of `fence_char`s
# as the fence string itself
Expand Down
85 changes: 80 additions & 5 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
from mdformat.renderer import MDRenderer, RenderContext, RenderTreeNode


def example_formatter(code, info):
return "dummy\n"


def test_code_formatter(monkeypatch):
monkeypatch.setitem(CODEFORMATTERS, "lang", example_formatter)
def fmt_func(code, info):
return "dummy\n"

monkeypatch.setitem(CODEFORMATTERS, "lang", fmt_func)
text = mdformat.text(
dedent(
"""\
Expand All @@ -37,6 +36,82 @@ def test_code_formatter(monkeypatch):
)


def test_code_formatter__empty_str(monkeypatch):
def fmt_func(code, info):
return ""

monkeypatch.setitem(CODEFORMATTERS, "lang", fmt_func)
text = mdformat.text(
dedent(
"""\
~~~lang
aag
gw
~~~
"""
),
codeformatters={"lang"},
)
assert text == dedent(
"""\
```lang
```
"""
)


def test_code_formatter__no_end_newline(monkeypatch):
def fmt_func(code, info):
return "dummy\ndum"

monkeypatch.setitem(CODEFORMATTERS, "lang", fmt_func)
text = mdformat.text(
dedent(
"""\
```lang
```
"""
),
codeformatters={"lang"},
)
assert text == dedent(
"""\
```lang
dummy
dum
```
"""
)


def test_code_formatter__interface(monkeypatch):
def fmt_func(code, info):
return info + code * 2

monkeypatch.setitem(CODEFORMATTERS, "lang", fmt_func)
text = mdformat.text(
dedent(
"""\
``` lang long
multi
mul
```
"""
),
codeformatters={"lang"},
)
assert text == dedent(
"""\
```lang long
lang longmulti
mul
multi
mul
```
"""
)


class TextEditorPlugin:
"""A plugin that makes all text the same."""

Expand Down

0 comments on commit dad3d12

Please sign in to comment.