run method

  1. @override
Future<ProcessData> run(
  1. ProcessData data
)
override

Implementation

@override
Future<ProcessData> run(ProcessData data) async {
  // Do not cache remot edata if cacheRemoteAssets is false
  if (!data.config.cacheRemoteAssets) {
    return data;
  }
  // Get any url of images that are in the markdown
  // Save it the local path on the device
  // and replace the url with the local path
  final imageRegex = RegExp(r'!\[.*?\]\((.*?)\)');

  var content = data.content;
  var options = {...data.options};

  final matches = imageRegex.allMatches(data.content);

  for (final Match match in matches) {
    final imageUrl = match.group(1);
    if (imageUrl == null) continue;

    final asset = await cacheRemoteAsset(imageUrl);

    if (asset != null) {
      final imageMarkdown = '![Image](${asset.relativePath})';
      content = content.replaceFirst(match.group(0)!, imageMarkdown);
    }
  }

  // Check also if image is on background: or src: in front matter
  // and replace the url with the local path, frontmatter is now data.options Map<String, dynamic>
  var background = options['background'];

  if (background != null && background is String) {
    final asset = await cacheRemoteAsset(background);

    if (asset != null) {
      background = asset.relativePath;
      options['background'] = background;
    }
  }

  var imageSource = options['options']?['src'];

  if (imageSource != null && imageSource is String) {
    final asset = await cacheRemoteAsset(imageSource);

    if (asset != null) {
      imageSource = asset.relativePath;
      options['options']['src'] = imageSource;
    }
  }

  return (
    content: content,
    options: options,
    config: data.config,
  );
}