Skip to content

Commit 21e6928

Browse files
authored
πŸ› FIX: footnote indentations (#3)
* πŸ› FIX: footnote indentations * Use RenderContext.indented
1 parent af68829 commit 21e6928

File tree

4 files changed

+76
-11
lines changed

4 files changed

+76
-11
lines changed

β€Žmdformat_footnote/plugin.pyβ€Ž

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import textwrap
12
from typing import Mapping
23

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

2021

2122
def _footnote_renderer(node: RenderTreeNode, context: RenderContext) -> str:
22-
text = f"[^{node.meta['label']}]: "
23-
child_iterator = iter(node.children)
24-
first_child = next(child_iterator)
25-
if first_child.type == "footnote_anchor":
26-
return text
23+
first_line = f"[^{node.meta['label']}]:"
24+
indent = " " * 4
25+
elements = []
26+
with context.indented(len(indent)):
27+
for child in node.children:
28+
if child.type == "footnote_anchor":
29+
continue
30+
elements.append(child.render(context))
31+
body = textwrap.indent("\n\n".join(elements), indent)
32+
# if the first body element is a paragraph, we can start on the first line,
33+
# otherwise we start on the second line
34+
if body and node.children and node.children[0].type != "paragraph":
35+
body = "\n" + body
2736
else:
28-
text += first_child.render(context)
29-
for child in child_iterator:
30-
text += "\n\n " + child.render(context)
31-
return text
37+
body = " " + body.lstrip()
38+
return first_line + body
3239

3340

3441
def _render_children(node: RenderTreeNode, context: RenderContext) -> str:

β€Žpyproject.tomlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ classifiers = [
1919
keywords = "mdformat,markdown,markdown-it"
2020

2121
requires-python=">=3.6"
22-
requires=["mdformat >=0.7.0,<0.8.0",
22+
requires=["mdformat >=0.7.8,<0.8.0",
2323
"mdit-py-plugins",
2424
]
2525

β€Žtests/fixtures.mdβ€Ž

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Here is a footnote reference,[^1] and another.[^longnote]
4646
[^longnote]: Here's one with multiple blocks.
4747

4848
Subsequent paragraphs are indented to show that they
49-
belong to the previous footnote.
49+
belong to the previous footnote.
5050

5151
Third paragraph here.
5252
.
@@ -83,3 +83,36 @@ Here is a [link]
8383

8484
[link]: https://www.python.org
8585
.
86+
87+
footnote-indentation
88+
.
89+
[^a]
90+
91+
[^a]: first paragraph with
92+
unindented next line.
93+
94+
paragraph with
95+
indented next line
96+
97+
paragraph with
98+
unindented next line
99+
100+
```
101+
content
102+
```
103+
.
104+
[^a]
105+
106+
[^a]: first paragraph with
107+
unindented next line.
108+
109+
paragraph with
110+
indented next line
111+
112+
paragraph with
113+
unindented next line
114+
115+
```
116+
content
117+
```
118+
.

β€Žtests/test_word_wrap.pyβ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import mdformat
2+
3+
4+
def test_word_wrap():
5+
input_text = """\
6+
[^a]
7+
8+
[^a]: Ooh no, the first line of this first paragraph is still wrapped too wide
9+
unfortunately. Should fix this.
10+
11+
But this second paragraph is wrapped exactly as expected. Woohooo, awesome!
12+
"""
13+
expected_output = """\
14+
[^a]
15+
16+
[^a]: Ooh no, the first line of this first
17+
paragraph is still wrapped too wide
18+
unfortunately. Should fix this.
19+
20+
But this second paragraph is wrapped
21+
exactly as expected. Woohooo,
22+
awesome!
23+
"""
24+
output = mdformat.text(input_text, options={"wrap": 40}, extensions={"footnote"})
25+
assert output == expected_output

0 commit comments

Comments
Β (0)