Skip to content

Commit 046a6eb

Browse files
committed
refactor: split out fix for #5
1 parent 29443e6 commit 046a6eb

2 files changed

Lines changed: 8 additions & 38 deletions

File tree

mdformat_footnote/plugin.py

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,18 @@ def _footnote_renderer(node: RenderTreeNode, context: RenderContext) -> str:
3232
first_line = f"[^{node.meta['label']}]:"
3333
indent = " " * 4
3434
elements = []
35-
36-
first_child_idx = 0
37-
while (
38-
first_child_idx < len(node.children)
39-
and node.children[first_child_idx].type == "footnote_anchor"
40-
):
41-
first_child_idx += 1
42-
43-
if (
44-
first_child_idx < len(node.children)
45-
and node.children[first_child_idx].type == "paragraph"
46-
):
47-
with context.indented(len(first_line) + 1):
48-
first_element = node.children[first_child_idx].render(context)
49-
50-
first_element_lines = first_element.split("\n")
51-
first_para_first_line = first_element_lines[0]
52-
first_para_rest_lines = first_element_lines[1:]
53-
54-
with context.indented(len(indent)):
55-
for child in node.children[first_child_idx + 1 :]:
56-
if child.type == "footnote_anchor":
57-
continue
58-
elements.append(child.render(context))
59-
60-
result = first_line + " " + first_para_first_line
61-
if first_para_rest_lines:
62-
indented_rest = textwrap.indent("\n".join(first_para_rest_lines), indent)
63-
result += "\n" + indented_rest
64-
if elements:
65-
result += "\n\n" + textwrap.indent("\n\n".join(elements), indent)
66-
return result
67-
6835
with context.indented(len(indent)):
6936
for child in node.children:
7037
if child.type == "footnote_anchor":
7138
continue
7239
elements.append(child.render(context))
7340
body = textwrap.indent("\n\n".join(elements), indent)
74-
if body:
41+
# if the first body element is a paragraph, we can start on the first line,
42+
# otherwise we start on the second line
43+
if body and node.children and node.children[0].type != "paragraph":
7544
body = "\n" + body
45+
else:
46+
body = " " + body.lstrip()
7647
return first_line + body
7748

7849

tests/test_word_wrap.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ def test_word_wrap():
1313
expected_output = """\
1414
[^a]
1515
16-
[^a]: Ooh no, the first line of this
17-
first paragraph is still wrapped
18-
too wide unfortunately. Should fix
19-
this.
16+
[^a]: Ooh no, the first line of this first
17+
paragraph is still wrapped too wide
18+
unfortunately. Should fix this.
2019
2120
But this second paragraph is wrapped
2221
exactly as expected. Woohooo,

0 commit comments

Comments
 (0)