Skip to content

phamduylong/csv2mdtable

Repository files navigation

CSV To Markdown Table Converter Go Reference

This is a utility tool used to convert Comma-separated values (CSV) files to a Markdown table. You can test it out in the Go playground.

Table Of Contents

Usage

An example conversion can look like this:

package main

import (
  "fmt"

  csv2mdtable "github.com/phamduylong/csv-to-md"
)

func main() {
  var cfg csv2mdtable.Config
  cfg.Align = csv2mdtable.Left
  cfg.VerboseLogging = true
  
  csv := `Index,Customer Id,First Name,Last Name,Company,City,Country,Phone
1,DD37Cf93aecA6Dc,Sheryl,Baxter,Rasmussen Group,East Leonard,Chile,229.077.5154
2,1Ef7b82A4CAAD10,Preston,Lozano,Vega-Gentry,East Jimmychester,Djibouti,5153435776
3,6F94879bDAfE5a6,Roy,Berry,Murillo-Perry,Isabelborough,Antigua and Barbuda,+1-539-402-0259
4,5Cef8BFA16c5e3c,Linda,Olsen,"Dominguez, Mcmillan and Donovan",Bensonview,Dominican Republic,001-808-617-6467x12895
5,053d585Ab6b3159,Joanna,Bender,"Martin, Lang and Andrade",West Priscilla,Slovakia (Slovak Republic),001-234-203-0635x76146`

  res, convertErr := csv2mdtable.Convert(csv, cfg)

  if convertErr != nil {
    fmt.Println(convertErr)
  }

  fmt.Printf("Converted table:\n\n%s\n", res)
}

Output would look like this:

2009/11/10 23:00:00 DEBUG Validating config 🤔
2009/11/10 23:00:00 DEBUG Config is valid ✅
Converted table:

| Index | Customer Id     | First Name | Last Name | Company                         | City              | Country                    | Phone                  |
| :---- | :-------------- | :--------- | :-------- | :------------------------------ | :---------------- | :------------------------- | :--------------------- |
| 1     | DD37Cf93aecA6Dc | Sheryl     | Baxter    | Rasmussen Group                 | East Leonard      | Chile                      | 229.077.5154           |
| 2     | 1Ef7b82A4CAAD10 | Preston    | Lozano    | Vega-Gentry                     | East Jimmychester | Djibouti                   | 5153435776             |
| 3     | 6F94879bDAfE5a6 | Roy        | Berry     | Murillo-Perry                   | Isabelborough     | Antigua and Barbuda        | +1-539-402-0259        |
| 4     | 5Cef8BFA16c5e3c | Linda      | Olsen     | Dominguez, Mcmillan and Donovan | Bensonview        | Dominican Republic         | 001-808-617-6467x12895 |
| 5     | 053d585Ab6b3159 | Joanna     | Bender    | Martin, Lang and Andrade        | West Priscilla    | Slovakia (Slovak Republic) | 001-234-203-0635x76146 |

Program exited.

Configuration Options

The program offers a range of different configuration options to customize the tool to best fit your use case.

Option Type What does it do?
Align Align Align the text on the rendered table. Visual feedback on the markdown syntax is also provided.
Caption string Set a caption for the table (will be rendered as an HTML comment above the table).
Compact bool Set whether the Markdown table be converted to compact syntax.
CSVReaderConfig CSVReaderConfig Config options to be passed into CSV reader object. See type Reader in the encoding/csv module.
CSVReaderConfig.Comma rune Set the delimiter of the CSV reader.
CSVReaderConfig.Comment rune Set the comment character for the CSV reader.
CSVReaderConfig.FieldsPerRecord int Set the amount of fields per CSV row.
CSVReaderConfig.LazyQuotes bool Set whether lazy quotes are allowed. If lazy quotes are allowed, a quote may appear in an unquoted field and a non-doubled quote may appear in a quoted field.
CSVReaderConfig.TrimLeadingSpace bool Set whether leading space before the fields' values should be ignored.
CSVReaderConfig.ReuseRecord bool Set whether calls to Read may return a slice sharing the backing array of the previous call's returned slice for performance. By default, each call to Read returns newly allocated memory owned by the caller.
ExcludedColumns []string Set the list of columns that should be ignored when constructing the table.
SortColumns ColumnSortOption Should the columns be sorted and how?
SortFunction ColumnSortFunction How should the columns be sorted? This option will be ignored if SortColumns is not set to Custom.
VerboseLogging bool Log detailed diagnostic messages when running the program.

About

A small utility project to convert CSV files into markdown's table syntax.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages