Skip to content
internal/tables
On this page

Rendering data as Markdown tables with per-column alignment, optional pretty-printing, and pipe escaping that leaves the pipes inside inline code alone.

#internal/tables

#internal/tables

Package tables renders data as Markdown tables with per-column alignment, optional pretty-printing, and pipe escaping that leaves inline code alone.

#EscapePipes

Go go
func EscapePipes(text string) string

EscapePipes escapes pipe characters in text, preserving pipes inside backtick spans.

A pipe outside a backtick span would end the table cell, so it becomes "\|"; a pipe inside one is part of an inline code literal and stays. If a backtick opens a span but never closes, the pipes after that backtick are escaped as if the span had never opened -- an unbalanced backtick renders as a literal backtick, so its pipes are cell-ending pipes again.

#RenderMarkdownTable

Go go
func RenderMarkdownTable(headers []string, rows [][]string, align []string, pretty bool) (string, error)

RenderMarkdownTable renders a Markdown table from headers and rows.

align gives the per-column alignment ("left", "center" or "right"); it may be nil, shorter than headers (the remaining columns get no alignment marker) or longer (the extra entries are ignored, though every entry is still validated). pretty pads every cell so the rendered source lines up in a text editor.

A row with fewer cells than there are headers is padded with empty cells; a row with more is an error. It also errors on empty headers, on an alignment value that is none of the three, and on a newline inside any cell or header.

Search