Add git clean filter to normalize Obsidian table padding

Obsidian auto-formats markdown tables with column-aligned padding on
every file open, causing noisy git diffs. This filter strips extra
whitespace from table cells on git add/diff, keeping the repo clean.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-16 23:32:04 +02:00
parent 8b137e567b
commit 43f49df9ea
2 changed files with 50 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
#!/usr/bin/env python3
"""Git clean filter: normalizes markdown table padding.
Obsidian auto-formats tables with aligned columns (extra spaces).
This filter strips padding to single-space format: | cell | cell |
Handles escaped pipes (\\|) inside cells correctly.
"""
import sys
import re
SEPARATOR_RE = re.compile(r'^\|(\s*[-:]+[-:\s]*\|)+\s*$')
def clean_table_row(line):
"""Strip extra whitespace from a markdown table row, preserving escaped pipes."""
# Replace escaped pipes with placeholder
placeholder = '\x00PIPE\x00'
work = line.replace('\\|', placeholder)
if not work.startswith('|') or not work.rstrip().endswith('|'):
return line
# Check if separator row
if SEPARATOR_RE.match(work):
cells = work.strip().split('|')
# cells[0] and cells[-1] are empty (before first | and after last |)
n_cols = len(cells) - 2
return '|' + '|'.join(['---'] * n_cols) + '|'
# Regular row: split by |, trim each cell
cells = work.strip().split('|')
# cells[0] = '' (before first |), cells[-1] = '' (after last |)
result_parts = []
for i, cell in enumerate(cells):
if i == 0 or i == len(cells) - 1:
result_parts.append('')
else:
content = cell.strip().replace(placeholder, '\\|')
result_parts.append(f' {content} ')
return '|'.join(result_parts)
for line in sys.stdin:
line = line.rstrip('\n').rstrip('\r')
if line.startswith('|'):
print(clean_table_row(line))
else:
print(line)