imageMarkdownTransformer function

TextMatchTransformer imageMarkdownTransformer({
  1. double maxWidth = markdownImageMaxWidth,
})

The image rule, with a maxWidth of your choosing.

The default is upstream's, so imageMarkdownTransformer() and imageTransformer are the same rule. Pass a different width when a document has to match what another implementation writes for the same markdown — a server-side converter, say. The alternative is copying the rule into the application, and a copied rule is one that stops matching this one the first time either changes.

Implementation

TextMatchTransformer imageMarkdownTransformer({
  double maxWidth = markdownImageMaxWidth,
}) => TextMatchTransformer(
  // The `!` has to be part of the match. Without it the link rule claims the
  // `[alt](src)` that follows, and the image arrives as a link with a stray
  // exclamation mark in front of it.
  regExp: RegExp(r'!\[([^\[\]]*)\]\(([^)\s]+)\)'),
  replace: (match, format) => $createImageNode(
    src: match.group(2)!,
    altText: match.group(1)!,
    maxWidth: maxWidth,
  ),
  export: (node, exportChildren) {
    if (node is! ImageNode) return null;
    return '![${node.altText}](${node.src})';
  },
);