csv2mdtable

package module
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2026 License: MIT Imports: 7 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(csv string, cfg Config) (string, error)

Convert CSV string into a markdown table. Returns the string representation of the markdown table if converted successfully and an error if failed.

func ValidateConfig

func ValidateConfig(cfg Config) error

Validate the Config object passed as parameter. An error will be returned in case the configuration was invalid.

Types

type Align

type Align int
const (
	Center Align = 0
	Left   Align = 1
	Right  Align = 2
)

type CSVReaderConfig added in v0.1.5

type CSVReaderConfig struct {
	// Comma is the field delimiter.
	// It is set to comma (',') by NewReader.
	// Comma must be a valid rune and must not be \r, \n,
	// or the Unicode replacement character (0xFFFD).
	Comma rune

	// Comment, if not 0, is the comment character. Lines beginning with the
	// Comment character without preceding whitespace are ignored.
	// With leading whitespace the Comment character becomes part of the
	// field, even if TrimLeadingSpace is true.
	// Comment must be a valid rune and must not be \r, \n,
	// or the Unicode replacement character (0xFFFD).
	// It must also not be equal to Comma.
	Comment rune

	// FieldsPerRecord is the number of expected fields per record.
	// If FieldsPerRecord is positive, Read requires each record to
	// have the given number of fields. If FieldsPerRecord is 0, Read sets it to
	// the number of fields in the first record, so that future records must
	// have the same field count. If FieldsPerRecord is negative, no check is
	// made and records may have a variable number of fields.
	FieldsPerRecord int

	// If LazyQuotes is true, a quote may appear in an unquoted field and a
	// non-doubled quote may appear in a quoted field.
	LazyQuotes bool

	// If TrimLeadingSpace is true, leading white space in a field is ignored.
	// This is done even if the field delimiter, Comma, is white space.
	TrimLeadingSpace bool

	// ReuseRecord controls 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.
	ReuseRecord bool
}

Mirroring https://pkg.go.dev/encoding/csv#Reader

type ColumnSortFunction added in v1.0.6

type ColumnSortFunction func(a string, b string) int

type ColumnSortOption added in v1.0.5

type ColumnSortOption int
const (
	None       ColumnSortOption = 0
	Ascending  ColumnSortOption = 1
	Descending ColumnSortOption = 2
	Custom     ColumnSortOption = 3
)

type Config

type Config struct {
	// Align the rendered content for the Markdown table. 0 = Center, 1 = Left, 2 = Right
	Align Align

	// Caption of the table (as an HTML comment)
	Caption string

	// Should the markdown table be the compact version
	Compact bool

	// Reader configuration to be used for Reader type.
	// See also https://pkg.go.dev/encoding/csv#Reader
	CSVReaderConfig CSVReaderConfig

	// List of columns to be excluded from table construction
	ExcludedColumns []string

	// Should the columns be sorted and how?
	SortColumns ColumnSortOption

	// Custom sort function
	SortFunction ColumnSortFunction

	// Log detailed diagnostic messages when running the program.
	VerboseLogging bool
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL