dvHeadingSlug function

String dvHeadingSlug(
  1. String text
)

The id a heading gets, so a link can name it: /docs/ui#lay-out-children.

The heading's words, lower case, with everything that is not a letter, a digit or a space dropped and the spaces made hyphens -- the shape GitHub and most documentation sites give a heading, so a link somebody guesses from the words usually works. Letters outside ASCII stay: a French heading's link reads as French.

Implementation

String dvHeadingSlug(String text) {
  final String slug = text
      .toLowerCase()
      .replaceAll(RegExp(r'[^\p{L}\p{N}\s-]', unicode: true), '')
      .trim()
      .replaceAll(RegExp(r'[\s-]+'), '-')
      .replaceAll(RegExp(r'^-+|-+$'), '');
  return slug.isEmpty ? 'section' : slug;
}