From ac03952c1384b8c1ce64064578689e46af11be40 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:43:15 +0200 Subject: [PATCH] improve: Convert tabs to spaces. Reduce space sequences to one space --- docs/users/changelog.md | 1 + src/mdformat/renderer/_context.py | 5 +++++ tests/data/default_style.md | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/docs/users/changelog.md b/docs/users/changelog.md index f4c249c..10ed08f 100644 --- a/docs/users/changelog.md +++ b/docs/users/changelog.md @@ -14,6 +14,7 @@ Note that there is currently no guarantee for a stable Markdown formatting style - Changed - Style: No longer escape square bracket enclosures. - Style: No longer escape less than sign followed by space character. + - Style: Convert tabs to spaces. Reduce space sequences to one space. - Improved - Plugin interface: A trailing newline is added to fenced code blocks if a plugin fails to add it. diff --git a/src/mdformat/renderer/_context.py b/src/mdformat/renderer/_context.py index 936ab87..b9a4da7 100644 --- a/src/mdformat/renderer/_context.py +++ b/src/mdformat/renderer/_context.py @@ -111,6 +111,11 @@ def text(node: RenderTreeNode, context: RenderContext) -> str: """ text = node.content + # Convert tabs to spaces + text = text.replace("\t", " ") + # Reduce tabs and spaces to one space + text = re.sub(" {2,}", " ", text) + # Escape backslash to prevent it from making unintended escapes. # This escape has to be first, else we start multiplying backslashes. text = text.replace("\\", "\\\\") diff --git a/tests/data/default_style.md b/tests/data/default_style.md index 653f4d3..7f1d453 100644 --- a/tests/data/default_style.md +++ b/tests/data/default_style.md @@ -508,3 +508,25 @@ Less than sign escapes \< . + +Tabs to spaces +. +# Convert tab to space in headings + +Make a space here. +. +# Convert tab to space in headings + +Make a space here. +. + +Reduce tabs and spaces to one space +. +# Reduce in headings + +Reduce to a space here. +. +# Reduce in headings + +Reduce to a space here. +.