tablefmt

package
v0.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package tablefmt answers "what does the canonical aligned form of a pipe-table look like for these source lines?" Callers ask the package to format a string of markdown, to spot non-conforming tables in a parsed line list, or to rewrite the source bytes in place.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatLines

func FormatLines(source []byte, lines [][]byte, codeLines map[int]bool, cfg Config) []byte

FormatLines rewrites every table found in source with canonical formatting, preserving everything else. lines must be the result of splitting source on newlines (i.e. f.Lines from internal/lint).

func FormatString

func FormatString(s string, pad int) string

FormatString formats all markdown tables in s with the given padding and returns the result. Padding less than 0 falls back to 1 (one space of padding on each side of cell content). Uses the default spaced separator style; callers that need to choose a style call FormatStringWithConfig instead.

func FormatStringWithConfig added in v0.23.0

func FormatStringWithConfig(s string, cfg Config) string

FormatStringWithConfig formats all markdown tables in s with cfg.

Types

type Config added in v0.23.0

type Config struct {
	Pad            int
	SeparatorStyle SeparatorStyle
}

Config controls how tables are formatted.

Pad is the number of spaces on each side of cell content. Values below 0 fall back to 1.

SeparatorStyle picks how the separator row is rendered: SeparatorSpaced writes `| --- | --- |` (the form spelled out by the GFM specification example), SeparatorCompact writes `|---|---|`. The zero value is SeparatorSpaced so a caller that builds a Config{Pad: 1} without touching the style field gets the spec-leaning default.

type SeparatorStyle added in v0.23.0

type SeparatorStyle int

SeparatorStyle selects the rendering of the separator row.

const (
	// SeparatorSpaced writes `| --- | --- |`. Zero value so the
	// default-constructed Config picks this layout.
	SeparatorSpaced SeparatorStyle = iota
	// SeparatorCompact writes `|---|---|`. Dashes fill the cell area
	// with no whitespace around them.
	SeparatorCompact
)

func ParseSeparatorStyle added in v0.23.0

func ParseSeparatorStyle(v any, ruleName string) (SeparatorStyle, error)

ParseSeparatorStyle converts a config value (typically a YAML scalar) to a SeparatorStyle, returning a rule-scoped error if v is not one of the supported string forms. ruleName is the rule's config key (e.g. "table-format", "catalog") and prefixes the error so multiple rules can share this helper without losing diagnostic context.

type Violation

type Violation struct {
	StartLine int    // 1-based line number of the table's first row
	Message   string // diagnostic message including the first differing row
}

Violation describes a single table whose source formatting differs from the canonical layout produced by this package.

func Violations

func Violations(lines [][]byte, codeLines map[int]bool, cfg Config) []Violation

Violations returns the formatting violations found in lines. codeLines maps 1-based line numbers known to sit inside a fenced or indented code block; those lines are skipped.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL