Skip to main content
Version: 3.x.x 🚧

@yozora/tokenizer-table

Npm VersionNpm DownloadNpm LicenseModule formats: cjs, esmNode.js VersionTested with JestCode Style: prettier

github flavor markdown spec

GFM enables the table extension, where an additional leaf block type is available.

A table is an arrangement of data with rows and columns, consisting of a single header row, a delimiter row separating the header from the data, and zero or more data rows.

Each row consists of cells containing arbitrary text, in which inlines are parsed, separated by pipes (|). A leading and trailing pipe is also recommended for clarity of reading, and if there’s otherwise parsing ambiguity. Spaces between pipes and cell content are trimmed. Block-level elements cannot be inserted in a table.

The delimiter row consists of cells whose only content are hyphens (-), and optionally, a leading or trailing colon (:), or both, to indicate left, right, or center alignment respectively.

Install

npm install --save @yozora/tokenizer-table

Usage

tip

@yozora/tokenizer-table has been integrated into @yozora/parser / @yozora/parser-gfm-ex, so you can use YozoraParser / GfmExParser directly.

import YozoraParser from '@yozora/parser'

const parser = new YozoraParser()

// parse source markdown content
parser.parse(`
| foo | bar |
| --- | --- |
| baz | bim |
`)

Options

NameTypeRequiredDefault
namestringfalse"@yozora/tokenizer-table"
prioritynumberfalseTokenizerPriority.INTERRUPTABLE_BLOCK
  • name: The unique name of the tokenizer, used to bind the token it generates, to determine the tokenizer that should be called in each life cycle of the token in the entire matching / parsing phase.

  • priority: Priority of the tokenizer, determine the order of processing, high priority priority execution. interruptable. In addition, in the match-block stage, a high-priority tokenizer can interrupt the matching process of a low-priority tokenizer.

Types

@yozora/tokenizer-table produce Table / [TableRow][node-type-table:cell] / [TableCell][node-type-table:cell] type nodes. See @yozora/ast for full base types.

  • TableCell

    import type { Parent } from '@yozora/ast'

    export const TableCellType = 'tableCell'
    export type TableCellType = typeof TableCellType

    /**
    * TableCell represents a header cell in a Table, if its parent is a head,
    * or a data cell otherwise.
    * @see https://github.com/syntax-tree/mdast#tablecell
    * @see https://github.github.com/gfm/#tables-extension-
    */
    export type TableCell = Parent<TableCellType>
  • TableRow

    import type { Parent } from '@yozora/ast'

    export const TableRowType = 'tableRow'
    export type TableRowType = typeof TableRowType

    /**
    * TableRow represents a row of cells in a table.
    * @see https://github.com/syntax-tree/mdast#tablerow
    * @see https://github.github.com/gfm/#tables-extension-
    */
    export interface TableRow extends Parent<TableRowType> {
    /**
    * Table cells
    */
    children: TableCell[]
    }
  • Table

    import type { AlignType, Parent } from '@yozora/ast'

    export const TableType = 'table'
    export type TableType = typeof TableType

    /**
    * Table column configs.
    */
    export interface TableColumn {
    /**
    * An align field can be present. If present, it must be a list of alignTypes.
    * It represents how cells in columns are aligned.
    */
    align: AlignType
    }

    /**
    * @see https://github.github.com/gfm/#table
    * @see https://github.com/syntax-tree/mdast#table
    */
    export interface Table extends Parent<TableType> {
    /**
    * Table column configuration items
    */
    columns: TableColumn[]
    /**
    * Table rows (include table headers)
    */
    children: TableRow[]
    }

Live Examples

  • Basic.

    #198
      
      
  • Cells in one column don’t need to match length, though it’s easier to read if they are. Likewise, use of leading and trailing pipes may be inconsistent.

    #199
      
      
  • Include a pipe in a cell’s content by escaping it, including inside other inline spans.

    #200
      
      
  • The table is broken at the first empty line, or beginning of another block-level structure.

      
      
  • The header row must match the delimiter row in the number of cells. If not, a table will not be recognized.

    #203
      
      
  • The remainder of the table’s rows may vary in the number of cells. If there are a number of cells fewer than the number of cells in the header row, empty cells are inserted. If there are greater, the excess is ignored.

    #204
      
      
  • If there are no rows in the body, no <tbody> is generated in HTML output.

    #205