markdown-table-ts

markdown-table-ts is a zero-dependency library for generating Markdown tables. Since it's written in TypeScript, it comes with type declarations out of the box.
Installation
Grab markdown-table-ts from npm with your favorite package manager.
npm install markdown-table-ts
yarn add markdown-table-ts
Quick start
import { Align, getMarkdownTable } from 'markdown-table-ts';
const table = getMarkdownTable({
table: {
head: ['ID', 'Name', 'Age'],
body: [
['1', 'John', '26'],
['2', 'Bob', '25'],
['3', 'Alice', '23'],
],
},
alignment: [Align.Left, Align.Center, Align.Right],
});
console.log(table);
API
getMarkdownTable(params: GetTableInput): string
- generates the corresponding Markdown table based on
params
- does not append newline to the end of the resulting table
- throws a
MarkdownTableError
when something goes wrong.
The underlying structure of GetTableInput
looks like this.
enum Align {
Left = 'left',
Right = 'right',
Center = 'center',
None = 'none',
}
type Column = string;
type Row = Column[];
interface Table {
head: Row,
body: Row[],
}
interface GetTableInput {
table: Table,
alignColumns?: boolean,
alignment?: Align[],
}
params.table.head
- table headers
- must be an array of strings
- cannot be empty
Examples
['header1', 'header2', 'header3']
undefined
[]
params.table.body
- table body
- must be an array whose elements are arrays of strings
- can be empty
Examples
['column1', 'column2', 'column3']
[]
undefined
params.alignColumns
- whether to vertically align columns (
|
) - must be boolean or undefined (in which case it will default to true)
Examples
true
false
undefined
'yes'
1
alignColumns
on/off
import { Align, Table, getMarkdownTable } from 'markdown-table-ts';
const myTable: Table = {
head: ['first header', 'second header', 'very very very long header'],
body: [
['1', 'John', '26'],
['very very very long id', 'Bob', '25'],
['3', 'Alice', '23'],
],
};
const myAlignment: Align[] = [Align.Center, Align.None, Align.Right];
console.log(getMarkdownTable({
table: myTable,
alignment: myAlignment,
alignColumns: true,
}));
console.log(getMarkdownTable({
table: myTable,
alignment: myAlignment,
alignColumns: false,
}));
params.alignment
- defines how to align each of the columns
- must be an array of
Align
which is one of
Align.Left
(:----
)Align.Right
(----:
)Align.Center
(:---:
)Align.None
(-----
)
- defaults to
Align.None
Examples
[Align.Left, Align.Right, Align.Center, Align.None]
['left', 'right', 'center', 'none']
[Align.left, 1, 'hello']
Edge cases
Some rows have more columns than others
When some of the rows (table head included) have more columns than others, the resulting table will have as many columns as the row with the most columns. Undefined columns will be empty.
import { getMarkdownTable } from 'markdown-table-ts';
const table = getMarkdownTable({
table: {
head: ['ID', 'Name', 'Age'],
body: [
['1', 'John', '26', 'more data', 'even more data'],
['2', 'Bob', '25'],
['3', 'Alice', '23', 'extra column'],
],
},
});
console.log(table);
Length of alignment
array is less than amount of columns in the table
When there are less elements in the params.alignment
array than there are columns in the resulting table, getMarkdownTable
will still succeed and columns with undefined alignment will default to Align.None
(-----
), e.g. when there are 4 columns in the table and the length of params.alignment
is 2, the first 2 columns will use alignment from params.alignment
and the rest of the columns will default to Align.None
(-----
).
import { Align, getMarkdownTable } from 'markdown-table-ts';
const table = getMarkdownTable({
table: {
head: ['ID', 'Name', 'Age', 'Country'],
body: [
['1', 'John', '26', 'Czechia'],
['2', 'Bob', '25', 'United States'],
['3', 'Alice', '23', 'Denmark'],
],
},
alignment: [Align.Center, Align.Left],
});
console.log(table);