Skip to content

Commit 17921de

Browse files
committedDec 5, 2021
Update docs
1 parent d1cd6fa commit 17921de

File tree

3 files changed

+230
-21
lines changed

3 files changed

+230
-21
lines changed
 

‎index.js

+132-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,140 @@
11
/**
22
* @typedef Options
3+
* Configuration (optional).
34
* @property {string|null|Array<string|null|undefined>} [align]
5+
* One style for all columns, or styles for their respective columns.
6+
* Each style is either `'l'` (left), `'r'` (right), or `'c'` (center).
7+
* Other values are treated as `''`, which doesn’t place the colon in the
8+
* alignment row but does align left.
9+
* *Only the lowercased first character is used, so `Right` is fine.*
410
* @property {boolean} [padding=true]
11+
* Whether to add a space of padding between delimiters and cells.
12+
*
13+
* When `true`, there is padding:
14+
*
15+
* ```markdown
16+
* | Alpha | B |
17+
* | ----- | ----- |
18+
* | C | Delta |
19+
* ```
20+
*
21+
* When `false`, there is no padding:
22+
*
23+
* ```markdown
24+
* |Alpha|B |
25+
* |-----|-----|
26+
* |C |Delta|
27+
* ```
528
* @property {boolean} [delimiterStart=true]
6-
* @property {boolean} [delimiterStart=true]
29+
* Whether to begin each row with the delimiter.
30+
*
31+
* > 👉 **Note**: please don’t use this: it could create fragile structures
32+
* > that aren’t understandable to some markdown parsers.
33+
*
34+
* When `true`, there are starting delimiters:
35+
*
36+
* ```markdown
37+
* | Alpha | B |
38+
* | ----- | ----- |
39+
* | C | Delta |
40+
* ```
41+
*
42+
* When `false`, there are no starting delimiters:
43+
*
44+
* ```markdown
45+
* Alpha | B |
46+
* ----- | ----- |
47+
* C | Delta |
48+
* ```
749
* @property {boolean} [delimiterEnd=true]
50+
* Whether to end each row with the delimiter.
51+
*
52+
* > 👉 **Note**: please don’t use this: it could create fragile structures
53+
* > that aren’t understandable to some markdown parsers.
54+
*
55+
* When `true`, there are ending delimiters:
56+
*
57+
* ```markdown
58+
* | Alpha | B |
59+
* | ----- | ----- |
60+
* | C | Delta |
61+
* ```
62+
*
63+
* When `false`, there are no ending delimiters:
64+
*
65+
* ```markdown
66+
* | Alpha | B
67+
* | ----- | -----
68+
* | C | Delta
69+
* ```
870
* @property {boolean} [alignDelimiters=true]
71+
* Whether to align the delimiters.
72+
* By default, they are aligned:
73+
*
74+
* ```markdown
75+
* | Alpha | B |
76+
* | ----- | ----- |
77+
* | C | Delta |
78+
* ```
79+
*
80+
* Pass `false` to make them staggered:
81+
*
82+
* ```markdown
83+
* | Alpha | B |
84+
* | - | - |
85+
* | C | Delta |
86+
* ```
987
* @property {(value: string) => number} [stringLength]
88+
* Function to detect the length of table cell content.
89+
* This is used when aligning the delimiters (`|`) between table cells.
90+
* Full-width characters and emoji mess up delimiter alignment when viewing
91+
* the markdown source.
92+
* To fix this, you can pass this function, which receives the cell content
93+
* and returns its “visible” size.
94+
* Note that what is and isn’t visible depends on where the text is displayed.
95+
*
96+
* Without such a function, the following:
97+
*
98+
* ```js
99+
* markdownTable([
100+
* ['Alpha', 'Bravo'],
101+
* ['中文', 'Charlie'],
102+
* ['👩‍❤️‍👩', 'Delta']
103+
* ])
104+
* ```
105+
*
106+
* Yields:
107+
*
108+
* ```markdown
109+
* | Alpha | Bravo |
110+
* | - | - |
111+
* | 中文 | Charlie |
112+
* | 👩‍❤️‍👩 | Delta |
113+
* ```
114+
*
115+
* With [`string-width`](https://github.com/sindresorhus/string-width):
116+
*
117+
* ```js
118+
* import stringWidth from 'string-width'
119+
*
120+
* markdownTable(
121+
* [
122+
* ['Alpha', 'Bravo'],
123+
* ['中文', 'Charlie'],
124+
* ['👩‍❤️‍👩', 'Delta']
125+
* ],
126+
* {stringLength: stringWidth}
127+
* )
128+
* ```
129+
*
130+
* Yields:
131+
*
132+
* ```markdown
133+
* | Alpha | Bravo |
134+
* | ----- | ------- |
135+
* | 中文 | Charlie |
136+
* | 👩‍❤️‍👩 | Delta |
137+
* ```
10138
*/
11139

12140
/**
@@ -16,10 +144,12 @@
16144
*/
17145

18146
/**
19-
* Create a table from a matrix of strings.
147+
* Generate a markdown ([GFM](https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables)) table..
20148
*
21149
* @param {Array<Array<string|null|undefined>>} table
150+
* Table data (matrix of strings).
22151
* @param {Options} [options]
152+
* Configuration (optional).
23153
* @returns {string}
24154
*/
25155
export function markdownTable(table, options = {}) {

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "markdown-table",
33
"version": "3.0.1",
4-
"description": "Markdown tables",
4+
"description": "Generate a markdown (GFM) table",
55
"license": "MIT",
66
"keywords": [
77
"text",

‎readme.md

+97-18
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,62 @@
55
[![Downloads][downloads-badge]][downloads]
66
[![Size][size-badge]][size]
77

8-
Generate fancy [Markdown][fancy] tables.
8+
Generate a markdown ([GFM][]) table.
99

10-
## Install
10+
## Contents
11+
12+
* [What is this?](#what-is-this)
13+
* [When should I use this?](#when-should-i-use-this)
14+
* [Install](#install)
15+
* [Use](#use)
16+
* [API](#api)
17+
* [`markdownTable(table[, options])`](#markdowntabletable-options)
18+
* [Types](#types)
19+
* [Compatibility](#compatibility)
20+
* [Security](#security)
21+
* [Inspiration](#inspiration)
22+
* [Contribute](#contribute)
23+
* [License](#license)
24+
25+
## What is this?
26+
27+
This is a simple package that takes table data and generates a [GitHub flavored
28+
markdown (GFM)][gfm] table.
1129

12-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
13-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
30+
## When should I use this?
31+
32+
You can use this package when you want to generate the source code of a GFM
33+
table from some data.
34+
35+
This is a simple solution in that it doesn’t handle escapes or HTML or any of
36+
that.
37+
For a complete but heavier solution, build an AST and serialize it with
38+
[`mdast-util-to-markdown`][mdast-util-to-markdown] (with
39+
[`mdast-util-gfm`][mdast-util-gfm]).
40+
41+
## Install
1442

15-
[npm][]:
43+
This package is [ESM only][esm].
44+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
1645

1746
```sh
1847
npm install markdown-table
1948
```
2049

50+
In Deno with [Skypack][]:
51+
52+
```js
53+
import {markdownTable} from 'https://cdn.skypack.dev/markdown-table@3?dts'
54+
```
55+
56+
In browsers with [Skypack][]:
57+
58+
```html
59+
<script type="module">
60+
import {markdownTable} from 'https://cdn.skypack.dev/markdown-table@3?min'
61+
</script>
62+
```
63+
2164
## Use
2265

2366
Typical usage (defaults to align left):
@@ -74,14 +117,16 @@ There is no default export.
74117

75118
### `markdownTable(table[, options])`
76119

77-
Turns a given matrix of strings (an array of arrays of strings) into a table.
120+
Generate a markdown table from table data (matrix of strings).
78121

79122
##### `options`
80123

124+
Configuration (optional).
125+
81126
###### `options.align`
82127

83128
One style for all columns, or styles for their respective columns (`string` or
84-
`string[]`).
129+
`Array<string>`).
85130
Each style is either `'l'` (left), `'r'` (right), or `'c'` (center).
86131
Other values are treated as `''`, which doesn’t place the colon in the alignment
87132
row but does align left.
@@ -112,8 +157,8 @@ When `false`, there is no padding:
112157

113158
Whether to begin each row with the delimiter (`boolean`, default: `true`).
114159

115-
Note: please don’t use this: it could create fragile structures that aren’t
116-
understandable to some Markdown parsers.
160+
> 👉 **Note**: please don’t use this: it could create fragile structures that
161+
> aren’t understandable to some markdown parsers.
117162
118163
When `true`, there are starting delimiters:
119164

@@ -135,8 +180,8 @@ C | Delta |
135180

136181
Whether to end each row with the delimiter (`boolean`, default: `true`).
137182

138-
Note: please don’t use this: it could create fragile structures that aren’t
139-
understandable to some Markdown parsers.
183+
> 👉 **Note**: please don’t use this: it could create fragile structures that
184+
> aren’t understandable to some markdown parsers.
140185
141186
When `true`, there are ending delimiters:
142187

@@ -175,12 +220,14 @@ Pass `false` to make them staggered:
175220

176221
###### `options.stringLength`
177222

178-
Method to detect the length of a cell (`Function`, default: `s => s.length`).
179-
180-
Full-width characters and ANSI-sequences all mess up delimiter alignment
181-
when viewing the Markdown source.
182-
To fix this, you have to pass in a `stringLength` option to detect the “visible”
183-
length of a cell (note that what is and isn’t visible depends on your editor).
223+
Function to detect the length of table cell content (`Function`, default:
224+
`s => s.length`).
225+
This is used when aligning the delimiters (`|`) between table cells.
226+
Full-width characters and emoji mess up delimiter alignment when viewing the
227+
markdown source.
228+
To fix this, you can pass this function, which receives the cell content and
229+
returns its “visible” size.
230+
Note that what is and isn’t visible depends on where the text is displayed.
184231

185232
Without such a function, the following:
186233

@@ -225,11 +272,31 @@ Yields:
225272
| 👩‍❤️‍👩 | Delta |
226273
```
227274

275+
## Types
276+
277+
This package is fully typed with [TypeScript][].
278+
It exports additional `Options` type that models its respective interface.
279+
280+
## Compatibility
281+
282+
This package is at least compatible with all maintained versions of Node.js.
283+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
284+
It also works in Deno and modern browsers.
285+
286+
## Security
287+
288+
This package is safe.
289+
228290
## Inspiration
229291

230292
The original idea and basic implementation was inspired by James Halliday’s
231293
[`text-table`][text-table] library.
232294

295+
## Contribute
296+
297+
Yes please!
298+
See [How to Contribute to Open Source][contribute].
299+
233300
## License
234301

235302
[MIT][license] © [Titus Wormer][author]
@@ -254,12 +321,24 @@ The original idea and basic implementation was inspired by James Halliday’s
254321

255322
[npm]: https://docs.npmjs.com/cli/install
256323

324+
[skypack]: https://www.skypack.dev
325+
257326
[license]: license
258327

259328
[author]: https://wooorm.com
260329

261-
[fancy]: https://help.github.com/articles/github-flavored-markdown/#tables
330+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
331+
332+
[typescript]: https://www.typescriptlang.org
333+
334+
[contribute]: https://opensource.guide/how-to-contribute/
335+
336+
[gfm]: https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables
262337

263338
[text-table]: https://github.com/substack/text-table
264339

265340
[string-width]: https://github.com/sindresorhus/string-width
341+
342+
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
343+
344+
[mdast-util-gfm]: https://github.com/syntax-tree/mdast-util-gfm

0 commit comments

Comments
 (0)
Please sign in to comment.