fabryk-content 0.3.0

Markdown parsing, frontmatter extraction, and content utilities for Fabryk
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
//! Markdown structure parsing utilities.
//!
//! This module provides utilities for extracting structural elements from
//! markdown content using `pulldown-cmark`:
//!
//! - Extract first heading (any level)
//! - Extract first paragraph
//! - Strip formatting to get plain text
//!
//! # Example
//!
//! ```rust
//! use fabryk_content::markdown::parser::{extract_first_heading, extract_first_paragraph};
//! use pulldown_cmark::HeadingLevel;
//!
//! let content = "# My Title\n\nThis is the first paragraph.\n\n## Section";
//!
//! let (level, title) = extract_first_heading(content).unwrap();
//! assert_eq!(level, HeadingLevel::H1);
//! assert_eq!(title, "My Title");
//!
//! let paragraph = extract_first_paragraph(content, 100).unwrap();
//! assert_eq!(paragraph, "This is the first paragraph.");
//! ```

use pulldown_cmark::{Event, HeadingLevel, Parser, Tag, TagEnd};

/// Extract the first heading from markdown content.
///
/// Returns the heading level and text content. Inline formatting (bold, italic,
/// links) is stripped from the heading text.
///
/// # Arguments
///
/// * `content` - Markdown content to parse
///
/// # Returns
///
/// * `Some((HeadingLevel, String))` - The heading level and text
/// * `None` - If no heading is found
///
/// # Example
///
/// ```rust
/// use fabryk_content::markdown::parser::extract_first_heading;
/// use pulldown_cmark::HeadingLevel;
///
/// let content = "Some text\n\n## Introduction\n\nMore text";
/// let (level, text) = extract_first_heading(content).unwrap();
/// assert_eq!(level, HeadingLevel::H2);
/// assert_eq!(text, "Introduction");
/// ```
pub fn extract_first_heading(content: &str) -> Option<(HeadingLevel, String)> {
    let parser = Parser::new(content);
    let mut in_heading = false;
    let mut heading_level = HeadingLevel::H1;
    let mut heading_text = String::new();

    for event in parser {
        match event {
            Event::Start(Tag::Heading { level, .. }) => {
                in_heading = true;
                heading_level = level;
                heading_text.clear();
            }
            Event::End(TagEnd::Heading(_)) => {
                if in_heading && !heading_text.is_empty() {
                    return Some((heading_level, heading_text.trim().to_string()));
                }
                in_heading = false;
            }
            Event::Text(text) | Event::Code(text) if in_heading => {
                heading_text.push_str(&text);
            }
            Event::SoftBreak | Event::HardBreak if in_heading => {
                heading_text.push(' ');
            }
            _ => {}
        }
    }

    None
}

/// Extract the first paragraph from markdown content.
///
/// Skips headings and extracts the first actual paragraph content.
/// Inline formatting is stripped. Content is truncated to `max_chars` if needed.
///
/// # Arguments
///
/// * `content` - Markdown content to parse
/// * `max_chars` - Maximum characters to return (truncates with "..." if exceeded)
///
/// # Returns
///
/// * `Some(String)` - The paragraph text
/// * `None` - If no paragraph is found
///
/// # Example
///
/// ```rust
/// use fabryk_content::markdown::parser::extract_first_paragraph;
///
/// let content = "# Title\n\nThis is a **bold** introduction.\n\nMore content.";
/// let paragraph = extract_first_paragraph(content, 50).unwrap();
/// assert_eq!(paragraph, "This is a bold introduction.");
/// ```
pub fn extract_first_paragraph(content: &str, max_chars: usize) -> Option<String> {
    let parser = Parser::new(content);
    let mut in_paragraph = false;
    let mut paragraph_text = String::new();
    let mut skip_until_heading_end = false;

    for event in parser {
        match event {
            // Skip content inside headings
            Event::Start(Tag::Heading { .. }) => {
                skip_until_heading_end = true;
            }
            Event::End(TagEnd::Heading(_)) => {
                skip_until_heading_end = false;
            }

            // Track paragraph boundaries
            Event::Start(Tag::Paragraph) if !skip_until_heading_end => {
                in_paragraph = true;
                paragraph_text.clear();
            }
            Event::End(TagEnd::Paragraph) if in_paragraph => {
                let trimmed = paragraph_text.trim();
                if !trimmed.is_empty() {
                    return Some(truncate_text(trimmed, max_chars));
                }
                in_paragraph = false;
            }

            // Collect text content
            Event::Text(text) | Event::Code(text) if in_paragraph => {
                paragraph_text.push_str(&text);
            }
            Event::SoftBreak | Event::HardBreak if in_paragraph => {
                paragraph_text.push(' ');
            }

            _ => {}
        }
    }

    None
}

/// Extract plain text content from markdown, stripping all formatting.
///
/// Removes headings, bold, italic, links, code blocks, etc. Returns just
/// the text content, suitable for indexing or summarization.
///
/// # Arguments
///
/// * `content` - Markdown content to parse
///
/// # Returns
///
/// Plain text content with formatting removed.
///
/// # Example
///
/// ```rust
/// use fabryk_content::markdown::parser::extract_text_content;
///
/// let content = "# Title\n\nSome **bold** and *italic* text.\n\n```rust\ncode\n```";
/// let text = extract_text_content(content);
/// assert!(text.contains("Title"));
/// assert!(text.contains("Some bold and italic text"));
/// assert!(!text.contains("**"));
/// assert!(!text.contains("*"));
/// ```
pub fn extract_text_content(content: &str) -> String {
    let parser = Parser::new(content);
    let mut text_content = String::new();
    let mut in_code_block = false;

    for event in parser {
        match event {
            Event::Start(Tag::CodeBlock(_)) => {
                in_code_block = true;
            }
            Event::End(TagEnd::CodeBlock) => {
                in_code_block = false;
            }
            Event::Text(text) if !in_code_block => {
                if !text_content.is_empty() && !text_content.ends_with(' ') {
                    text_content.push(' ');
                }
                text_content.push_str(&text);
            }
            Event::Code(text) if !in_code_block => {
                if !text_content.is_empty() && !text_content.ends_with(' ') {
                    text_content.push(' ');
                }
                text_content.push_str(&text);
            }
            Event::SoftBreak | Event::HardBreak => {
                if !text_content.is_empty() && !text_content.ends_with(' ') {
                    text_content.push(' ');
                }
            }
            Event::End(TagEnd::Paragraph) | Event::End(TagEnd::Heading(_)) => {
                if !text_content.is_empty() && !text_content.ends_with('\n') {
                    text_content.push('\n');
                }
            }
            _ => {}
        }
    }

    // Clean up multiple spaces and normalize whitespace
    normalize_whitespace(&text_content)
}

/// Truncate text to a maximum length, adding "..." if truncated.
fn truncate_text(text: &str, max_chars: usize) -> String {
    if text.len() <= max_chars {
        text.to_string()
    } else {
        // Find a word boundary near max_chars
        let truncate_at = text[..max_chars]
            .rfind(|c: char| c.is_whitespace())
            .unwrap_or(max_chars);

        format!("{}...", text[..truncate_at].trim())
    }
}

/// Normalize whitespace: collapse multiple spaces, trim lines.
fn normalize_whitespace(text: &str) -> String {
    text.split_whitespace().collect::<Vec<_>>().join(" ")
}

// ============================================================================
// Tests
// ============================================================================

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

    // ------------------------------------------------------------------------
    // extract_first_heading tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_extract_h1_heading() {
        let content = "# Main Title\n\nSome content";
        let (level, text) = extract_first_heading(content).unwrap();
        assert_eq!(level, HeadingLevel::H1);
        assert_eq!(text, "Main Title");
    }

    #[test]
    fn test_extract_h2_heading() {
        let content = "## Section Title\n\nContent here";
        let (level, text) = extract_first_heading(content).unwrap();
        assert_eq!(level, HeadingLevel::H2);
        assert_eq!(text, "Section Title");
    }

    #[test]
    fn test_extract_heading_with_formatting() {
        let content = "# Title with **bold** and *italic*\n\nBody";
        let (level, text) = extract_first_heading(content).unwrap();
        assert_eq!(level, HeadingLevel::H1);
        assert_eq!(text, "Title with bold and italic");
    }

    #[test]
    fn test_extract_heading_with_code() {
        let content = "# Using `Result` in Rust\n\nExplanation";
        let (_, text) = extract_first_heading(content).unwrap();
        assert_eq!(text, "Using Result in Rust");
    }

    #[test]
    fn test_extract_heading_skips_initial_text() {
        let content = "Some introductory text\n\n## First Real Heading\n\nBody";
        let (level, text) = extract_first_heading(content).unwrap();
        assert_eq!(level, HeadingLevel::H2);
        assert_eq!(text, "First Real Heading");
    }

    #[test]
    fn test_extract_heading_no_heading() {
        let content = "Just some paragraph text without any heading.";
        assert!(extract_first_heading(content).is_none());
    }

    // ------------------------------------------------------------------------
    // extract_first_paragraph tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_extract_paragraph_after_heading() {
        let content = "# Title\n\nThis is the first paragraph.\n\nSecond paragraph.";
        let paragraph = extract_first_paragraph(content, 100).unwrap();
        assert_eq!(paragraph, "This is the first paragraph.");
    }

    #[test]
    fn test_extract_paragraph_no_heading() {
        let content = "First paragraph without heading.\n\nSecond paragraph.";
        let paragraph = extract_first_paragraph(content, 100).unwrap();
        assert_eq!(paragraph, "First paragraph without heading.");
    }

    #[test]
    fn test_extract_paragraph_with_formatting() {
        let content = "# Title\n\nThis has **bold** and *italic* formatting.\n\nMore.";
        let paragraph = extract_first_paragraph(content, 100).unwrap();
        assert_eq!(paragraph, "This has bold and italic formatting.");
    }

    #[test]
    fn test_extract_paragraph_truncation() {
        let content = "# Title\n\nThis is a longer paragraph that should be truncated.\n\nMore.";
        let paragraph = extract_first_paragraph(content, 20).unwrap();
        assert!(paragraph.len() <= 23); // 20 + "..."
        assert!(paragraph.ends_with("..."));
    }

    #[test]
    fn test_extract_paragraph_no_paragraph() {
        let content = "# Just a Heading";
        assert!(extract_first_paragraph(content, 100).is_none());
    }

    #[test]
    fn test_extract_paragraph_empty_paragraphs_skipped() {
        let content = "# Title\n\n\n\nActual content.\n\nMore.";
        let paragraph = extract_first_paragraph(content, 100).unwrap();
        assert_eq!(paragraph, "Actual content.");
    }

    // ------------------------------------------------------------------------
    // extract_text_content tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_extract_text_strips_formatting() {
        let content = "# Title\n\nSome **bold** and *italic* text.";
        let text = extract_text_content(content);
        assert!(text.contains("Title"));
        assert!(text.contains("Some bold and italic text"));
        assert!(!text.contains("**"));
        assert!(!text.contains("*"));
    }

    #[test]
    fn test_extract_text_excludes_code_blocks() {
        let content = "# Title\n\nParagraph.\n\n```rust\nfn main() {}\n```\n\nMore text.";
        let text = extract_text_content(content);
        assert!(text.contains("Paragraph"));
        assert!(text.contains("More text"));
        assert!(!text.contains("fn main"));
    }

    #[test]
    fn test_extract_text_preserves_inline_code() {
        let content = "Use the `Result` type for error handling.";
        let text = extract_text_content(content);
        assert!(text.contains("Result"));
    }

    #[test]
    fn test_extract_text_handles_links() {
        let content = "Check out [this link](https://example.com) for more info.";
        let text = extract_text_content(content);
        assert!(text.contains("this link"));
        assert!(!text.contains("https://"));
    }

    #[test]
    fn test_extract_text_normalizes_whitespace() {
        let content = "Multiple   spaces   and\n\n\nnewlines.";
        let text = extract_text_content(content);
        assert!(!text.contains("  ")); // No double spaces
    }

    // ------------------------------------------------------------------------
    // Edge cases
    // ------------------------------------------------------------------------

    #[test]
    fn test_empty_content() {
        let content = "";
        assert!(extract_first_heading(content).is_none());
        assert!(extract_first_paragraph(content, 100).is_none());
        assert_eq!(extract_text_content(content), "");
    }

    #[test]
    fn test_only_whitespace() {
        let content = "   \n\n   \n";
        assert!(extract_first_heading(content).is_none());
        assert!(extract_first_paragraph(content, 100).is_none());
    }

    #[test]
    fn test_unicode_content() {
        let content = "# 音楽理論\n\nこれは日本語のテキストです。";
        let (_, title) = extract_first_heading(content).unwrap();
        assert_eq!(title, "音楽理論");

        let para = extract_first_paragraph(content, 100).unwrap();
        assert_eq!(para, "これは日本語のテキストです。");
    }
}