csv2md

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2025 License: MIT Imports: 6 Imported by: 0

README

CSV To Markdown Table Converter

This is a utility tool used to convert Comma-separated values (CSV) files to a Markdown table.

Test it out in the Go playground.

Table Of Contents

Usage

An example conversion can look like this:

package main

import (
  "fmt"

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

func main() {
  var cfg csv2md.Config
  cfg.Align = csv2md.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 := csv2md.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.
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.
VerboseLogging bool Log detailed diagnostic messages when running the program.

Performance

I did not create a very proper setup to measure the performance. Ran it with my own PC so take it with a grain of salt.

Rows Columns Average Execution Time (5 runs)
100 12 1,8ms
1.000 12 37ms
10.000 12 2,4s
100.000 12 249s

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 Config

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

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

	// Log detailed diagnostic messages when running the program.
	VerboseLogging bool
}

Jump to

Keyboard shortcuts

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