#!/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)