/** * 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, links: HashMap, span: Span) /** * Block-level nodes in a Markdown document. */ export sealed class Block { case Paragraph(var inlines: GrowableArray, var id: String | null, span: Span) case Heading(level: i32, var inlines: GrowableArray, var id: String | null, span: Span) case BlockQuote(var blocks: GrowableArray, span: Span) case ListBlock(ordered: boolean, startNumber: i32, delimiter: i32, var tight: boolean, var items: GrowableArray, span: Span) case ListItem(var blocks: GrowableArray, var checked: Option, 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, span: Span) case Strong(delimiter: i32, var children: GrowableArray, span: Span) case Link(var destination: String, var title: String, var children: GrowableArray, span: Span) case Image(var destination: String, var title: String, var children: GrowableArray, 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, 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): String { let sb = new StringBuilder(32); appendInlinesPlainText(inlines, sb); return sb.toString(); }