parse method

  1. @override
List<LinkifyElement> parse(
  1. List<LinkifyElement> elements,
  2. LinkifyOptions options
)
override

Implementation

@override
List<LinkifyElement> parse(elements, options) {
  final list = <LinkifyElement>[];

  for (var element in elements) {
    if (element is TextElement) {
      final match = _emailRegex.firstMatch(element.text);

      if (match == null) {
        list.add(element);
      } else {
        final text = element.text.replaceFirst(match.group(0)!, '');

        if (match.group(1)?.isNotEmpty == true) {
          list.add(TextElement(match.group(1)!));
        }

        if (match.group(2)?.isNotEmpty == true) {
          // Always humanize emails
          list.add(EmailElement(
            match.group(2)!.replaceFirst(RegExp(r'mailto:'), ''),
          ));
        }

        if (text.isNotEmpty) {
          list.addAll(parse([TextElement(text)], options));
        }
      }
    } else {
      list.add(element);
    }
  }

  return list;
}