Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/willabides/mdtable
- Owner: WillAbides
- License: mit
- Created: 2021-01-10T16:45:44.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-09-14T16:25:26.000Z (over 3 years ago)
- Last Synced: 2024-11-21T08:38:08.337Z (about 2 months ago)
- Topics: generator, markdown
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 mainimport (
"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 mainimport (
"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)