markdown 0.0.1-preview.1

CommonMark and GFM Markdown parser, AST, plugin system, and HTML renderer

Readme Files Dependencies Versions

Markdown (@zena-lang/markdown)#

A CommonMark and GitHub Flavored Markdown (GFM) parser, syntax tree, plugin system, and HTML renderer written in Zena (zena-lang:markdown/<library>). See docs/design/markdown.md for the design.

Usage#

Add it with zena pkg add zena-lang:markdown, then import its main library:

import {
  parse, renderHtml, markdownToHtml, gfmExtensions
} from 'zena-lang:markdown';

// Render CommonMark + GFM to safe HTML (raw HTML stripped, unsafe URLs blocked)
let html = markdownToHtml('# Hello\n\nSome ~~old~~ **new** text.', {
  extensions: gfmExtensions(),
  safe: true,
});

// Or parse to an AST first
let doc = parse('# Heading\n\nParagraph.', gfmExtensions());
let out = renderHtml(doc, {safe: true, extensions: gfmExtensions()});

Running Tests#

npm test -w @zena-lang/markdown
npm run test:quick -- packages/markdown/test
4.2 KiB Raw
/**
 * Abstract syntax tree types for Markdown documents.
 *
 * Core CommonMark block and inline constructs are variants of the sealed
 * `Block` and `Inline` hierarchies. Extensions define their own AST nodes by
 * subclassing `CustomBlock` or `CustomInline`.
 */

import {GrowableArray, Option, StringBuilder} from 'zena:core';
import {HashMap} from 'zena:collections';

/**
 * A 0-based UTF-8 byte range `[start, end)` in the source string.
 */
export final class Span(start: i32, end: i32)

/**
 * A normalized link reference definition (`[label]: destination "title"`).
 */
export final class LinkDef(label: String, destination: String, title: String, span: Span)

/**
 * The root of a parsed Markdown document.
 */
export final class Document(var blocks: GrowableArray<Block>, links: HashMap<String, LinkDef>, span: Span)

/**
 * Block-level nodes in a Markdown document.
 */
export sealed class Block {
  case Paragraph(var inlines: GrowableArray<Inline>, var id: String | null, span: Span)
  case Heading(level: i32, var inlines: GrowableArray<Inline>, var id: String | null, span: Span)
  case BlockQuote(var blocks: GrowableArray<Block>, span: Span)
  case ListBlock(ordered: boolean, startNumber: i32, delimiter: i32, var tight: boolean, var items: GrowableArray<ListItem>, span: Span)
  case ListItem(var blocks: GrowableArray<Block>, var checked: Option<boolean>, span: Span)
  case FencedCodeBlock(info: String, literal: String, span: Span)
  case IndentedCodeBlock(literal: String, span: Span)
  case HtmlBlock(literal: String, span: Span)
  case ThematicBreak(span: Span)
  case CustomBlock

  abstract span: Span;
}

/**
 * Base class for block nodes defined by syntax extensions (such as GFM tables
 * or GitHub alerts).
 */
export abstract class CustomBlock extends Block {
  span: Span;

  new(this.span) : super() {}
}

/**
 * Inline-level nodes within a block's rich text.
 */
export sealed class Inline {
  case Text(var literal: String, span: Span)
  case CodeSpan(literal: String, span: Span)
  case Emphasis(delimiter: i32, var children: GrowableArray<Inline>, span: Span)
  case Strong(delimiter: i32, var children: GrowableArray<Inline>, span: Span)
  case Link(var destination: String, var title: String, var children: GrowableArray<Inline>, span: Span)
  case Image(var destination: String, var title: String, var children: GrowableArray<Inline>, span: Span)
  case Autolink(var destination: String, var label: String, isEmail: boolean, span: Span)
  case HtmlInline(literal: String, span: Span)
  case SoftBreak(span: Span)
  case HardBreak(span: Span)
  case CustomInline

  abstract span: Span;
}

/**
 * Base class for inline nodes defined by syntax extensions (such as GFM
 * strikethrough).
 */
export abstract class CustomInline extends Inline {
  span: Span;

  new(this.span) : super() {}

  /**
   * Appends the plain-text representation of this custom inline node to `sb`.
   * Used when extracting image `alt` text or heading anchor slugs.
   */
  appendPlainText(sb: StringBuilder): void {}
}

/**
 * Appends the plain text of `inlines` to `sb`, stripping formatting tags while
 * preserving text inside emphasis, links, images, code spans, and custom
 * inlines.
 */
export function appendInlinesPlainText(inlines: GrowableArray<Inline>, sb: StringBuilder): void {
  for (let node in inlines) {
    match (node) {
      case Text {literal}: {
        sb.append(literal);
      }
      case CodeSpan {literal}: {
        sb.append(literal);
      }
      case Emphasis {children}: {
        appendInlinesPlainText(children, sb);
      }
      case Strong {children}: {
        appendInlinesPlainText(children, sb);
      }
      case Link {children}: {
        appendInlinesPlainText(children, sb);
      }
      case Image {children}: {
        appendInlinesPlainText(children, sb);
      }
      case Autolink {label}: {
        sb.append(label);
      }
      case HtmlInline: {}
      case SoftBreak: {
        sb.appendByte(0x0A);
      }
      case HardBreak: {
        sb.appendByte(0x0A);
      }
      case CustomInline as custom: {
        custom.appendPlainText(sb);
      }
    };
  }
}

/**
 * Returns the plain-text content of `inlines`.
 */
export function inlinesToPlainText(inlines: GrowableArray<Inline>): String {
  let sb = new StringBuilder(32);
  appendInlinesPlainText(inlines, sb);
  return sb.toString();
}
none
VersionPublishedDependenciesApproved by
0.0.1-preview.1 2026-11-19 none