kanbrain_core/model/
text.rs1const MAX_SLUG_LEN: usize = 40;
5
6pub fn parse_tags(s: &str) -> Vec<String> {
8 let mut tags: Vec<String> = Vec::new();
9 for raw in s.split(|c: char| c == ',' || c.is_whitespace()) {
10 let tag = raw.trim_start_matches('#').to_lowercase();
11 if !tag.is_empty() && !tags.contains(&tag) {
12 tags.push(tag);
13 }
14 }
15 tags
16}
17
18pub fn counted(n: usize, one: &str, many: &str) -> String {
20 format!("{n} {}", if n == 1 { one } else { many })
21}
22
23pub fn one_line(title: &str) -> String {
26 title.split(['\n', '\r']).map(str::trim).filter(|line| !line.is_empty()).collect::<Vec<_>>().join(" ")
28}
29
30pub fn slugify(title: &str) -> String {
32 let mut slug = String::new();
33 for c in title.chars() {
34 if slug.len() >= MAX_SLUG_LEN {
35 break;
36 }
37 if c.is_ascii_alphanumeric() {
38 slug.push(c.to_ascii_lowercase());
39 } else if !slug.ends_with('-') {
40 slug.push('-');
41 }
42 }
43 let slug = slug.trim_matches('-');
44 if slug.is_empty() { "task".to_string() } else { slug.to_string() }
45}
46
47pub fn sanitize_description(text: &str) -> (String, bool) {
51 demote(text, &["## "], "### ")
52}
53
54pub fn sanitize_comment(text: &str) -> (String, bool) {
56 demote(text, &["## ", "### "], "#### ")
57}
58
59fn demote(text: &str, prefixes: &[&str], to: &str) -> (String, bool) {
61 let mut changed = false;
62 let lines: Vec<String> = text
63 .lines()
64 .map(|line| match prefixes.iter().find_map(|p| line.strip_prefix(p)) {
65 Some(rest) => {
66 changed = true;
67 format!("{to}{rest}")
68 }
69 None => line.to_string(),
70 })
71 .collect();
72 (lines.join("\n").trim().to_string(), changed)
73}
74
75#[cfg(test)]
76#[path = "../../tests/model/text.rs"]
77mod tests;