@joshuaavalon/cheerio-table-parser

Parsing table element of Cheerio into 2D array.

Usage no npm install needed!

<script type="module">
  import joshuaavalonCheerioTableParser from 'https://cdn.skypack.dev/@joshuaavalon/cheerio-table-parser';
</script>

README

Cheerio Table Parser

License Pipelines Coverage NPM

Parsing table element of Cheerio into 2D array.

Installation

npm install @joshuaavalon/cheerio-table-parser

Usage

import cheerio from "cheerio";
import { parseTable } from "@joshuaavalon/cheerio-table-parser";

const dom = cheerio.load(html);
table = parseTable(dom("table")[0], {
  parser: (element) => cheerio(element).text(),
});

/*
[
  ["A", "B", "C", "D", "E"],
  ["1a", "2a", "3a", "4a", "5a"],
  ["1a", "2b", "2b", "4b", "5b"],
  ["1a", "2b", "2b", "4c", "5c"],
  ["1a", "2d", "3d", "4c", "5d"],
  ["1a", "2d", "3e", "4e", "5e"]
]
*/

Known Issues

Overlapping Cell

According to HTML table specifications, it depends on how user agent render the table. Most browsers render it as overlapping cell.

However, this is not possible in this library. Therefore, it will favour rowspan over colspan.

Handle thead, tbody, tfoot

<thead>, <tbody>, <tfoot> should be handled separately. This is not consider in this library. However, you can pass in <tbody> instead of <table> separately.