Markdown Compiler

stringify(ast)

The stringify function serializes a Document AST or node structure back into canonical Markdown text.

Signature

const { stringify } = require('@sullux/markdown-compiler')

const markdownText = stringify(ast)

Parameters

  • ast (Document | BlockNode | Array<BlockNode>):
    • Can be a full Document object ({ frontmatter, blocks }), a single Block node, or an array of nodes.

Returns

  • string: Canonical Markdown representation with proper indentation, headers, block boundaries, and frontmatter.

Behavior & Features

1. Frontmatter Serialization

When the provided AST includes a frontmatter object with keys, it is serialized as YAML enclosed in --- frontmatter blocks:

const { stringify, Node } = require('@sullux/markdown-compiler')

const doc = {
  frontmatter: {
    title: 'Release Notes',
    published: true,
    version: '1.0.1',
  },
  blocks: [
    Node.header(1, [Node.text('Release Notes')]),
    Node.paragraph([Node.text('Bug fixes and stability improvements.')]),
  ],
}

console.log(stringify(doc))

Output:

---
title: Release Notes
published: true
version: 1.0.1
---

# Release Notes

Bug fixes and stability improvements.

2. GFM Table Formatting

Tables are rendered with aligned column pipes and proper alignment delimiters (:---, :---:, ---:):

const { stringify, Node } = require('@sullux/markdown-compiler')

const table = Node.table(
  ['left', 'center', 'right'],
  [
    [Node.text('Metric'), Node.text('Status'), Node.text('Value')],
    [Node.text('Latency'), Node.text('PASS'), Node.text('1.4ms')],
    [Node.text('Throughput'), Node.text('PASS'), Node.text('27 tok/s')],
  ]
)

console.log(stringify(table))

Output:

| Metric     | Status |     Value |
| :--------- | :----: | --------: |
| Latency    |  PASS  |     1.4ms |
| Throughput |  PASS  |  27 tok/s |

3. Nested List & Container Formatting

Hierarchical lists adhering to CommonMark container structures preserve nesting depth, numbering, task checkboxes, and tight vs. loose blank line spacing:

const { stringify, Node } = require('@sullux/markdown-compiler')

const list = Node.orderedList([
  Node.listItem([Node.paragraph([Node.text('Setup environment')])]),
  Node.listItem([
    Node.paragraph([Node.text('Build binary')]),
    Node.bulletList([
      Node.listItem([Node.paragraph([Node.text('Check drivers')])], true),
      Node.listItem([Node.paragraph([Node.text('Verify tools')])], false),
    ]),
  ]),
  Node.listItem([Node.paragraph([Node.text('Launch service')])]),
])

console.log(stringify(list))

Output:

1. Setup environment
2. Build binary
  * [x] Check drivers
  * [ ] Verify tools
3. Launch service