1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// This file is part of helpers4.
// Copyright (C) 2025 baxyz
// SPDX-License-Identifier: LGPL-3.0-or-later
/// The anchor GitHub gives to a heading: `"Hello, World!"` becomes `"hello-world"`.
///
/// The text is trimmed and lowercased, letters (any language), digits, `-` and `_` are kept, every
/// whitespace character becomes a `-` (they are not merged, so `"a b"` is `"a--b"`), and
/// everything else is dropped. Unlike [`slugify`](https://docs.rs/helpers4/latest/helpers4/string/fn.slugify.html)
/// it follows GitHub's rules rather than making a tidy slug, so use it to build the `#fragment` of a
/// link to a heading. Two headings with the same text get a numeric suffix on GitHub; that part
/// needs the whole document and is left to you.
///
/// # Arguments
///
/// - `heading` - The text of the heading, without the leading `#`s.
///
/// # Returns
///
/// The anchor, without the leading `#`.
///
/// # Examples
///
/// ```
/// use helpers4::markdown::heading_slug;
///
/// assert_eq!(heading_slug("Hello, World!"), "hello-world");
/// assert_eq!(heading_slug(" Getting started (v2) "), "getting-started-v2");
/// assert_eq!(heading_slug("snake_case & kebab-case"), "snake_case--kebab-case");
/// ```