obsidian-cli-inspector 1.0.3

Local-first CLI/TUI for indexing and querying Obsidian vaults
Documentation
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/// Chunking module for splitting markdown notes into retrieval-ready text units
///
/// This module implements heading-based chunking with paragraph fallback,
/// preserving document structure and maintaining stable heading paths.
mod chunk;
mod heading;
mod overlap;
mod paragraph;

pub use chunk::Chunk;

#[derive(Debug, Clone)]
struct HeadingInfo {
    level: usize,
    text: String,
    byte_offset: usize,
}

pub struct MarkdownChunker {
    max_chunk_size: usize, // in characters (approximate)
    overlap: usize,        // overlap between chunks in characters
}

impl Default for MarkdownChunker {
    fn default() -> Self {
        Self::new(1000, 100)
    }
}

impl MarkdownChunker {
    pub fn new(max_chunk_size: usize, overlap: usize) -> Self {
        MarkdownChunker {
            max_chunk_size,
            overlap,
        }
    }

    /// Split a markdown document into chunks
    pub fn chunk(&self, content: &str) -> Vec<Chunk> {
        // First, try to split by headings
        let heading_sections = self.split_by_headings(content);

        if heading_sections.is_empty() {
            // No headings found, fall back to paragraph-based chunking
            return paragraph::chunk_by_paragraphs(
                content,
                None,
                0,
                self.max_chunk_size,
                self.overlap,
            );
        }

        let mut chunks = Vec::new();

        for section in heading_sections {
            // If section is small enough, create a single chunk
            if section.text.len() <= self.max_chunk_size {
                chunks.push(Chunk {
                    heading_path: section.heading_path.clone(),
                    text: section.text.clone(),
                    byte_offset: section.byte_offset,
                    byte_length: section.text.len(),
                    token_count: self.estimate_tokens(&section.text),
                });
            } else {
                // Section is too large, split by paragraphs
                let sub_chunks = paragraph::chunk_by_paragraphs(
                    &section.text,
                    section.heading_path.as_deref(),
                    section.byte_offset,
                    self.max_chunk_size,
                    self.overlap,
                );
                chunks.extend(sub_chunks);
            }
        }

        chunks
    }

    /// Split content by markdown headings
    fn split_by_headings(&self, content: &str) -> Vec<Section> {
        let mut sections = Vec::new();
        let mut heading_stack: Vec<HeadingInfo> = Vec::new();
        let mut current_text = String::new();
        let mut section_start_offset = 0;
        let mut current_offset = 0;

        for line in content.lines() {
            let line_with_newline = format!("{line}\n");

            if let Some(heading) = heading::parse_heading(line) {
                // Save previous section if it has content
                if !current_text.trim().is_empty() {
                    let heading_path = self.build_heading_path(&heading_stack);
                    sections.push(Section {
                        heading_path,
                        text: current_text.clone(),
                        byte_offset: section_start_offset,
                    });
                }

                // Update heading stack
                self.update_heading_stack(&mut heading_stack, heading, current_offset);

                // Start new section
                current_text = line_with_newline.clone();
                section_start_offset = current_offset;
            } else {
                current_text.push_str(&line_with_newline);
            }

            current_offset += line_with_newline.len();
        }

        // Don't forget the last section
        if !current_text.trim().is_empty() {
            let heading_path = self.build_heading_path(&heading_stack);
            sections.push(Section {
                heading_path,
                text: current_text,
                byte_offset: section_start_offset,
            });
        }

        sections
    }

    /// Update the heading stack based on the new heading level
    fn update_heading_stack(
        &self,
        stack: &mut Vec<HeadingInfo>,
        mut new_heading: HeadingInfo,
        offset: usize,
    ) {
        new_heading.byte_offset = offset;

        // Pop headings at same or lower level
        while let Some(top) = stack.last() {
            if top.level >= new_heading.level {
                stack.pop();
            } else {
                break;
            }
        }

        stack.push(new_heading);
    }

    /// Build a heading path string from the stack (e.g., "# Main > ## Sub > ### Detail")
    fn build_heading_path(&self, stack: &[HeadingInfo]) -> Option<String> {
        if stack.is_empty() {
            return None;
        }

        let parts: Vec<String> = stack
            .iter()
            .map(|h| format!("{} {}", "#".repeat(h.level), h.text))
            .collect();

        Some(parts.join(" > "))
    }

    /// Estimate token count (rough approximation: 1 token ≈ 4 characters)
    fn estimate_tokens(&self, text: &str) -> usize {
        // Simple heuristic: avg 4 chars per token
        // Also count whitespace-separated words for better accuracy
        let char_estimate = text.len() / 4;
        let word_count = text.split_whitespace().count();

        // Use average of both estimates
        (char_estimate + word_count) / 2
    }
}

#[derive(Debug, Clone)]
struct Section {
    heading_path: Option<String>,
    text: String,
    byte_offset: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_heading() {
        let h1 = heading::parse_heading("# Main Title");
        assert!(h1.is_some());
        let h1 = h1.unwrap();
        assert_eq!(h1.level, 1);
        assert_eq!(h1.text, "Main Title");

        let h2 = heading::parse_heading("## Subtitle");
        assert!(h2.is_some());
        assert_eq!(h2.unwrap().level, 2);

        let not_heading = heading::parse_heading("#NoSpace");
        assert!(not_heading.is_none());

        let not_heading2 = heading::parse_heading("Regular text");
        assert!(not_heading2.is_none());
    }

    #[test]
    fn test_chunk_simple_document() {
        let chunker = MarkdownChunker::new(500, 50);
        let content = r#"# Introduction

This is the introduction paragraph.

## Background

Some background information here.

### Details

More detailed information.
"#;

        let chunks = chunker.chunk(content);
        assert!(!chunks.is_empty());

        // Should have chunks with proper heading paths
        assert!(chunks.iter().any(|c| c.heading_path.is_some()));
    }

    #[test]
    fn test_chunk_no_headings() {
        let chunker = MarkdownChunker::new(100, 20);
        let content =
            "This is a simple paragraph without any headings. It should still be chunked properly.";

        let chunks = chunker.chunk(content);
        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0].heading_path, None);
    }

    #[test]
    fn test_estimate_tokens() {
        let chunker = MarkdownChunker::default();

        let text = "This is a test sentence.";
        let tokens = chunker.estimate_tokens(text);

        // Should be roughly 5-6 tokens (within reasonable range)
        assert!((4..=8).contains(&tokens));
    }

    #[test]
    fn test_split_into_paragraphs() {
        let content = r#"First paragraph.

Second paragraph.

Third paragraph."#;

        let paragraphs = paragraph::split_into_paragraphs(content);
        assert_eq!(paragraphs.len(), 3);
    }

    #[test]
    fn test_heading_path_generation() {
        let chunker = MarkdownChunker::default();
        let content = r#"# Main
Some text.
## Sub1
More text.
### Detail
Details here.
"#;

        let chunks = chunker.chunk(content);

        // Should have a chunk with nested heading path
        let nested_chunk = chunks
            .iter()
            .find(|c| c.heading_path.as_ref().is_some_and(|p| p.contains(" > ")));
        assert!(nested_chunk.is_some());
    }

    #[test]
    fn test_chunk_empty_content() {
        let chunker = MarkdownChunker::default();
        let chunks = chunker.chunk("");
        assert!(chunks.is_empty());
    }

    #[test]
    fn test_chunk_whitespace_only() {
        let chunker = MarkdownChunker::default();
        let chunks = chunker.chunk("   \n\n   \n   ");
        assert!(chunks.is_empty());
    }

    #[test]
    fn test_chunk_single_heading() {
        let chunker = MarkdownChunker::default();
        let content = "# Title\n\nContent here.";
        let chunks = chunker.chunk(content);
        assert!(!chunks.is_empty());
        assert!(chunks[0].heading_path.is_some());
    }

    #[test]
    fn test_chunk_large_section_paragraph_split() {
        let chunker = MarkdownChunker::new(100, 20);
        // Create content that will exceed max_chunk_size and need paragraph splitting
        let content = r#"# Main

This is a very long paragraph that should be split into multiple chunks when the chunker processes it. It contains many words and should exceed the maximum chunk size of 100 characters to trigger the paragraph-based splitting logic.

Another paragraph here.
"#;
        let chunks = chunker.chunk(content);
        assert!(chunks.len() > 1);
    }

    #[test]
    fn test_build_heading_path_empty() {
        let chunker = MarkdownChunker::default();
        let path = chunker.build_heading_path(&[]);
        assert!(path.is_none());
    }

    #[test]
    fn test_build_heading_path_single() {
        let chunker = MarkdownChunker::default();
        let info = HeadingInfo {
            level: 1,
            text: "Title".to_string(),
            byte_offset: 0,
        };
        let path = chunker.build_heading_path(&[info]);
        assert!(path.is_some());
        assert!(path.unwrap().contains("# Title"));
    }

    #[test]
    fn test_update_heading_stack_new_level() {
        let chunker = MarkdownChunker::default();
        let mut stack: Vec<HeadingInfo> = Vec::new();

        let h1 = HeadingInfo {
            level: 1,
            text: "Main".to_string(),
            byte_offset: 0,
        };
        chunker.update_heading_stack(&mut stack, h1, 0);
        assert_eq!(stack.len(), 1);

        let h2 = HeadingInfo {
            level: 2,
            text: "Sub".to_string(),
            byte_offset: 10,
        };
        chunker.update_heading_stack(&mut stack, h2, 10);
        assert_eq!(stack.len(), 2);
    }

    #[test]
    fn test_update_heading_stack_same_level() {
        let chunker = MarkdownChunker::default();
        let mut stack: Vec<HeadingInfo> = Vec::new();

        let h1 = HeadingInfo {
            level: 1,
            text: "Main".to_string(),
            byte_offset: 0,
        };
        chunker.update_heading_stack(&mut stack, h1, 0);

        let h2 = HeadingInfo {
            level: 1,
            text: "Another".to_string(),
            byte_offset: 10,
        };
        chunker.update_heading_stack(&mut stack, h2, 10);
        assert_eq!(stack.len(), 1);
        assert_eq!(stack[0].text, "Another");
    }

    #[test]
    fn test_update_heading_stack_skip_level() {
        let chunker = MarkdownChunker::default();
        let mut stack: Vec<HeadingInfo> = Vec::new();

        let h1 = HeadingInfo {
            level: 1,
            text: "Main".to_string(),
            byte_offset: 0,
        };
        chunker.update_heading_stack(&mut stack, h1, 0);

        let h3 = HeadingInfo {
            level: 3,
            text: "Detail".to_string(),
            byte_offset: 10,
        };
        chunker.update_heading_stack(&mut stack, h3, 10);
        // Should have h1 and h3 (h2 was skipped)
        assert_eq!(stack.len(), 2);
    }

    #[test]
    fn test_estimate_tokens_empty() {
        let chunker = MarkdownChunker::default();
        let tokens = chunker.estimate_tokens("");
        assert_eq!(tokens, 0);
    }

    #[test]
    fn test_estimate_tokens_long_text() {
        let chunker = MarkdownChunker::default();
        let text = "This is a longer piece of text that should have more tokens. It has many words and characters.";
        let tokens = chunker.estimate_tokens(text);
        assert!(tokens > 10);
    }

    #[test]
    fn test_split_by_headings_no_headings() {
        let chunker = MarkdownChunker::default();
        let content = "Just some plain text without any headings.";
        let sections = chunker.split_by_headings(content);
        assert_eq!(sections.len(), 1);
        assert!(sections[0].heading_path.is_none());
    }

    #[test]
    fn test_split_by_headings_multiple() {
        let chunker = MarkdownChunker::default();
        let content = "# H1\n\nContent 1\n\n## H2\n\nContent 2\n";
        let sections = chunker.split_by_headings(content);
        assert!(!sections.is_empty());
    }
}