Skip to content

heading_slug

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 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.

use helpers4::markdown::heading_slug;

Cargo feature markdown (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features markdown

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["markdown"] }
pub fn heading_slug(heading: &str) -> String
ParameterTypeDescription
heading&strThe text of the heading, without the leading #s.

String — The anchor, without the leading #.

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");

src/markdown/heading_slug.rs