1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Static method to count headings in markdown content.
use super::super::Toc;
impl<'a> Toc<'a> {
/// Count the number of headings in markdown content.
///
/// This is a static method useful for calculating dynamic TOC height
/// without constructing a full Toc widget.
///
/// # Arguments
///
/// * `content` - The markdown content to scan.
///
/// # Returns
///
/// The number of headings found.
pub fn count_headings(content: &str) -> usize {
let mut count = 0;
let mut in_code_block = false;
for line in content.lines() {
let trimmed = line.trim();
// Track code blocks
if trimmed.starts_with("```") {
in_code_block = !in_code_block;
continue;
}
if in_code_block {
continue;
}
// Check for headings
if trimmed.starts_with('#') {
let level = trimmed.chars().take_while(|c| *c == '#').count();
if level <= 6 {
let text = trimmed[level..].trim();
if !text.is_empty() {
count += 1;
}
}
}
}
count
}
}