stringToLinkHtml static method

String stringToLinkHtml(
  1. String str, {
  2. double width = 0.0,
  3. double height = 0.0,
})

Implementation

static String stringToLinkHtml(String str, {double width = 0.0, double height = 0.0}) {
  String result = str;
  if (str.length > 2) {
    String tmp = str;
    RegExp exp = RegExp(CoreConstants.patternLinkHtml);
    Iterable<RegExpMatch> matches = exp.allMatches(str.toLowerCase());
    for (var match in matches) {
      String url = tmp.substring(match.start, match.end);
      if (url.contains('..') || double.tryParse(url) != null) {continue;}
      else {
        String symbol = '';
        if (match.start > 0) symbol = tmp.substring(match.start - 1, match.start);
        if (!symbol.contains('#') && url.length > 3) {
          String http = url;
          if (!url.toLowerCase().contains('http')) http = 'https://$url';
          result = result.replaceFirst(
            url,
            "<a href='$http'>$url</a>",
          );
        }
      }
    }
  }
  return result;
}