markdownToHtml function

String markdownToHtml(
  1. String markdown, {
  2. bool normalize = true,
  3. Iterable<BlockSyntax>? blockSyntaxes,
  4. Iterable<InlineSyntax>? inlineSyntaxes,
  5. ExtensionSet? extensionSet,
  6. Resolver? linkResolver,
  7. Resolver? imageLinkResolver,
  8. bool inlineOnly = false,
})

Converts a markdown document into a HTML string.

markdown The markdown document. normalize If true normalizes indent.

Implementation

String markdownToHtml(String markdown,
    {bool normalize = true,
    Iterable<mk.BlockSyntax>? blockSyntaxes,
    Iterable<mk.InlineSyntax>? inlineSyntaxes,
    mk.ExtensionSet? extensionSet,
    mk.Resolver? linkResolver,
    mk.Resolver? imageLinkResolver,
    bool inlineOnly = false}) {
  if (markdown.isEmpty) return '';
  if (normalize) markdown = normalizeIndent(markdown);

  var markdownHtml = mk.markdownToHtml(markdown,
      blockSyntaxes: blockSyntaxes ?? [],
      inlineSyntaxes: inlineSyntaxes ?? [],
      extensionSet: extensionSet,
      linkResolver: linkResolver,
      imageLinkResolver: imageLinkResolver,
      inlineOnly: inlineOnly);

  // allow attributes for url. For example:
  // [GitHub](https://github.com/){:target="_blank"}
  markdownHtml = regExpReplaceAll(
      RegExp(r'(<a.*?)(>.*?</a>){:(.*?)}',
          multiLine: false, caseSensitive: false),
      markdownHtml,
      r'$1 $3$2');

  return markdownHtml;
}