Socket
Socket
Sign inDemoInstall

markdown-table

Package Overview
Dependencies
0
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    markdown-table

Markdown tables


Version published
Maintainers
1
Install size
16.5 kB
Created

Package description

What is markdown-table?

The markdown-table npm package is a utility for generating tables in Markdown format. It allows users to create well-formatted Markdown tables from arrays of data, providing options to customize alignment and padding.

What are markdown-table's main functionalities?

Basic Table Creation

This feature allows the creation of a basic Markdown table from an array of arrays. Each sub-array represents a row in the table.

const markdownTable = require('markdown-table');

const table = markdownTable([
  ['Branch', 'Commit'],
  ['master', '0123456789abcdef'],
  ['staging', 'fedcba9876543210']
]);

console.log(table);

Custom Alignment

This feature enables the specification of alignment for each column in the table. 'l' stands for left, 'c' for center, and 'r' for right.

const markdownTable = require('markdown-table');

const table = markdownTable(
  [
    ['Feature', 'Support'],
    ['Tables', 'Yes'],
    ['Alignment', 'Yes']
  ],
  { align: ['l', 'c'] }
);

console.log(table);

Other packages similar to markdown-table

Readme

Source

markdown-table

Build Coverage Downloads Size

Generate fancy Markdown tables.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install markdown-table

Use

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 |

API

This package exports the following identifiers: markdownTable. There is no default export.

markdownTable(table[, options])

Turns a given matrix of strings (an array of arrays of strings) into a table.

options
options.align

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.

options.padding

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|
options.delimiterStart

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 |
options.delimiterEnd

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
options.alignDelimiters

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 |
options.stringLength

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   |

Inspiration

The original idea and basic implementation was inspired by James Halliday’s text-table library.

License

MIT © Titus Wormer

Keywords

FAQs

Last updated on 22 Jun 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc