Socket
Socket
Sign inDemoInstall

markdown-table

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

markdown-table - npm Package Compare versions

Comparing version 3.0.1 to 3.0.2

295

index.d.ts
/**
* @typedef MarkdownTableOptions
* @property {string|null|Array.<string|null|undefined>} [align]
* @typedef Options
* Configuration (optional).
* @property {string|null|Array<string|null|undefined>} [align]
* One style for all columns, or styles for their respective columns.
* 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.*
* @property {boolean} [padding=true]
* Whether to add a space of padding between delimiters and cells.
*
* When `true`, there is padding:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* When `false`, there is no padding:
*
* ```markdown
* |Alpha|B |
* |-----|-----|
* |C |Delta|
* ```
* @property {boolean} [delimiterStart=true]
* @property {boolean} [delimiterStart=true]
* Whether to begin each row with the delimiter.
*
* > 👉 **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:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* When `false`, there are no starting delimiters:
*
* ```markdown
* Alpha | B |
* ----- | ----- |
* C | Delta |
* ```
* @property {boolean} [delimiterEnd=true]
* Whether to end each row with the delimiter.
*
* > 👉 **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:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* When `false`, there are no ending delimiters:
*
* ```markdown
* | Alpha | B
* | ----- | -----
* | C | Delta
* ```
* @property {boolean} [alignDelimiters=true]
* Whether to align the delimiters.
* By default, they are aligned:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* Pass `false` to make them staggered:
*
* ```markdown
* | Alpha | B |
* | - | - |
* | C | Delta |
* ```
* @property {(value: string) => number} [stringLength]
* Function to detect the length of table cell content.
* This is used when aligning the delimiters (`|`) between table cells.
* Full-width characters and emoji mess up delimiter alignment when viewing
* the markdown source.
* To fix this, you can pass this function, which receives the cell content
* and returns its “visible” size.
* Note that what is and isn’t visible depends on where the text is displayed.
*
* Without such a function, the following:
*
* ```js
* markdownTable([
* ['Alpha', 'Bravo'],
* ['中文', 'Charlie'],
* ['👩‍❤️‍👩', 'Delta']
* ])
* ```
*
* Yields:
*
* ```markdown
* | Alpha | Bravo |
* | - | - |
* | 中文 | Charlie |
* | 👩‍❤️‍👩 | Delta |
* ```
*
* With [`string-width`](https://github.com/sindresorhus/string-width):
*
* ```js
* import stringWidth from 'string-width'
*
* markdownTable(
* [
* ['Alpha', 'Bravo'],
* ['中文', 'Charlie'],
* ['👩‍❤️‍👩', 'Delta']
* ],
* {stringLength: stringWidth}
* )
* ```
*
* Yields:
*
* ```markdown
* | Alpha | Bravo |
* | ----- | ------- |
* | 中文 | Charlie |
* | 👩‍❤️‍👩 | Delta |
* ```
*/
/**
* Create a table from a matrix of strings.
* @typedef {Options} MarkdownTableOptions
* @todo
* Remove next major.
*/
/**
* Generate a markdown ([GFM](https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables)) table..
*
* @param {Array.<Array.<string|null|undefined>>} table
* @param {MarkdownTableOptions} [options]
* @param {Array<Array<string|null|undefined>>} table
* Table data (matrix of strings).
* @param {Options} [options]
* Configuration (optional).
* @returns {string}

@@ -20,11 +155,155 @@ */

table: Array<Array<string | null | undefined>>,
options?: MarkdownTableOptions | undefined
options?: Options | undefined
): string
export type MarkdownTableOptions = {
/**
* Configuration (optional).
*/
export type Options = {
/**
* One style for all columns, or styles for their respective columns.
* 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.*
*/
align?: string | (string | null | undefined)[] | null | undefined
/**
* Whether to add a space of padding between delimiters and cells.
*
* When `true`, there is padding:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* When `false`, there is no padding:
*
* ```markdown
* |Alpha|B |
* |-----|-----|
* |C |Delta|
* ```
*/
padding?: boolean | undefined
/**
* Whether to begin each row with the delimiter.
*
* > 👉 **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:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* When `false`, there are no starting delimiters:
*
* ```markdown
* Alpha | B |
* ----- | ----- |
* C | Delta |
* ```
*/
delimiterStart?: boolean | undefined
/**
* Whether to end each row with the delimiter.
*
* > 👉 **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:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* When `false`, there are no ending delimiters:
*
* ```markdown
* | Alpha | B
* | ----- | -----
* | C | Delta
* ```
*/
delimiterEnd?: boolean | undefined
/**
* Whether to align the delimiters.
* By default, they are aligned:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* Pass `false` to make them staggered:
*
* ```markdown
* | Alpha | B |
* | - | - |
* | C | Delta |
* ```
*/
alignDelimiters?: boolean | undefined
/**
* Function to detect the length of table cell content.
* This is used when aligning the delimiters (`|`) between table cells.
* Full-width characters and emoji mess up delimiter alignment when viewing
* the markdown source.
* To fix this, you can pass this function, which receives the cell content
* and returns its “visible” size.
* Note that what is and isn’t visible depends on where the text is displayed.
*
* Without such a function, the following:
*
* ```js
* markdownTable([
* ['Alpha', 'Bravo'],
* ['中文', 'Charlie'],
* ['👩‍❤️‍👩', 'Delta']
* ])
* ```
*
* Yields:
*
* ```markdown
* | Alpha | Bravo |
* | - | - |
* | 中文 | Charlie |
* | 👩‍❤️‍👩 | Delta |
* ```
*
* With [`string-width`](https://github.com/sindresorhus/string-width):
*
* ```js
* import stringWidth from 'string-width'
*
* markdownTable(
* [
* ['Alpha', 'Bravo'],
* ['中文', 'Charlie'],
* ['👩‍❤️‍👩', 'Delta']
* ],
* {stringLength: stringWidth}
* )
* ```
*
* Yields:
*
* ```markdown
* | Alpha | Bravo |
* | ----- | ------- |
* | 中文 | Charlie |
* | 👩‍❤️‍👩 | Delta |
* ```
*/
stringLength?: ((value: string) => number) | undefined
}
export type MarkdownTableOptions = Options

263

index.js
/**
* @typedef MarkdownTableOptions
* @property {string|null|Array.<string|null|undefined>} [align]
* @typedef Options
* Configuration (optional).
* @property {string|null|Array<string|null|undefined>} [align]
* One style for all columns, or styles for their respective columns.
* 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.*
* @property {boolean} [padding=true]
* Whether to add a space of padding between delimiters and cells.
*
* When `true`, there is padding:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* When `false`, there is no padding:
*
* ```markdown
* |Alpha|B |
* |-----|-----|
* |C |Delta|
* ```
* @property {boolean} [delimiterStart=true]
* @property {boolean} [delimiterStart=true]
* Whether to begin each row with the delimiter.
*
* > 👉 **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:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* When `false`, there are no starting delimiters:
*
* ```markdown
* Alpha | B |
* ----- | ----- |
* C | Delta |
* ```
* @property {boolean} [delimiterEnd=true]
* Whether to end each row with the delimiter.
*
* > 👉 **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:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* When `false`, there are no ending delimiters:
*
* ```markdown
* | Alpha | B
* | ----- | -----
* | C | Delta
* ```
* @property {boolean} [alignDelimiters=true]
* Whether to align the delimiters.
* By default, they are aligned:
*
* ```markdown
* | Alpha | B |
* | ----- | ----- |
* | C | Delta |
* ```
*
* Pass `false` to make them staggered:
*
* ```markdown
* | Alpha | B |
* | - | - |
* | C | Delta |
* ```
* @property {(value: string) => number} [stringLength]
* Function to detect the length of table cell content.
* This is used when aligning the delimiters (`|`) between table cells.
* Full-width characters and emoji mess up delimiter alignment when viewing
* the markdown source.
* To fix this, you can pass this function, which receives the cell content
* and returns its “visible” size.
* Note that what is and isn’t visible depends on where the text is displayed.
*
* Without such a function, the following:
*
* ```js
* markdownTable([
* ['Alpha', 'Bravo'],
* ['中文', 'Charlie'],
* ['👩‍❤️‍👩', 'Delta']
* ])
* ```
*
* Yields:
*
* ```markdown
* | Alpha | Bravo |
* | - | - |
* | 中文 | Charlie |
* | 👩‍❤️‍👩 | Delta |
* ```
*
* With [`string-width`](https://github.com/sindresorhus/string-width):
*
* ```js
* import stringWidth from 'string-width'
*
* markdownTable(
* [
* ['Alpha', 'Bravo'],
* ['中文', 'Charlie'],
* ['👩‍❤️‍👩', 'Delta']
* ],
* {stringLength: stringWidth}
* )
* ```
*
* Yields:
*
* ```markdown
* | Alpha | Bravo |
* | ----- | ------- |
* | 中文 | Charlie |
* | 👩‍❤️‍👩 | Delta |
* ```
*/
/**
* Create a table from a matrix of strings.
* @typedef {Options} MarkdownTableOptions
* @todo
* Remove next major.
*/
/**
* Generate a markdown ([GFM](https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables)) table..
*
* @param {Array.<Array.<string|null|undefined>>} table
* @param {MarkdownTableOptions} [options]
* @param {Array<Array<string|null|undefined>>} table
* Table data (matrix of strings).
* @param {Options} [options]
* Configuration (optional).
* @returns {string}
*/
export function markdownTable(table, options) {
const settings = options || {}
const align = (settings.align || []).concat()
const stringLength = settings.stringLength || defaultStringLength
/** @type {number[]} Character codes as symbols for alignment per column. */
export function markdownTable(table, options = {}) {
const align = (options.align || []).concat()
const stringLength = options.stringLength || defaultStringLength
/** @type {Array<number>} Character codes as symbols for alignment per column. */
const alignments = []
let rowIndex = -1
/** @type {string[][]} Cells per row. */
/** @type {Array<Array<string>>} Cells per row. */
const cellMatrix = []
/** @type {number[][]} Sizes of each cell per row. */
/** @type {Array<Array<number>>} Sizes of each cell per row. */
const sizeMatrix = []
/** @type {number[]} */
/** @type {Array<number>} */
const longestCellByColumn = []
let mostCellsPerRow = 0
/** @type {number} */
let columnIndex
/** @type {string[]} Cells of current row */
let row
/** @type {number[]} Sizes of current row */
let sizes
/** @type {number} Sizes of current cell */
let size
/** @type {string} Current cell */
let cell
/** @type {string[]} Chunks of current line. */
let line
/** @type {string} */
let before
/** @type {string} */
let after
/** @type {number} */
let code
let rowIndex = -1

@@ -55,5 +172,7 @@ // This is a superfluous loop if we don’t align delimiters, but otherwise we’d

while (++rowIndex < table.length) {
columnIndex = -1
row = []
sizes = []
/** @type {Array<string>} */
const row = []
/** @type {Array<number>} */
const sizes = []
let columnIndex = -1

@@ -65,6 +184,6 @@ if (table[rowIndex].length > mostCellsPerRow) {

while (++columnIndex < table[rowIndex].length) {
cell = serialize(table[rowIndex][columnIndex])
const cell = serialize(table[rowIndex][columnIndex])
if (settings.alignDelimiters !== false) {
size = stringLength(cell)
if (options.alignDelimiters !== false) {
const size = stringLength(cell)
sizes[columnIndex] = size

@@ -88,3 +207,3 @@

// Figure out which alignments to use.
columnIndex = -1
let columnIndex = -1

@@ -96,3 +215,3 @@ if (typeof align === 'object' && 'length' in align) {

} else {
code = toAlignment(align)
const code = toAlignment(align)

@@ -106,9 +225,11 @@ while (++columnIndex < mostCellsPerRow) {

columnIndex = -1
row = []
sizes = []
/** @type {Array<string>} */
const row = []
/** @type {Array<number>} */
const sizes = []
while (++columnIndex < mostCellsPerRow) {
code = alignments[columnIndex]
before = ''
after = ''
const code = alignments[columnIndex]
let before = ''
let after = ''

@@ -125,4 +246,4 @@ if (code === 99 /* `c` */) {

// There *must* be at least one hyphen-minus in each alignment cell.
size =
settings.alignDelimiters === false
let size =
options.alignDelimiters === false
? 1

@@ -134,5 +255,5 @@ : Math.max(

cell = before + '-'.repeat(size) + after
const cell = before + '-'.repeat(size) + after
if (settings.alignDelimiters !== false) {
if (options.alignDelimiters !== false) {
size = before.length + size + after.length

@@ -155,19 +276,21 @@

rowIndex = -1
/** @type {string[]} */
/** @type {Array<string>} */
const lines = []
while (++rowIndex < cellMatrix.length) {
row = cellMatrix[rowIndex]
sizes = sizeMatrix[rowIndex]
const row = cellMatrix[rowIndex]
const sizes = sizeMatrix[rowIndex]
columnIndex = -1
line = []
/** @type {Array<string>} */
const line = []
while (++columnIndex < mostCellsPerRow) {
cell = row[columnIndex] || ''
before = ''
after = ''
const cell = row[columnIndex] || ''
let before = ''
let after = ''
if (settings.alignDelimiters !== false) {
size = longestCellByColumn[columnIndex] - (sizes[columnIndex] || 0)
code = alignments[columnIndex]
if (options.alignDelimiters !== false) {
const size =
longestCellByColumn[columnIndex] - (sizes[columnIndex] || 0)
const code = alignments[columnIndex]

@@ -189,3 +312,3 @@ if (code === 114 /* `r` */) {

if (settings.delimiterStart !== false && !columnIndex) {
if (options.delimiterStart !== false && !columnIndex) {
line.push('|')

@@ -195,7 +318,7 @@ }

if (
settings.padding !== false &&
options.padding !== false &&
// Don’t add the opening space if we’re not aligning and the cell is
// empty: there will be a closing space.
!(settings.alignDelimiters === false && cell === '') &&
(settings.delimiterStart !== false || columnIndex)
!(options.alignDelimiters === false && cell === '') &&
(options.delimiterStart !== false || columnIndex)
) {

@@ -205,3 +328,3 @@ line.push(' ')

if (settings.alignDelimiters !== false) {
if (options.alignDelimiters !== false) {
line.push(before)

@@ -212,7 +335,7 @@ }

if (settings.alignDelimiters !== false) {
if (options.alignDelimiters !== false) {
line.push(after)
}
if (settings.padding !== false) {
if (options.padding !== false) {
line.push(' ')

@@ -222,3 +345,3 @@ }

if (
settings.delimiterEnd !== false ||
options.delimiterEnd !== false ||
columnIndex !== mostCellsPerRow - 1

@@ -231,3 +354,3 @@ ) {

lines.push(
settings.delimiterEnd === false
options.delimiterEnd === false
? line.join('').replace(/ +$/, '')

@@ -262,3 +385,3 @@ : line.join('')

function toAlignment(value) {
const code = typeof value === 'string' ? value.charCodeAt(0) : 0
const code = typeof value === 'string' ? value.codePointAt(0) : 0

@@ -265,0 +388,0 @@ return code === 67 /* `C` */ || code === 99 /* `c` */

{
"name": "markdown-table",
"version": "3.0.1",
"description": "Markdown tables",
"version": "3.0.2",
"description": "Generate a markdown (GFM) table",
"license": "MIT",

@@ -35,6 +35,6 @@ "keywords": [

"c8": "^7.0.0",
"chalk": "^4.0.0",
"chalk": "^5.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"remark-cli": "^10.0.0",
"remark-preset-wooorm": "^9.0.0",
"rimraf": "^3.0.0",

@@ -45,10 +45,10 @@ "strip-ansi": "^7.0.0",

"typescript": "^4.0.0",
"xo": "^0.39.0"
"xo": "^0.47.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"prepublishOnly": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"

@@ -55,0 +55,0 @@ },

@@ -8,11 +8,40 @@ # markdown-table

Generate fancy [Markdown][fancy] tables.
Generate a markdown ([GFM][]) table.
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`markdownTable(table[, options])`](#markdowntabletable-options)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Inspiration](#inspiration)
* [Contribute](#contribute)
* [License](#license)
## What is this?
This is a simple package that takes table data and generates a [GitHub flavored
markdown (GFM)][gfm] table.
## When should I use this?
You can use this package when you want to generate the source code of a GFM
table from some data.
This is a simple solution in that it doesn’t handle escapes or HTML or any of
that.
For a complete but heavier solution, build an AST and serialize it with
[`mdast-util-to-markdown`][mdast-util-to-markdown] (with
[`mdast-util-gfm`][mdast-util-gfm]).
## Install
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
[npm][]:
```sh

@@ -22,2 +51,16 @@ npm install markdown-table

In Deno with [Skypack][]:
```js
import {markdownTable} from 'https://cdn.skypack.dev/markdown-table@3?dts'
```
In browsers with [Skypack][]:
```html
<script type="module">
import {markdownTable} from 'https://cdn.skypack.dev/markdown-table@3?min'
</script>
```
## Use

@@ -79,10 +122,12 @@

Turns a given matrix of strings (an array of arrays of strings) into a table.
Generate a markdown table from table data (matrix of strings).
##### `options`
Configuration (optional).
###### `options.align`
One style for all columns, or styles for their respective columns (`string` or
`string[]`).
`Array<string>`).
Each style is either `'l'` (left), `'r'` (right), or `'c'` (center).

@@ -118,4 +163,4 @@ Other values are treated as `''`, which doesn’t place the colon in the alignment

Note: please don’t use this: it could create fragile structures that aren’t
understandable to some Markdown parsers.
> 👉 **Note**: please don’t use this: it could create fragile structures that
> aren’t understandable to some markdown parsers.

@@ -142,4 +187,4 @@ When `true`, there are starting delimiters:

Note: please don’t use this: it could create fragile structures that aren’t
understandable to some Markdown parsers.
> 👉 **Note**: please don’t use this: it could create fragile structures that
> aren’t understandable to some markdown parsers.

@@ -183,9 +228,11 @@ When `true`, there are ending delimiters:

Method to detect the length of a cell (`Function`, default: `s => s.length`).
Function to detect the length of table cell content (`Function`, default:
`s => s.length`).
This is used when aligning the delimiters (`|`) between table cells.
Full-width characters and emoji mess up delimiter alignment when viewing the
markdown source.
To fix this, you can pass this function, which receives the cell content and
returns its “visible” size.
Note that what is and isn’t visible depends on where the text is displayed.
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:

@@ -221,3 +268,3 @@

],
{stringLength: width}
{stringLength: stringWidth}
)

@@ -235,2 +282,17 @@ ```

## Types
This package is fully typed with [TypeScript][].
It exports additional `Options` type that models its respective interface.
## Compatibility
This package is at least compatible with all maintained versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
It also works in Deno and modern browsers.
## Security
This package is safe.
## Inspiration

@@ -241,2 +303,7 @@

## Contribute
Yes please!
See [How to Contribute to Open Source][contribute].
## License

@@ -266,2 +333,4 @@

[skypack]: https://www.skypack.dev
[license]: license

@@ -271,6 +340,16 @@

[fancy]: https://help.github.com/articles/github-flavored-markdown/#tables
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[typescript]: https://www.typescriptlang.org
[contribute]: https://opensource.guide/how-to-contribute/
[gfm]: https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables
[text-table]: https://github.com/substack/text-table
[string-width]: https://github.com/sindresorhus/string-width
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
[mdast-util-gfm]: https://github.com/syntax-tree/mdast-util-gfm
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc