getFirstValidDomain function

String getFirstValidDomain(
  1. String source
)

Returns the first ASCII-only domain found in source.

Internationalized domain names (IDNs) such as 日本語.jp contain no ASCII-only label, so validAsciiDomainRegex finds no match. In that case the original source is returned unchanged instead of dereferencing a null match (which previously threw a TypeError and made .links, .entities, .format() and markdown links crash on any IDN URL).

Implementation

String getFirstValidDomain(final String source) =>
    validAsciiDomainRegex.firstMatch(source)?.group(0) ?? source;