Skip to content

Commit

Permalink
refactor: normalizeLinkDestination with functional way
Browse files Browse the repository at this point in the history
  • Loading branch information
mym0404 committed Mar 19, 2024
1 parent 2e790e4 commit cd42034
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions lib/src/util.dart
Expand Up @@ -52,23 +52,17 @@ String normalizeLinkDestination(String destination) {
// Refer: https://spec.commonmark.org/0.30/#example-502.

final regex = RegExp('%[0-9A-Fa-f]{2}');
final matches = regex.allMatches(destination).toList();
final splitIterator = destination.split(regex).map((e) {
try {
e = Uri.decodeFull(e);
} catch (_) {}
return Uri.encodeFull(decodeHtmlCharacters(e));
}).iterator;

splitIterator.moveNext();
final buffer = StringBuffer(splitIterator.current);
for (var i = 0; i < matches.length; i++) {
splitIterator.moveNext();
buffer.write(matches[i].match);
buffer.write(splitIterator.current);
}

return buffer.toString();
return destination.splitMapJoin(
regex,
onMatch: (m) => m.match,
onNonMatch: (e) {
try {
e = Uri.decodeFull(e);
} catch (_) {}
return Uri.encodeFull(decodeHtmlCharacters(e));
},
);
}

/// Normalizes a link title, including the process of HTML characters decoding
Expand Down

0 comments on commit cd42034

Please sign in to comment.