diff options
author | Josh McKinney <joshka@users.noreply.github.com> | 2023-11-15 02:27:19 -0800 |
---|---|---|
committer | Josh McKinney <joshka@users.noreply.github.com> | 2023-11-15 13:24:56 -0800 |
commit | d5bdde1f5cee6d3a8331eba82dcdc4cc8e5a02ea (patch) | |
tree | ba841e762c01157fbae214c43ff5118ba38c995c | |
parent | 52ca8fc8318dfab6c26d6f556e86ab102ab1f0f4 (diff) |
fix: use correct case for TL;DR and FAQ
Fixes: #153
-rw-r--r-- | src/resolve.rs | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/resolve.rs b/src/resolve.rs index ac77556..93bde90 100644 --- a/src/resolve.rs +++ b/src/resolve.rs @@ -38,7 +38,7 @@ impl AdmonitionMeta { // Load the directive (and title, if one still not given) let (directive, title) = match (Directive::from_str(&raw_directive), title) { - (Ok(directive), None) => (directive, ucfirst(&raw_directive)), + (Ok(directive), None) => (directive, format_directive_title(&raw_directive)), (Err(_), None) => (Directive::Note, "Note".to_owned()), (Ok(directive), Some(title)) => (directive, title), (Err(_), Some(title)) => (Directive::Note, title), @@ -53,10 +53,21 @@ impl AdmonitionMeta { } } +/// Format the title of an admonition directive +/// +/// We special case a few words to make them look nicer (e.g. "tldr" -> "TL;DR" and "faq" -> "FAQ"). +fn format_directive_title(input: &str) -> String { + match input { + "tldr" => "TL;DR".to_owned(), + "faq" => "FAQ".to_owned(), + _ => uppercase_first(input), + } +} + /// Make the first letter of `input` upppercase. /// /// source: https://stackoverflow.com/a/38406885 -fn ucfirst(input: &str) -> String { +fn uppercase_first(input: &str) -> String { let mut chars = input.chars(); match chars.next() { None => String::new(), @@ -70,6 +81,18 @@ mod test { use pretty_assertions::assert_eq; #[test] + fn test_format_directive_title() { + assert_eq!(format_directive_title(""), ""); + assert_eq!(format_directive_title("a"), "A"); + assert_eq!(format_directive_title("tldr"), "TL;DR"); + assert_eq!(format_directive_title("faq"), "FAQ"); + assert_eq!(format_directive_title("note"), "Note"); + assert_eq!(format_directive_title("abstract"), "Abstract"); + // Unicode + assert_eq!(format_directive_title("🦀"), "🦀"); + } + + #[test] fn test_admonition_info_from_raw() { assert_eq!( AdmonitionMeta::resolve( |