# complex-html-tablify

> Create complex HTML table from YAML

Latest version **1.1.0** (published 2022-04-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install complex-html-tablify
pnpm add complex-html-tablify
yarn add complex-html-tablify
bun add complex-html-tablify
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2022-04-20 |
| First published | 2022-04-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 60.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | nosuke23 |
| Maintainers | nosuke23 |
| Keywords | YAML, HTML, table, parser |

## Links

- npm: https://www.npmjs.com/package/complex-html-tablify
- Repository: https://github.com/nosuke23/complex-html-tablify
- Homepage: https://github.com/nosuke23/complex-html-tablify#readme
- Issues: https://github.com/nosuke23/complex-html-tablify/issues
- npm.io page: https://npm.io/package/complex-html-tablify

## Dependencies (2)

- [yaml](https://npm.io/package/yaml.md) ^1.10.2
- [ramda](https://npm.io/package/ramda.md) ^0.28.0

## Alternatives

- [monaco-yaml](https://npm.io/package/monaco-yaml.md) — 420.1K weekly downloads
- [@crewx/workflow](https://npm.io/package/@crewx/workflow.md) — 3.1K weekly downloads
- [yaml-cat](https://npm.io/package/yaml-cat.md) — 38 weekly downloads
- [nunjucks-in-yaml](https://npm.io/package/nunjucks-in-yaml.md) — 9 weekly downloads
- [shopify-symlinks](https://npm.io/package/shopify-symlinks.md) — 3 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2022-04-20

## README

# Complex HTML Tablify

Complex HTML Tablify provide a human friendly way to write complex HTML table by YAML syntax and simple tags.

## Install

```
npm install complex-html-tablify
```

## Usage Examples

### Simple

#### [Table with two tier headers](https://www.w3.org/WAI/tutorials/tables/irregular/)

```javascript
import { tablifyFromYamlString } from "complex-html-tablify"

const data = `
# The first document is treated as a table header
-
  - !td
  - Mars
  - !cs
  - Venus
  - !cs
-
  - !rs
  - &h1 Produced
  - &h2 Sold
  - *h1
  - *h2
---
-
  - !th Teddy Bears
  - 50,000
  - 30,000
  - 100,000
  - 80,000
-
  - !th Board Games
  - 10,000
  - 5,000
  - 12,000
  - 9,000
`

const caption = "Table with two tier headers"
const options = {
  caption,
}

const htmlTable = tablifyFromYamlString(data, options) //-> HTMLTableElement
const htmlString = htmlTable.outerHTML //-> <table>...</table>
```

output:

<table>
    <caption>Table with two tier headers</caption>
    <col>
    <colgroup span="2"></colgroup>
    <colgroup span="2"></colgroup>
    <thead>
        <tr>
            <td rowspan="2"></td>
            <th colspan="2">Mars</th>
            <th colspan="2">Venus</th>
        </tr>
        <tr>
            <th>Produced</th>
            <th>Sold</th>
            <th>Produced</th>
            <th>Sold</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>Teddy Bears</th>
            <td>50,000</td>
            <td>30,000</td>
            <td>100,000</td>
            <td>80,000</td>
        </tr>
        <tr>
            <th>Board Games</th>
            <td>10,000</td>
            <td>5,000</td>
            <td>12,000</td>
            <td>9,000</td>
        </tr>
    </tbody>
</table>

<details>
<summary>HTML</summary>

```html
<table>
  <caption>
    Table with two tier headers
  </caption>
  <col />
  <colgroup span="2"></colgroup>
  <colgroup span="2"></colgroup>
  <thead>
    <tr>
      <td rowspan="2"></td>
      <th colspan="2">Mars</th>
      <th colspan="2">Venus</th>
    </tr>
    <tr>
      <th>Produced</th>
      <th>Sold</th>
      <th>Produced</th>
      <th>Sold</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Teddy Bears</th>
      <td>50,000</td>
      <td>30,000</td>
      <td>100,000</td>
      <td>80,000</td>
    </tr>
    <tr>
      <th>Board Games</th>
      <td>10,000</td>
      <td>5,000</td>
      <td>12,000</td>
      <td>9,000</td>
    </tr>
  </tbody>
</table>
```

</details>

### Data with Headers

#### [Table with headers spanning multiple rows or columns](https://www.w3.org/WAI/tutorials/tables/irregular/)

```javascript
import { tablifyWithHeader } from "complex-html-tablify"

const firstLevelHeader = `
-
  - Poster name
  - Color
  - Sizes available
  - !cs
  - !cs
`
const secondLevelHeader = `
-
  - !th Zodiac
  - !th Full color
-
  - !rs
  - !th Black and white
- 
  - !rs
  - !th Sepia
- 
  - !th Angels
  - !th Black and white
- 
  - !rs
  - !th Sepia
`
const header = [firstLevelHeader, secondLevelHeader]

const data = [
  ["A2", "A3", "A4"],
  ["A1", "A2", "A3"],
  ["A3", "A4", "A5"],
  ["A1", "A3", "A4"],
  ["A2", "A3", "A5"],
]

const caption = "Poster availability"
const options = { caption }

const htmlTable = tablifyWithHeader(data, header, options)
const htmlString = htmlTable.outerHTML
```

output:

<table>
    <caption>Poster availability</caption>
    <col>
    <col>
    <colgroup span="3"></colgroup>
    <thead>
        <tr>
            <th>Poster name</th>
            <th>Color</th>
            <th colspan="3">Sizes available</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th rowspan="3">Zodiac</th>
            <th>Full color</th>
            <td>A2</td>
            <td>A3</td>
            <td>A4</td>
        </tr>
        <tr>
            <th>Black and white</th>
            <td>A1</td>
            <td>A2</td>
            <td>A3</td>
        </tr>
        <tr>
            <th>Sepia</th>
            <td>A3</td>
            <td>A4</td>
            <td>A5</td>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <th rowspan="2">Angels</th>
            <th>Black and white</th>
            <td>A1</td>
            <td>A3</td>
            <td>A4</td>
        </tr>
        <tr>
            <th>Sepia</th>
            <td>A2</td>
            <td>A3</td>
            <td>A5</td>
        </tr>
    </tbody>
</table>

<details>
<summary>HTML</summary>

```html
<table>
  <caption>
    Poster availability
  </caption>
  <col />
  <col />
  <colgroup span="3"></colgroup>
  <thead>
    <tr>
      <th>Poster name</th>
      <th>Color</th>
      <th colspan="3">Sizes available</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th rowspan="3">Zodiac</th>
      <th>Full color</th>
      <td>A2</td>
      <td>A3</td>
      <td>A4</td>
    </tr>
    <tr>
      <th>Black and white</th>
      <td>A1</td>
      <td>A2</td>
      <td>A3</td>
    </tr>
    <tr>
      <th>Sepia</th>
      <td>A3</td>
      <td>A4</td>
      <td>A5</td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <th rowspan="2">Angels</th>
      <th>Black and white</th>
      <td>A1</td>
      <td>A3</td>
      <td>A4</td>
    </tr>
    <tr>
      <th>Sepia</th>
      <td>A2</td>
      <td>A3</td>
      <td>A5</td>
    </tr>
  </tbody>
</table>
```

</table>
</details>

Note: This library consists functional components, so you can use those in compbination.

## Custom YAML Tags

### HTML Tag

- `!th`: Table Header. The cell with `!th` tag regarded as a header.
- `!td`: Table Data. The cell with `!th` tag regarded as a data.

### Span

You can bind cells to an above or a left cell.

- `!rs`: Row Span. The cell with `!rs` tag is bound to the above cell together.
- `!cs`: Column Span. The cell with `!cs` tag is bound to the left cell together.

## Options

- callback: `(val: any) => string` (default: <code>(val) => \`${val}\`</code>) - Evaluate each value of cells.

- sanitized: `boolean` (default: `true`) - Unenable HTML tags in each cell.

- alignments: `Array<{horizontal?: string; vertical?: string}>` (default: `[]`) - Align each cells of columns.

  - horiznotal: - Using the keyword values: `start`, `end`, `left`, `right`, `center`, `justify`, `justify-all`, or `match-parent`.
  - vertical: - Using the keyword values: `baseline`, `sub`, `super`, `text-top`, `text-bottom`, `middle`, `top`, or `bottom`.

- caption: `string | null` (default: `null`) - Define a table caption.

- defaultCellTag: `{[key in HTMLTableSectionElementTagName]?: HTMLTableCellElementTagName}` (default: `{ thead: "th", tbody: "td", tfoot: "td" }`) - Define each default cell tag of sections.

  - HTMLTableSectionElementTagName: `thead`, `tbody` or `tfoot`
  - HTMLTableCellElementTagName: `th` or `td`

---
_Source: https://npm.io/package/complex-html-tablify · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
