Categorygithub.com/willabides/mdtable
modulepackage
0.3.1
Repository: https://github.com/willabides/mdtable.git
Documentation: pkg.go.dev

# README

mdtable

godoc ci

Package mdtable generates markdown tables from string slices with formatting options for alignment and column width.

Functions

func Generate

func Generate(data [][]string, options ...Option) []byte

Generate generates a markdown table.

data -- is the table data. The top level slice contains rows and second level slices are cells. The first row is the header row. You need at least two rows (including the header row) to create a valid markdown table.

options -- are options for formatting the table. All options are in options.md.

There are three different types of alignment available, Alignment, TextAlignment and HeaderAlignment. Each of these can be set at either the table or column level.

Alignment -- controls how the markdown will appear when rendered in a browser.

TextAlignment -- controls the text alignment of fields. Default behavior is using the Alignment value of AlignLeft if that is unset.

HeaderAlignment -- is the same as TextAlignment, but for the header row only. Default behavior is using the TextAlignment value.

Examples

package main

import (
	"fmt"
	"github.com/willabides/mdtable"
)

func main() {
	data := [][]string{
		// first row is the header
		{"Name", "Favorite Animal", "Lucky Number"},

		// the rest is data
		{"Dave", "Elephant", "7"},
		{"Iris", "Gorilla", "8"},
		{"Ava Gayle", "Sloth", "972.5"},
	}

	b := mdtable.Generate(data)
	fmt.Println(string(b))

}

Output:

| Name      | Favorite Animal | Lucky Number |
|-----------|-----------------|--------------|
| Dave      | Elephant        | 7            |
| Iris      | Gorilla         | 8            |
| Ava Gayle | Sloth           | 972.5        |

Options

package main

import (
	"fmt"
	"github.com/willabides/mdtable"
)

func main() {
	// This adds options one at a time and shows what the output of
	// mdtable.Generate would be after each option is added.

	data := [][]string{
		{"Name", "Favorite Animal", "Lucky Number"},
		{"Dave", "Elephant", "7"},
		{"Iris", "Gorilla", "8"},
		{"Ava Gayle", "Sloth", "972.5"},
	}

	var options []mdtable.Option

	// Right align the whole table
	options = append(options,
		mdtable.Alignment(mdtable.AlignRight),
	)

	/*
		|      Name | Favorite Animal | Lucky Number |
		|----------:|----------------:|-------------:|
		|      Dave |        Elephant |            7 |
		|      Iris |         Gorilla |            8 |
		| Ava Gayle |           Sloth |        972.5 |
	*/

	// Left align header text
	options = append(options,
		mdtable.HeaderAlignment(mdtable.AlignLeft),
	)

	/*
		| Name      | Favorite Animal | Lucky Number |
		|----------:|----------------:|-------------:|
		|      Dave |        Elephant |            7 |
		|      Iris |         Gorilla |            8 |
		| Ava Gayle |           Sloth |        972.5 |
	*/

	// Set Favorite Animal's (offset 1) minimum width to 20 and center its text
	options = append(options,
		mdtable.ColumnMinWidth(1, 20),
		mdtable.ColumnTextAlignment(1, mdtable.AlignCenter),
	)

	/*
		| Name      | Favorite Animal      | Lucky Number |
		|----------:|---------------------:|-------------:|
		|      Dave |       Elephant       |            7 |
		|      Iris |       Gorilla        |            8 |
		| Ava Gayle |        Sloth         |        972.5 |
	*/

	options = append(options,
		mdtable.ColumnAlignment(0, mdtable.AlignLeft), // Left align Name
	)

	b := mdtable.Generate(data, options...)
	fmt.Println(string(b))

}

Output:

| Name      | Favorite Animal      | Lucky Number |
|:----------|---------------------:|-------------:|
| Dave      |       Elephant       |            7 |
| Iris      |       Gorilla        |            8 |
| Ava Gayle |        Sloth         |        972.5 |

Readme created from Go doc with goreadme

# Functions

Alignment sets alignment for columns.
ColumnAlignment sets the markdown alignment for a column.
ColumnHeaderAlignment sets the text alignment for a column header.
ColumnMinWidth sets the minimum width for a column.
ColumnTextAlignment sets the text alignment for a column.
Generate generates a markdown table.
HeaderAlignment sets the text alignment for headers.
TextAlignment sets the default text alignment for non-header cells.

# Constants

markdown: |:------:| text: | foo |.
markdown: |--------| text: | foo |.
markdown: |:-------| text: | foo |.
markdown: |-------:| text: | foo |.

# Type aliases

Align is a value for markdown and text alignment.
Option is an option to control a table's formatting.