parse method

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

Implementation

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

  var childLines = parseChildLines(parser, endBlock);

  // The Markdown tests expect a trailing newline.
  childLines.add('');

  var text = childLines.join('\n');
  if (parser.document.encodeHtml) {
    text = escapeHtml(text);
  }
  var code = Element.text('code', text);

  // 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
    var firstSpace = infoString.indexOf(' ');
    if (firstSpace >= 0) {
      infoString = infoString.substring(0, firstSpace);
    }
    if (parser.document.encodeHtml) {
      infoString = escapeHtmlAttribute(infoString);
    }
    code.attributes['class'] = getLanguageClass(infoString);
  }

  var element = Element('pre', [code]);

  return element;
}