parse method

  1. @override
Node parse(
  1. BlockParser parser
)
override

Implementation

@override
Node parse(BlockParser parser) {
  // Get the syntax identifier, if there is one.
  final match = pattern.firstMatch(parser.current)!;
  final endBlock = match.group(1);
  var infoString = match.group(2)!;

  final childLines = parseChildLines(parser, endBlock)
    // The Markdown tests expect a trailing newline.
    ..add('');

  final code = Element.text('code', childLines.join('\n'));

  // the info-string should be trimmed
  // http://spec.commonmark.org/0.22/#example-100
  infoString = infoString.trim();
  if (infoString.isNotEmpty) {
    // only use the first word in the syntax
    // http://spec.commonmark.org/0.22/#example-100
    infoString = infoString.split(' ').first;
    code.attributes['class'] = 'language-$infoString';
  }

  final element = Element('pre', [code]);
  return element;
}