use std::collections::HashSet;
use unicode_normalization::{UnicodeNormalization, char::is_combining_mark};
const FALLBACK: &str = "section";
pub(crate) fn slugify_base(text: &str) -> String {
let mut slug = String::with_capacity(text.len());
let mut pending_hyphen = false;
for ch in text.nfkd().flat_map(char::to_lowercase) {
if ch.is_ascii_alphanumeric() {
if pending_hyphen && !slug.is_empty() {
slug.push('-');
}
pending_hyphen = false;
slug.push(ch);
} else if is_combining_mark(ch) {
continue;
} else {
pending_hyphen = true;
}
}
if slug.is_empty() {
FALLBACK.to_string()
} else {
slug
}
}
pub(crate) fn slugify_unique(text: &str, existing: &mut HashSet<String>) -> String {
let base = slugify_base(text);
let slug = disambiguate(base, existing);
existing.insert(slug.clone());
slug
}
fn disambiguate(base: String, existing: &HashSet<String>) -> String {
if !existing.contains(&base) {
return base;
}
let mut n: usize = 1;
loop {
let candidate = format!("{base}-{n}");
if !existing.contains(&candidate) {
return candidate;
}
n += 1;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lowercases_and_hyphenates() {
assert_eq!(slugify_base("Hello World"), "hello-world");
}
#[test]
fn collapses_runs_of_separators() {
assert_eq!(slugify_base("Hello --- World!!!"), "hello-world");
}
#[test]
fn trims_leading_and_trailing_hyphens() {
assert_eq!(slugify_base(" Overview "), "overview");
assert_eq!(slugify_base("!!!edges!!!"), "edges");
}
#[test]
fn unicode_is_normalized_to_ascii() {
assert_eq!(slugify_base("Café"), "cafe");
assert_eq!(slugify_base("naïve résumé"), "naive-resume");
}
#[test]
fn punctuation_only_falls_back_to_section() {
assert_eq!(slugify_base("..."), "section");
assert_eq!(slugify_base("!!!"), "section");
assert_eq!(slugify_base(""), "section");
}
#[test]
fn digits_are_preserved() {
assert_eq!(slugify_base("Version 1.2.3"), "version-1-2-3");
}
#[test]
fn collision_appends_incrementing_suffix() {
let mut seen = HashSet::new();
assert_eq!(slugify_unique("Overview", &mut seen), "overview");
assert_eq!(slugify_unique("Overview", &mut seen), "overview-1");
assert_eq!(slugify_unique("Overview", &mut seen), "overview-2");
}
#[test]
fn suffixed_candidate_is_itself_checked() {
let mut seen = HashSet::new();
assert_eq!(slugify_unique("Overview 1", &mut seen), "overview-1");
assert_eq!(slugify_unique("Overview", &mut seen), "overview");
assert_eq!(slugify_unique("Overview", &mut seen), "overview-2");
}
#[test]
fn honours_preseeded_existing_set() {
let mut seen = HashSet::new();
seen.insert("overview".to_string());
assert_eq!(slugify_unique("Overview", &mut seen), "overview-1");
}
}