Skip to content

Commit

Permalink
improve: Convert tabs to spaces. Reduce space sequences to one space
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin committed Oct 28, 2024
1 parent f4a8125 commit ac03952
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/users/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
5 changes: 5 additions & 0 deletions src/mdformat/renderer/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("\\", "\\\\")
Expand Down
22 changes: 22 additions & 0 deletions tests/data/default_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
.

0 comments on commit ac03952

Please sign in to comment.