MingaAgent.Markdown (Minga v0.1.0)

Copy Markdown View Source

Simple markdown parser for agent chat rendering.

Parses a subset of markdown into styled line segments suitable for terminal rendering. This is intentionally not a full CommonMark parser; it handles the patterns that LLM output commonly uses.

Supported syntax

  • **bold** and __bold__
  • *italic* and _italic_
  • `inline code`
  • Fenced code blocks (`` with optional language tag) -# Headers(levels 1-3) -- list itemsand list items-> blockquotes- Horizontal rules (---,**,_) ## Output format Returns a list of{line_segments, line_type}tuples where eachline_segmentsis a list of{text, style}pairs andline_type` indicates the block context.

Summary

Types

Extracted code block with language and content.

Line type indicating block context.

A parsed line with its segments and type.

A styled text segment.

Style attributes for a text segment.

Functions

Extracts fenced code blocks from markdown text.

Parses markdown text into styled line segments.

Parses inline markdown formatting within a single line.

Types

code_block()

@type code_block() :: %{language: String.t(), content: String.t()}

Extracted code block with language and content.

line_type()

@type line_type() ::
  :text
  | :code
  | {:code_header, String.t()}
  | :header
  | :blockquote
  | :list_item
  | :rule
  | :empty

Line type indicating block context.

parsed_line()

@type parsed_line() :: {[segment()], line_type()}

A parsed line with its segments and type.

segment()

@type segment() :: {String.t(), style()}

A styled text segment.

style()

@type style() ::
  :plain
  | :bold
  | :italic
  | :bold_italic
  | :code
  | :code_block
  | {:code_content, String.t()}
  | :header1
  | :header2
  | :header3
  | :blockquote
  | :list_bullet
  | :rule

Style attributes for a text segment.

Functions

extract_code_blocks(text)

@spec extract_code_blocks(String.t()) :: [code_block()]

Extracts fenced code blocks from markdown text.

Returns a list of %{language: String.t(), content: String.t()} maps, one per fenced code block. The content excludes the fence markers.

parse(text)

@spec parse(String.t()) :: [parsed_line()]

Parses markdown text into styled line segments.

Returns a list of {segments, line_type} tuples, one per output line.

parse_inline(text)

@spec parse_inline(String.t()) :: [segment()]

Parses inline markdown formatting within a single line.

Returns a list of {text, style} segments.