highlight method

String highlight(
  1. String source
)

Highlights source Markdown into HTML with syntax spans.

Uses MarkdownGrammarDefinition.defaultParser to parse the AST and extracts start/stop positions from the nodes. If parsing fails, returns HTML-escaped source.

Implementation

String highlight(String source) {
  if (source.isEmpty) return '';
  final result = MarkdownGrammarDefinition.defaultParser.parse(source);
  if (result is Success<DocumentNode>) {
    return highlightAst(result.value, source);
  }
  return _escape(source);
}