diff --git a/mdformat_footnote/plugin.py b/mdformat_footnote/plugin.py index 385f731..a84e774 100644 --- a/mdformat_footnote/plugin.py +++ b/mdformat_footnote/plugin.py @@ -1,3 +1,4 @@ +import textwrap from typing import Mapping from markdown_it import MarkdownIt @@ -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: diff --git a/pyproject.toml b/pyproject.toml index 4bfc1d6..10f76df 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", ] diff --git a/tests/fixtures.md b/tests/fixtures.md index c254bb0..05275ae 100644 --- a/tests/fixtures.md +++ b/tests/fixtures.md @@ -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. . @@ -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 + ``` +. diff --git a/tests/test_word_wrap.py b/tests/test_word_wrap.py new file mode 100644 index 0000000..fbe2867 --- /dev/null +++ b/tests/test_word_wrap.py @@ -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