1pub mod anchor_styles;
6pub mod blockquote;
7pub mod code_block_utils;
8pub mod early_returns;
9pub mod element_cache;
10pub mod emphasis_utils;
11pub mod fix_utils;
12pub mod header_id_utils;
13pub mod jinja_utils;
14pub mod kramdown_utils;
15pub mod line_ending;
16pub mod markdown_elements;
17pub mod mkdocs_abbreviations;
18pub mod mkdocs_admonitions;
19pub mod mkdocs_attr_list;
20pub mod mkdocs_common;
21pub mod mkdocs_config;
22pub mod mkdocs_critic;
23pub mod mkdocs_definition_lists;
24pub mod mkdocs_extensions;
25pub mod mkdocs_footnotes;
26pub mod mkdocs_html_markdown;
27pub mod mkdocs_icons;
28pub mod mkdocs_patterns;
29pub mod mkdocs_snippets;
30pub mod mkdocs_tabs;
31pub mod mkdocs_test_utils;
32pub mod mkdocstrings_refs;
33pub mod pymdown_blocks;
34pub mod quarto_divs;
35pub mod range_utils;
36pub mod regex_cache;
37pub mod sentence_utils;
38pub mod skip_context;
39pub mod string_interner;
40pub mod table_utils;
41pub mod text_reflow;
42pub mod utf8_offsets;
43
44pub use code_block_utils::CodeBlockUtils;
45pub use line_ending::{
46 LineEnding, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings, get_line_ending_str,
47 normalize_line_ending,
48};
49pub use markdown_elements::{ElementQuality, ElementType, MarkdownElement, MarkdownElements};
50pub use range_utils::LineIndex;
51
52pub fn is_definition_list_item(line: &str) -> bool {
62 let trimmed = line.trim_start();
63 trimmed.starts_with(": ")
64 || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(|c| c.is_whitespace()))
65}
66
67pub fn is_template_directive_only(line: &str) -> bool {
77 let trimmed = line.trim();
78 if trimmed.is_empty() {
79 return false;
80 }
81 (trimmed.starts_with("{{") && trimmed.ends_with("}}")) || (trimmed.starts_with("{%") && trimmed.ends_with("%}"))
82}
83
84pub trait StrExt {
86 fn replace_trailing_spaces(&self, replacement: &str) -> String;
88
89 fn has_trailing_spaces(&self) -> bool;
91
92 fn trailing_spaces(&self) -> usize;
94}
95
96impl StrExt for str {
97 fn replace_trailing_spaces(&self, replacement: &str) -> String {
98 let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
102 (stripped, true)
103 } else {
104 (self, false)
105 };
106
107 let mut non_space_len = content.len();
109 for c in content.chars().rev() {
110 if c == ' ' {
111 non_space_len -= 1;
112 } else {
113 break;
114 }
115 }
116
117 let mut result =
119 String::with_capacity(non_space_len + replacement.len() + if ends_with_newline { 1 } else { 0 });
120 result.push_str(&content[..non_space_len]);
121 result.push_str(replacement);
122 if ends_with_newline {
123 result.push('\n');
124 }
125
126 result
127 }
128
129 fn has_trailing_spaces(&self) -> bool {
130 self.trailing_spaces() > 0
131 }
132
133 fn trailing_spaces(&self) -> usize {
134 let content = self.strip_suffix('\n').unwrap_or(self);
138
139 let mut space_count = 0;
141 for c in content.chars().rev() {
142 if c == ' ' {
143 space_count += 1;
144 } else {
145 break;
146 }
147 }
148
149 space_count
150 }
151}
152
153use std::collections::hash_map::DefaultHasher;
154use std::hash::{Hash, Hasher};
155
156pub fn fast_hash(content: &str) -> u64 {
169 let mut hasher = DefaultHasher::new();
170 content.hash(&mut hasher);
171 hasher.finish()
172}
173
174#[cfg(test)]
175mod tests {
176 use super::*;
177
178 #[test]
179 fn test_detect_line_ending_pure_lf() {
180 let content = "First line\nSecond line\nThird line\n";
182 assert_eq!(detect_line_ending(content), "\n");
183 }
184
185 #[test]
186 fn test_detect_line_ending_pure_crlf() {
187 let content = "First line\r\nSecond line\r\nThird line\r\n";
189 assert_eq!(detect_line_ending(content), "\r\n");
190 }
191
192 #[test]
193 fn test_detect_line_ending_mixed_more_lf() {
194 let content = "First line\nSecond line\r\nThird line\nFourth line\n";
196 assert_eq!(detect_line_ending(content), "\n");
197 }
198
199 #[test]
200 fn test_detect_line_ending_mixed_more_crlf() {
201 let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
203 assert_eq!(detect_line_ending(content), "\r\n");
204 }
205
206 #[test]
207 fn test_detect_line_ending_empty_string() {
208 let content = "";
210 assert_eq!(detect_line_ending(content), "\n");
211 }
212
213 #[test]
214 fn test_detect_line_ending_single_line_no_ending() {
215 let content = "This is a single line with no line ending";
217 assert_eq!(detect_line_ending(content), "\n");
218 }
219
220 #[test]
221 fn test_detect_line_ending_equal_lf_and_crlf() {
222 let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
226 assert_eq!(detect_line_ending(content), "\n");
227 }
228
229 #[test]
230 fn test_detect_line_ending_single_lf() {
231 let content = "Line 1\n";
233 assert_eq!(detect_line_ending(content), "\n");
234 }
235
236 #[test]
237 fn test_detect_line_ending_single_crlf() {
238 let content = "Line 1\r\n";
240 assert_eq!(detect_line_ending(content), "\r\n");
241 }
242
243 #[test]
244 fn test_detect_line_ending_embedded_cr() {
245 let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
248 assert_eq!(detect_line_ending(content), "\n");
250 }
251}