parse-markdown-table

Convert a markdown table from text to JavaScript objects

Usage no npm install needed!

<script type="module">
  import parseMarkdownTable from 'https://cdn.skypack.dev/parse-markdown-table';
</script>

README

Parse Markdown Table

Convert a markdown table from text to JavaScript objects

Usage

ArrayTable

const { createMarkdownArrayTable } = await import('parse-markdown-table')

const markdown = `
  | id | name        | email                    |
  |----|-------------|--------------------------|
  |  1 | John Doe    | john-doe@gmail.com       |
  |  2 | Peter Smith | petersmith22@outlook.com |
  |  3 | Julia Jones | jjones778@gmail.com      |
`

// this function can take string, string[], Iterable<string>, and AsyncIterable<string>
const table = await createMarkdownArrayTable(markdown)

console.info('headers', table.headers)
for await (const row of table.rows) {
  console.info('row', row)
}

Output:

headers [ 'id', 'name', 'email' ]
row [ '1', 'John Doe', 'john-doe@gmail.com' ]
row [ '2', 'Peter Smith', 'petersmith22@outlook.com' ]
row [ '3', 'Julia Jones', 'jjones778@gmail.com' ]

ObjectTable

const { createMarkdownObjectTable } = await import('parse-markdown-table')

const markdown = `
  | id | name        | email                    |
  |----|-------------|--------------------------|
  |  1 | John Doe    | john-doe@gmail.com       |
  |  2 | Peter Smith | petersmith22@outlook.com |
  |  3 | Julia Jones | jjones778@gmail.com      |
`

// this function can take string, string[], Iterable<string>, or AsyncIterable<string>
const table = createMarkdownObjectTable(markdown)

for await (const row of table) {
  console.info('row', row)
}

Output:

row { id: '1', name: 'John Doe', email: 'john-doe@gmail.com' }
row { id: '2', name: 'Peter Smith', email: 'petersmith22@outlook.com' }
row { id: '3', name: 'Julia Jones', email: 'jjones778@gmail.com' }

License

MIT © Hoàng Văn Khải