diff --git a/Cargo.lock b/Cargo.lock index a92ef3a18d..e1a75af134 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -730,6 +730,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -2050,6 +2059,7 @@ dependencies = [ "async-stream", "cached", "check-if-email-exists", + "convert_case", "doc-comment", "email_address", "futures", @@ -3990,6 +4000,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" + [[package]] name = "unicode-width" version = "0.1.10" diff --git a/lychee-lib/Cargo.toml b/lychee-lib/Cargo.toml index 526dde965e..bfa193a80c 100644 --- a/lychee-lib/Cargo.toml +++ b/lychee-lib/Cargo.toml @@ -20,6 +20,7 @@ version = "0.13.0" async-stream = "0.3.5" cached = "0.44.0" check-if-email-exists = { version = "0.9.0", optional = true } +convert_case = "0.6.0" email_address = "0.2.4" futures = "0.3.27" glob = "0.3.1" diff --git a/lychee-lib/src/extract/markdown.rs b/lychee-lib/src/extract/markdown.rs index 1153fd1850..a01eb67135 100644 --- a/lychee-lib/src/extract/markdown.rs +++ b/lychee-lib/src/extract/markdown.rs @@ -1,6 +1,7 @@ //! Extract things from markdown documents use std::collections::{HashMap, HashSet}; +use convert_case::{Case, Casing}; use pulldown_cmark::{Event, Parser, Tag}; use crate::{extract::plaintext::extract_plaintext, types::uri::raw::RawUri}; @@ -98,7 +99,7 @@ pub(crate) fn extract_markdown_fragments(input: &str) -> HashSet { out.insert(frag.to_string()); } - let mut id = into_kebab_case(&heading); + let mut id = heading.to_case(Case::Kebab); let k_id_count = kebab_id_counter.entry(id.clone()).or_insert(0); if *k_id_count != 0 { @@ -123,22 +124,6 @@ pub(crate) fn extract_markdown_fragments(input: &str) -> HashSet { out } -/// Converts text into kebab case -#[must_use] -pub fn into_kebab_case(text: &str) -> String { - text.chars() - .filter_map(|ch| { - if ch.is_alphanumeric() || ch == '_' || ch == '-' { - Some(ch.to_ascii_lowercase()) - } else if ch.is_whitespace() { - Some('-') - } else { - None - } - }) - .collect::() -} - #[cfg(test)] mod tests { use super::*;