Skip to content

Commit 884820d

Browse files
committed
Merge branch 'master' into fix-22-nested-footnotes
2 parents 46fc717 + d3cd3a9 commit 884820d

2 files changed

Lines changed: 61 additions & 14 deletions

File tree

mdformat_footnote/plugin.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,30 @@ def _footnote_ref_renderer(node: RenderTreeNode, context: RenderContext) -> str:
5656
def _footnote_renderer(node: RenderTreeNode, context: RenderContext) -> str:
5757
first_line = f"[^{node.meta['label']}]:"
5858
indent = " " * 4
59-
elements = []
59+
60+
children = [c for c in node.children if c.type != "footnote_anchor"]
61+
62+
if children and children[0].type == "paragraph":
63+
with context.indented(len(first_line) + 1):
64+
first_element = children[0].render(context)
65+
66+
first_para_first_line, *first_para_rest_lines = first_element.split("\n")
67+
68+
with context.indented(len(indent)):
69+
elements = [child.render(context) for child in children[1:]]
70+
71+
result = first_line + " " + first_para_first_line
72+
if first_para_rest_lines:
73+
result += "\n" + textwrap.indent("\n".join(first_para_rest_lines), indent)
74+
if elements:
75+
result += "\n\n" + textwrap.indent("\n\n".join(elements), indent)
76+
return result
77+
6078
with context.indented(len(indent)):
61-
for child in node.children:
62-
if child.type == "footnote_anchor":
63-
continue
64-
elements.append(child.render(context))
79+
elements = [child.render(context) for child in children]
6580
body = textwrap.indent("\n\n".join(elements), indent)
66-
# if the first body element is a paragraph, we can start on the first line,
67-
# otherwise we start on the second line
68-
if body and node.children and node.children[0].type != "paragraph":
81+
if body:
6982
body = "\n" + body
70-
else:
71-
body = " " + body.lstrip()
7283
return first_line + body
7384

7485

tests/fixtures/word_wrap.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
Word wrap at column 40
1+
wrap at 30
2+
.
3+
[^short]
4+
5+
[^short]: This is a shorter footnote that should wrap at thirty characters max.
6+
.
7+
[^short]
8+
9+
[^short]: This is a shorter
10+
footnote that should
11+
wrap at thirty
12+
characters max.
13+
.
14+
15+
16+
wrap at 40
217
.
318
[^a]
419

@@ -9,11 +24,32 @@ Word wrap at column 40
924
.
1025
[^a]
1126

12-
[^a]: Ooh no, the first line of this first
13-
paragraph is still wrapped too wide
14-
unfortunately. Should fix this.
27+
[^a]: Ooh no, the first line of this
28+
first paragraph is still wrapped
29+
too wide unfortunately. Should fix
30+
this.
1531

1632
But this second paragraph is wrapped
1733
exactly as expected. Woohooo,
1834
awesome!
1935
.
36+
37+
38+
wrap at 60
39+
.
40+
[^longer]
41+
42+
[^longer]: This footnote has a longer wrap length so it can contain more text per line
43+
before wrapping occurs.
44+
45+
Multiple paragraphs should also respect the wrapping configuration.
46+
.
47+
[^longer]
48+
49+
[^longer]: This footnote has a longer wrap length so it can
50+
contain more text per line before wrapping
51+
occurs.
52+
53+
Multiple paragraphs should also respect the wrapping
54+
configuration.
55+
.

0 commit comments

Comments
 (0)