Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/willabides/mdtable

generate markdown tables
https://github.com/willabides/mdtable

generator markdown

Last synced: about 1 month ago
JSON representation

generate markdown tables

Awesome Lists containing this project

README

        

# mdtable

[![godoc](https://pkg.go.dev/badge/github.com/willabides/mdtable.svg)](https://pkg.go.dev/github.com/willabides/mdtable)
[![ci](https://github.com/WillAbides/mdtable/workflows/ci/badge.svg?branch=main&event=push)](https://github.com/WillAbides/mdtable/actions?query=workflow%3Aci+branch%3Amain+event%3Apush)

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

## Functions

### func [Generate](/mdtable.go#L89)

`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

```golang
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

```golang
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](https://github.com/posener/goreadme)