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
3.8 KiB Raw
/**
 * GFM task list item extension (`- [ ] todo`, `- [x] done`).
 */

import {GrowableArray, StringReader, some} from 'zena:core';
import {Block, Document, Inline, Paragraph, BlockQuote, ListBlock, ListItem, Text, SoftBreak, HardBreak, Span} from '../ast.zena';
import {AstTransformer, BlockParserFactory, DelimiterProcessor, InlineContentParser, InlineParserRunner, MarkdownExtension, NodeRenderer} from '../plugin.zena';

final class TaskListTransformer implements AstTransformer {
  transform(doc: Document, inlineParser: InlineParserRunner): void {
    this.#visitBlocks(doc.blocks);
  }

  #visitBlocks(blocks: GrowableArray<Block>): void {
    for (let block in blocks) {
      match (block) {
        case BlockQuote {blocks: let children}: {
          this.#visitBlocks(children);
        }
        case ListBlock {items}: {
          for (let item in items) {
            this.#processItem(item);
            this.#visitBlocks(item.blocks);
          }
        }
        case ListItem as item: {
          this.#processItem(item);
          this.#visitBlocks(item.blocks);
        }
        case _: {}
      };
    }
  }

  #processItem(item: ListItem): void {
    if (item.blocks.length == 0) {
      return;
    }
    if (let Paragraph {inlines} = item.blocks[0]) {
      if (inlines.length == 0) {
        return;
      }
      if (let Text {literal, span} = inlines[0]) {
        let r = new StringReader(literal);
        if (!r.matchCodePoint(0x5B)) {
          // '['
          return;
        }
        let mark = r.advance();
        var checked = false;
        if (mark == 0x20) {
          // ' '
          checked = false;
        } else if (mark == 0x78 || mark == 0x58) {
          // 'x' or 'X'
          checked = true;
        } else {
          return;
        }
        if (!r.matchCodePoint(0x5D)) {
          // ']'
          return;
        }

        // Must be followed by space/tab, or end of Text node when followed by
        // another inline (e.g. `SoftBreak` or `Emphasis`)
        if (!r.isAtEnd) {
          let after = r.peek();
          if (after != 0x20 && after != 0x09) {
            return;
          }
          r.skipSpaces();
          let restOffset = r.mark();
          let rest = r.readToEnd();
          item.checked = some(checked);
          if (rest.isEmpty && inlines.length > 1) {
            this.#removeFirstInline(inlines);
          } else {
            inlines[0] = new Text(
              rest,
              new Span(span.start + restOffset, span.end)
            );
          }
        } else if (inlines.length > 1) {
          item.checked = some(checked);
          this.#removeFirstInline(inlines);
          if (inlines.length > 0) {
            if (inlines[0] is SoftBreak || inlines[0] is HardBreak) {
              this.#removeFirstInline(inlines);
            } else if (let Text {literal as nextLit, span as nextSpan} = inlines[0]) {
              let nextReader = new StringReader(nextLit);
              if (nextReader.matchCodePoint(0x20)) {
                inlines[0] = new Text(
                  nextReader.readToEnd(),
                  new Span(nextSpan.start + 1, nextSpan.end)
                );
              }
            }
          }
        }
      }
    }
  }

  #removeFirstInline(inlines: GrowableArray<Inline>): void {
    var i = 1;
    while (i < inlines.length) {
      inlines[i - 1] = inlines[i];
      i += 1;
    }
    inlines.pop();
  }
}

/**
 * Returns the `MarkdownExtension` for GFM task list items (`- [ ]` and `- [x]`).
 */
export function taskListExtension(): MarkdownExtension {
  let transformers = new GrowableArray<AstTransformer>(1);
  transformers.push(new TaskListTransformer());
  return new MarkdownExtension(
    new GrowableArray<BlockParserFactory>(0),
    new GrowableArray<DelimiterProcessor>(0),
    new GrowableArray<InlineContentParser>(0),
    transformers,
    new GrowableArray<NodeRenderer>(0)
  );
}
none
VersionPublishedDependenciesApproved by
0.0.1-preview.1 2026-11-19 none