From 116cd843e72adf3e171a407e6d64085faec242a4 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Mon, 7 Feb 2022 16:16:05 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20footnote=20indentatio?= =?UTF-8?q?ns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mdformat_footnote/plugin.py | 23 ++++++++++++++--------- tests/fixtures.md | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/mdformat_footnote/plugin.py b/mdformat_footnote/plugin.py index 385f731..70250cd 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,20 @@ 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']}]:" + elements = [] + for child in node.children: + if child.type == "footnote_anchor": + continue + elements.append(child.render(context)) + body = textwrap.indent("\n\n".join(elements), " " * 4) + # 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/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 + ``` +. From 7c37bd1aaf122a45cc9c7d0488743eb51a1b0931 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Tue, 8 Feb 2022 01:31:22 +0200 Subject: [PATCH 2/2] Use RenderContext.indented --- mdformat_footnote/plugin.py | 12 +++++++----- pyproject.toml | 2 +- tests/test_word_wrap.py | 25 +++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 tests/test_word_wrap.py diff --git a/mdformat_footnote/plugin.py b/mdformat_footnote/plugin.py index 70250cd..a84e774 100644 --- a/mdformat_footnote/plugin.py +++ b/mdformat_footnote/plugin.py @@ -21,12 +21,14 @@ def _footnote_ref_renderer(node: RenderTreeNode, context: RenderContext) -> str: def _footnote_renderer(node: RenderTreeNode, context: RenderContext) -> str: first_line = f"[^{node.meta['label']}]:" + indent = " " * 4 elements = [] - for child in node.children: - if child.type == "footnote_anchor": - continue - elements.append(child.render(context)) - body = textwrap.indent("\n\n".join(elements), " " * 4) + 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": 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/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