1pub mod anchor_styles;
6pub mod code_block_utils;
7pub mod early_returns;
10pub mod element_cache;
11pub mod emphasis_utils;
12pub mod fix_utils;
13pub mod header_id_utils;
14pub mod jinja_utils;
15pub mod kramdown_utils;
16pub mod line_ending;
17pub mod markdown_elements;
18pub mod mkdocs_admonitions;
19pub mod mkdocs_common;
20pub mod mkdocs_critic;
21pub mod mkdocs_footnotes;
22pub mod mkdocs_patterns;
23pub mod mkdocs_snippets;
24pub mod mkdocs_tabs;
25pub mod mkdocs_test_utils;
26pub mod mkdocstrings_refs;
27pub mod range_utils;
28pub mod regex_cache;
29pub mod skip_context;
30pub mod string_interner;
31pub mod table_utils;
32pub mod text_reflow;
33pub mod utf8_offsets;
34
35pub use code_block_utils::CodeBlockUtils;
36pub use line_ending::{
38 LineEnding, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings, get_line_ending_str,
39 normalize_line_ending,
40};
41pub use markdown_elements::{ElementQuality, ElementType, MarkdownElement, MarkdownElements};
42pub use range_utils::LineIndex;
43
44pub fn is_definition_list_item(line: &str) -> bool {
54 let trimmed = line.trim_start();
55 trimmed.starts_with(": ")
56 || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(|c| c.is_whitespace()))
57}
58
59pub trait StrExt {
61 fn replace_trailing_spaces(&self, replacement: &str) -> String;
63
64 fn has_trailing_spaces(&self) -> bool;
66
67 fn trailing_spaces(&self) -> usize;
69}
70
71impl StrExt for str {
72 fn replace_trailing_spaces(&self, replacement: &str) -> String {
73 let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
77 (stripped, true)
78 } else {
79 (self, false)
80 };
81
82 let mut non_space_len = content.len();
84 for c in content.chars().rev() {
85 if c == ' ' {
86 non_space_len -= 1;
87 } else {
88 break;
89 }
90 }
91
92 let mut result =
94 String::with_capacity(non_space_len + replacement.len() + if ends_with_newline { 1 } else { 0 });
95 result.push_str(&content[..non_space_len]);
96 result.push_str(replacement);
97 if ends_with_newline {
98 result.push('\n');
99 }
100
101 result
102 }
103
104 fn has_trailing_spaces(&self) -> bool {
105 self.trailing_spaces() > 0
106 }
107
108 fn trailing_spaces(&self) -> usize {
109 let content = self.strip_suffix('\n').unwrap_or(self);
113
114 let mut space_count = 0;
116 for c in content.chars().rev() {
117 if c == ' ' {
118 space_count += 1;
119 } else {
120 break;
121 }
122 }
123
124 space_count
125 }
126}
127
128use std::collections::hash_map::DefaultHasher;
129use std::hash::{Hash, Hasher};
130
131pub fn fast_hash(content: &str) -> u64 {
144 let mut hasher = DefaultHasher::new();
145 content.hash(&mut hasher);
146 hasher.finish()
147}
148
149#[cfg(test)]
150mod tests {
151 use super::*;
152
153 #[test]
154 fn test_detect_line_ending_pure_lf() {
155 let content = "First line\nSecond line\nThird line\n";
157 assert_eq!(detect_line_ending(content), "\n");
158 }
159
160 #[test]
161 fn test_detect_line_ending_pure_crlf() {
162 let content = "First line\r\nSecond line\r\nThird line\r\n";
164 assert_eq!(detect_line_ending(content), "\r\n");
165 }
166
167 #[test]
168 fn test_detect_line_ending_mixed_more_lf() {
169 let content = "First line\nSecond line\r\nThird line\nFourth line\n";
171 assert_eq!(detect_line_ending(content), "\n");
172 }
173
174 #[test]
175 fn test_detect_line_ending_mixed_more_crlf() {
176 let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
178 assert_eq!(detect_line_ending(content), "\r\n");
179 }
180
181 #[test]
182 fn test_detect_line_ending_empty_string() {
183 let content = "";
185 assert_eq!(detect_line_ending(content), "\n");
186 }
187
188 #[test]
189 fn test_detect_line_ending_single_line_no_ending() {
190 let content = "This is a single line with no line ending";
192 assert_eq!(detect_line_ending(content), "\n");
193 }
194
195 #[test]
196 fn test_detect_line_ending_equal_lf_and_crlf() {
197 let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
201 assert_eq!(detect_line_ending(content), "\n");
202 }
203
204 #[test]
205 fn test_detect_line_ending_single_lf() {
206 let content = "Line 1\n";
208 assert_eq!(detect_line_ending(content), "\n");
209 }
210
211 #[test]
212 fn test_detect_line_ending_single_crlf() {
213 let content = "Line 1\r\n";
215 assert_eq!(detect_line_ending(content), "\r\n");
216 }
217
218 #[test]
219 fn test_detect_line_ending_embedded_cr() {
220 let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
223 assert_eq!(detect_line_ending(content), "\n");
225 }
226}