Skip to content

Compact tables

compact_tables

Compact markdown table whitespace.

CompactTablesStrategy

Bases: SmeltStrategy

Remove padding whitespace from markdown table cells.

Source code in packages/axm-smelt/src/axm_smelt/strategies/compact_tables.py
Python
class CompactTablesStrategy(SmeltStrategy):
    """Remove padding whitespace from markdown table cells."""

    @property
    def name(self) -> str:
        return "compact_tables"

    @property
    def category(self) -> str:
        return "whitespace"

    def apply(self, ctx: SmeltContext) -> SmeltContext:
        if ctx.format is not Format.MARKDOWN:
            return ctx

        lines = ctx.text.split("\n")
        out: list[str] = []
        changed = False
        in_fenced = False

        for line in lines:
            if line.startswith("```"):
                in_fenced = not in_fenced
                out.append(line)
                continue

            if in_fenced or not _TABLE_LINE_RE.match(line):
                out.append(line)
                continue

            compacted = _compact_table_line(line)

            if compacted != line:
                changed = True
            out.append(compacted)

        if not changed:
            return ctx

        return SmeltContext(text="\n".join(out), format=ctx.format)