slug static method

String slug(
  1. String title, {
  2. String separator = '-',
  3. Map<String, String>? dictionary,
})

Generates a URL-friendly slug from title.

Implementation

static String slug(
  String title, {
  String separator = '-',
  Map<String, String>? dictionary,
}) {
  var t = title;
  final dict = {'@': 'at', ...?dictionary};
  dict.forEach((k, v) {
    t = t.replaceAll(k, ' $v ');
  });
  t = t.toLowerCase();
  t = t.replaceAll(RegExp(r'[^\p{L}\p{N}\s_-]+', unicode: true), '');
  final sep = RegExp.escape(separator);
  t = t.replaceAll(RegExp('[\\s${sep}_]+'), separator);
  t = t.replaceAll(RegExp('^$sep+|$sep+\$'), '');
  return t;
}