Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions mdformat_footnote/plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import textwrap
from typing import Mapping

from markdown_it import MarkdownIt
Expand All @@ -19,16 +20,22 @@ def _footnote_ref_renderer(node: RenderTreeNode, context: RenderContext) -> str:


def _footnote_renderer(node: RenderTreeNode, context: RenderContext) -> str:
text = f"[^{node.meta['label']}]: "
child_iterator = iter(node.children)
first_child = next(child_iterator)
if first_child.type == "footnote_anchor":
return text
first_line = f"[^{node.meta['label']}]:"
indent = " " * 4
elements = []
with context.indented(len(indent)):
for child in node.children:
if child.type == "footnote_anchor":
continue
elements.append(child.render(context))
body = textwrap.indent("\n\n".join(elements), indent)
# if the first body element is a paragraph, we can start on the first line,
# otherwise we start on the second line
if body and node.children and node.children[0].type != "paragraph":
body = "\n" + body
else:
text += first_child.render(context)
for child in child_iterator:
text += "\n\n " + child.render(context)
return text
body = " " + body.lstrip()
return first_line + body


def _render_children(node: RenderTreeNode, context: RenderContext) -> str:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ classifiers = [
keywords = "mdformat,markdown,markdown-it"

requires-python=">=3.6"
requires=["mdformat >=0.7.0,<0.8.0",
requires=["mdformat >=0.7.8,<0.8.0",
"mdit-py-plugins",
]

Expand Down
35 changes: 34 additions & 1 deletion tests/fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Here is a footnote reference,[^1] and another.[^longnote]
[^longnote]: Here's one with multiple blocks.

Subsequent paragraphs are indented to show that they
belong to the previous footnote.
belong to the previous footnote.

Third paragraph here.
.
Expand Down Expand Up @@ -83,3 +83,36 @@ Here is a [link]

[link]: https://www.python.org
.

footnote-indentation
.
[^a]

[^a]: first paragraph with
unindented next line.

paragraph with
indented next line

paragraph with
unindented next line

```
content
```
.
[^a]

[^a]: first paragraph with
unindented next line.

paragraph with
indented next line

paragraph with
unindented next line

```
content
```
.
25 changes: 25 additions & 0 deletions tests/test_word_wrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import mdformat


def test_word_wrap():
input_text = """\
[^a]

[^a]: Ooh no, the first line of this first paragraph is still wrapped too wide
unfortunately. Should fix this.

But this second paragraph is wrapped exactly as expected. Woohooo, awesome!
"""
expected_output = """\
[^a]

[^a]: Ooh no, the first line of this first
paragraph is still wrapped too wide
unfortunately. Should fix this.

But this second paragraph is wrapped
exactly as expected. Woohooo,
awesome!
"""
output = mdformat.text(input_text, options={"wrap": 40}, extensions={"footnote"})
assert output == expected_output