kotmark

A Markdown engine
Kotlin Multiplatform

Extension 05 / kotmark-ext-gfm-tables

GFM tables

The table extension adds pipe-table parsing and renderers for HTML, Markdown, and text.

Artifact
io.heapy.kotmark:kotmark-ext-gfm-tables:0.1.0
Package
io.heapy.kotmark.ext.gfm.tables

Usage

Register table syntax and HTML output
import io.heapy.kotmark.ext.gfm.tables.tables
import io.heapy.kotmark.extensions
import io.heapy.kotmark.parser.parser
import io.heapy.kotmark.renderer.html.htmlRenderer

fun renderWithTables(markdown: String): String {
    val extensions = extensions { tables() }
    val document = parser { extensions(extensions) }.parse(markdown)
    return htmlRenderer { extensions(extensions) }.render(document)
}

Example

INPUT · Markdown
| Module | Kind |
| :--- | ---: |
| math | block |
| ins | inline |
OUTPUT · HTML
<table>
<thead>
<tr>
<th align="left">Module</th>
<th align="right">Kind</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">math</td>
<td align="right">block</td>
</tr>
<tr>
<td align="left">ins</td>
<td align="right">inline</td>
</tr>
</tbody>
</table>

Syntax

  • A header row must be followed by a delimiter row; column counts must match.
  • Colons in the delimiter specify left, right, or center alignment.
  • Cells support inline Markdown; escape a literal pipe as \|.
  • The table ends at a blank line or when another block starts.

Cell limit

Each table is limited to 1,000,000 parsed cells by default. Header cells and empty cells added to complete a body row count towards the limit. Parsing throws IllegalArgumentException before creating a cell that would exceed the limit.

Use tables { maxCells = 10_000 } inside extensions { … } to choose a different limit, or set maxCells = null to disable it. The limit applies separately to each table. The standalone tablesExtension { … } factory is also available. tables() and TablesExtension.create() use the default limit.