InlineParser constructor

InlineParser(
  1. String source,
  2. Document document
)

Implementation

InlineParser(super.source, this.document): syntaxes = [] {
  // User specified syntaxes are the first syntaxes to be evaluated.
  syntaxes.addAll(document.inlineSyntaxes);

  // This first RegExp matches plain text to accelerate parsing. It's written
  // so that it does not match any prefix of any following syntaxes. Most
  // Markdown is plain text, so it's faster to match one RegExp per 'word'
  // rather than fail to match all the following RegExps at each non-syntax
  // character position.
  // The `(?=\s|$)` lookahead lets the run stop before whitespace or at the
  // end of a line. Without the `$` alternative a run that reaches the end of
  // a line without a trailing whitespace (for example a single long word)
  // fails the lookahead only after the greedy `*`/`+` has consumed the rest
  // of the line, then backtracks one character at a time, and the parser
  // retries from the next position, giving quadratic behavior on long
  // unbroken runs. Allowing the run to end at `$` makes it match in one step.
  if (document.hasCustomInlineSyntaxes) {
    // We should be less aggressive in blowing past "words".
    syntaxes.add(TextSyntax(r'[A-Za-z0-9]+(?=\s|$)'));
  } else {
    syntaxes.add(TextSyntax(r'[ \tA-Za-z0-9]*[A-Za-z0-9](?=\s|$)'));
  }

  if (document.withDefaultInlineSyntaxes) {
    // Custom link resolvers go after the generic text syntax.
    syntaxes.addAll([
      EscapeSyntax(),
      DecodeHtmlSyntax(),
      LinkSyntax(linkResolver: document.linkResolver),
      ImageSyntax(linkResolver: document.imageLinkResolver)
    ]);

    syntaxes.addAll(_defaultSyntaxes);
  }

  if (encodeHtml) {
    syntaxes.addAll([
      EscapeHtmlSyntax(),
      // Leave already-encoded HTML entities alone. Ensures we don't turn
      // "&" into "&"
      TextSyntax('&[#a-zA-Z0-9]*;', startCharacter: $ampersand),
    ]);
  }
}