Generate fancy Markdown tables.
This package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
npm install markdown-table
Typical usage (defaults to align left):
import {markdownTable} from 'markdown-table'
markdownTable([
['Branch', 'Commit'],
['main', '0123456789abcdef'],
['staging', 'fedcba9876543210']
])
Yields:
| Branch | Commit |
| ------- | ---------------- |
| main | 0123456789abcdef |
| staging | fedcba9876543210 |
With align:
markdownTable(
[
['Beep', 'No.', 'Boop'],
['beep', '1024', 'xyz'],
['boop', '3388450', 'tuv'],
['foo', '10106', 'qrstuv'],
['bar', '45', 'lmno']
],
{align: ['l', 'c', 'r']}
)
Yields:
| Beep | No. | Boop |
| :--- | :-----: | -----: |
| beep | 1024 | xyz |
| boop | 3388450 | tuv |
| foo | 10106 | qrstuv |
| bar | 45 | lmno |
This package exports the following identifiers: markdownTable
.
There is no default export.
Turns a given matrix of strings (an array of arrays of strings) into a table.
One style for all columns, or styles for their respective columns (string
or
string[]
).
Each style is either 'l'
(left), 'r'
(right), or 'c'
(center).
Other values are treated as ''
, which doesn’t place the colon in the alignment
row but does align left.
Only the lowercased first character is used, so Right
is fine.
Whether to add a space of padding between delimiters and cells (boolean
,
default: true
).
When true
, there is padding:
| Alpha | B |
| ----- | ----- |
| C | Delta |
When false
, there is no padding:
|Alpha|B |
|-----|-----|
|C |Delta|
Whether to begin each row with the delimiter (boolean
, default: true
).
Note: please don’t use this: it could create fragile structures that aren’t understandable to some Markdown parsers.
When true
, there are starting delimiters:
| Alpha | B |
| ----- | ----- |
| C | Delta |
When false
, there are no starting delimiters:
Alpha | B |
----- | ----- |
C | Delta |
Whether to end each row with the delimiter (boolean
, default: true
).
Note: please don’t use this: it could create fragile structures that aren’t understandable to some Markdown parsers.
When true
, there are ending delimiters:
| Alpha | B |
| ----- | ----- |
| C | Delta |
When false
, there are no ending delimiters:
| Alpha | B
| ----- | -----
| C | Delta
Whether to align the delimiters (boolean
, default: true
).
By default, they are aligned:
| Alpha | B |
| ----- | ----- |
| C | Delta |
Pass false
to make them staggered:
| Alpha | B |
| - | - |
| C | Delta |
Method to detect the length of a cell (Function
, default: s => s.length
).
Full-width characters and ANSI-sequences all mess up delimiter alignment
when viewing the Markdown source.
To fix this, you have to pass in a stringLength
option to detect the “visible”
length of a cell (note that what is and isn’t visible depends on your editor).
Without such a function, the following:
markdownTable([
['Alpha', 'Bravo'],
['中文', 'Charlie'],
['👩❤️👩', 'Delta']
])
Yields:
| Alpha | Bravo |
| - | - |
| 中文 | Charlie |
| 👩❤️👩 | Delta |
With string-width
:
import stringWidth from 'string-width'
markdownTable(
[
['Alpha', 'Bravo'],
['中文', 'Charlie'],
['👩❤️👩', 'Delta']
],
{stringLength: width}
)
Yields:
| Alpha | Bravo |
| ----- | ------- |
| 中文 | Charlie |
| 👩❤️👩 | Delta |
The original idea and basic implementation was inspired by James Halliday’s
text-table
library.
markdown-table's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.