From 43f49df9ea21ddeb64c8f862947a9842b4de5afc Mon Sep 17 00:00:00 2001 From: Ivan Liashkevich Date: Mon, 16 Feb 2026 23:32:04 +0200 Subject: [PATCH] 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 --- .git-filters/clean-md-tables.py | 49 +++++++++++++++++++++++++++++++++ .gitattributes | 1 + 2 files changed, 50 insertions(+) create mode 100644 .git-filters/clean-md-tables.py create mode 100644 .gitattributes diff --git a/.git-filters/clean-md-tables.py b/.git-filters/clean-md-tables.py new file mode 100644 index 0000000..1d08746 --- /dev/null +++ b/.git-filters/clean-md-tables.py @@ -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) diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..eaf2853 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.md filter=obsidian-tables