1pub mod anchor_styles;
6pub mod blockquote;
7pub mod code_block_utils;
8pub mod early_returns;
11pub mod element_cache;
12pub mod emphasis_utils;
13pub mod fix_utils;
14pub mod header_id_utils;
15pub mod jinja_utils;
16pub mod kramdown_utils;
17pub mod line_ending;
18pub mod markdown_elements;
19pub mod mkdocs_abbreviations;
20pub mod mkdocs_admonitions;
21pub mod mkdocs_attr_list;
22pub mod mkdocs_common;
23pub mod mkdocs_config;
24pub mod mkdocs_critic;
25pub mod mkdocs_definition_lists;
26pub mod mkdocs_extensions;
27pub mod mkdocs_footnotes;
28pub mod mkdocs_html_markdown;
29pub mod mkdocs_icons;
30pub mod mkdocs_patterns;
31pub mod mkdocs_snippets;
32pub mod mkdocs_tabs;
33pub mod mkdocs_test_utils;
34pub mod mkdocstrings_refs;
35pub mod pymdown_blocks;
36pub mod quarto_divs;
37pub mod range_utils;
38pub mod regex_cache;
39pub mod sentence_utils;
40pub mod skip_context;
41pub mod string_interner;
42pub mod table_utils;
43pub mod text_reflow;
44pub mod utf8_offsets;
45
46pub use code_block_utils::CodeBlockUtils;
47pub use line_ending::{
49 LineEnding, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings, get_line_ending_str,
50 normalize_line_ending,
51};
52pub use markdown_elements::{ElementQuality, ElementType, MarkdownElement, MarkdownElements};
53pub use range_utils::LineIndex;
54
55pub fn is_definition_list_item(line: &str) -> bool {
65 let trimmed = line.trim_start();
66 trimmed.starts_with(": ")
67 || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(|c| c.is_whitespace()))
68}
69
70pub trait StrExt {
72 fn replace_trailing_spaces(&self, replacement: &str) -> String;
74
75 fn has_trailing_spaces(&self) -> bool;
77
78 fn trailing_spaces(&self) -> usize;
80}
81
82impl StrExt for str {
83 fn replace_trailing_spaces(&self, replacement: &str) -> String {
84 let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
88 (stripped, true)
89 } else {
90 (self, false)
91 };
92
93 let mut non_space_len = content.len();
95 for c in content.chars().rev() {
96 if c == ' ' {
97 non_space_len -= 1;
98 } else {
99 break;
100 }
101 }
102
103 let mut result =
105 String::with_capacity(non_space_len + replacement.len() + if ends_with_newline { 1 } else { 0 });
106 result.push_str(&content[..non_space_len]);
107 result.push_str(replacement);
108 if ends_with_newline {
109 result.push('\n');
110 }
111
112 result
113 }
114
115 fn has_trailing_spaces(&self) -> bool {
116 self.trailing_spaces() > 0
117 }
118
119 fn trailing_spaces(&self) -> usize {
120 let content = self.strip_suffix('\n').unwrap_or(self);
124
125 let mut space_count = 0;
127 for c in content.chars().rev() {
128 if c == ' ' {
129 space_count += 1;
130 } else {
131 break;
132 }
133 }
134
135 space_count
136 }
137}
138
139use std::collections::hash_map::DefaultHasher;
140use std::hash::{Hash, Hasher};
141
142pub fn fast_hash(content: &str) -> u64 {
155 let mut hasher = DefaultHasher::new();
156 content.hash(&mut hasher);
157 hasher.finish()
158}
159
160#[cfg(test)]
161mod tests {
162 use super::*;
163
164 #[test]
165 fn test_detect_line_ending_pure_lf() {
166 let content = "First line\nSecond line\nThird line\n";
168 assert_eq!(detect_line_ending(content), "\n");
169 }
170
171 #[test]
172 fn test_detect_line_ending_pure_crlf() {
173 let content = "First line\r\nSecond line\r\nThird line\r\n";
175 assert_eq!(detect_line_ending(content), "\r\n");
176 }
177
178 #[test]
179 fn test_detect_line_ending_mixed_more_lf() {
180 let content = "First line\nSecond line\r\nThird line\nFourth line\n";
182 assert_eq!(detect_line_ending(content), "\n");
183 }
184
185 #[test]
186 fn test_detect_line_ending_mixed_more_crlf() {
187 let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
189 assert_eq!(detect_line_ending(content), "\r\n");
190 }
191
192 #[test]
193 fn test_detect_line_ending_empty_string() {
194 let content = "";
196 assert_eq!(detect_line_ending(content), "\n");
197 }
198
199 #[test]
200 fn test_detect_line_ending_single_line_no_ending() {
201 let content = "This is a single line with no line ending";
203 assert_eq!(detect_line_ending(content), "\n");
204 }
205
206 #[test]
207 fn test_detect_line_ending_equal_lf_and_crlf() {
208 let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
212 assert_eq!(detect_line_ending(content), "\n");
213 }
214
215 #[test]
216 fn test_detect_line_ending_single_lf() {
217 let content = "Line 1\n";
219 assert_eq!(detect_line_ending(content), "\n");
220 }
221
222 #[test]
223 fn test_detect_line_ending_single_crlf() {
224 let content = "Line 1\r\n";
226 assert_eq!(detect_line_ending(content), "\r\n");
227 }
228
229 #[test]
230 fn test_detect_line_ending_embedded_cr() {
231 let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
234 assert_eq!(detect_line_ending(content), "\n");
236 }
237}