sherwood 0.8.0

A static site generator with built-in development server
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use crate::content::parsing::ast_utils::extract_text_from_nodes;
use crate::core::markdown_config;
use markdown::mdast::Node;
use markdown::to_mdast;

pub struct ExcerptExtractor {
    parse_options: markdown::ParseOptions,
}

impl Default for ExcerptExtractor {
    fn default() -> Self {
        Self::new()
    }
}

impl ExcerptExtractor {
    pub fn new() -> Self {
        let parse_options = markdown_config::with_frontmatter_and_gfm();

        Self { parse_options }
    }

    /// Extract plain text excerpt from markdown AST (first paragraph)
    /// Strips all formatting, returns full paragraph text
    pub fn extract_excerpt_from_markdown(&self, markdown: &str) -> Option<String> {
        let root = to_mdast(markdown, &self.parse_options).ok()?;
        self.extract_first_paragraph_from_ast(&root)
    }

    /// Extract first paragraph text from AST, stripping formatting
    fn extract_first_paragraph_from_ast(&self, root: &Node) -> Option<String> {
        if let Node::Root(root_node) = root {
            for child in &root_node.children {
                if let Node::Paragraph(para) = child {
                    let text = extract_text_from_nodes(&para.children);
                    let trimmed = text.trim();
                    if !trimmed.is_empty() {
                        return Some(trimmed.to_string());
                    }
                }
            }
        }
        None
    }

    /// Extract plain text excerpt from content (for non-markdown parsers)
    /// Splits by double newlines to find first non-empty paragraph
    pub fn extract_excerpt_from_plain_text(content: &str) -> Option<String> {
        // Split by double newlines and find first non-empty paragraph
        for para in content.split("\n\n") {
            let trimmed = para.trim();
            if !trimmed.is_empty() {
                return Some(trimmed.to_string());
            }
        }
        None
    }
}

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

    #[test]
    fn test_excerpt_extraction_from_markdown() {
        let content = r#"
# Title

This is the first paragraph with **bold** and *italic* text.

This is the second paragraph."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This is the first paragraph with bold and italic text.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_empty_content() {
        let content = "";
        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(excerpt, None);
    }

    #[test]
    fn test_excerpt_extraction_no_paragraphs() {
        let content = "# Just a heading";
        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(excerpt, None);
    }

    #[test]
    fn test_excerpt_extraction_with_code() {
        let content = r#"
# Title

This paragraph has `inline code` and **bold** text.

More content."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This paragraph has inline code and bold text.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_with_links() {
        let content = r#"
# Title

This paragraph has a [link](https://example.com) and more text.

More content."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This paragraph has a link and more text.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_complex_markdown() {
        let content = r#"
# Title

This paragraph has **bold**, *italic*, `code`, and [links](https://example.com) all mixed together.

Second paragraph here."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some(
                "This paragraph has bold, italic, code, and links all mixed together.".to_string()
            )
        );
    }

    #[test]
    fn test_excerpt_extraction_with_frontmatter() {
        let content = r#"+++
title = "Test Title"
+++

# First Title

This is the first paragraph that should be extracted as an excerpt.

This is the second paragraph."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This is the first paragraph that should be extracted as an excerpt.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_with_images() {
        let content = r#"
# Title

This paragraph has ![alt text](image.jpg) an image and text.

More content."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This paragraph has alt text an image and text.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_nested_formatting() {
        let content = r#"
# Title

This has **bold with *italic* inside** and `code` text.

More content."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This has bold with italic inside and code text.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_with_strikethrough() {
        let content = r#"
# Title

This has ~~strikethrough~~ and regular text.

More content."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This has strikethrough and regular text.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_whitespace_only_paragraph() {
        let content = r#"
# Title


   
This has actual content after empty paragraph.

More content."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This has actual content after empty paragraph.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_leading_whitespace() {
        let content = r#"
# Title
   
   This paragraph has leading whitespace.

More content."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This paragraph has leading whitespace.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_trailing_whitespace() {
        let content = r#"
# Title

This paragraph has trailing whitespace.   
   

More content."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This paragraph has trailing whitespace.".to_string())
        );
    }

    #[test]
    fn test_plain_text_excerpt_extraction() {
        assert_eq!(
            ExcerptExtractor::extract_excerpt_from_plain_text(
                "First paragraph.\n\nSecond paragraph."
            ),
            Some("First paragraph.".to_string())
        );
    }

    #[test]
    fn test_plain_text_excerpt_single_paragraph() {
        let content = "Just one paragraph without double newlines.";
        assert_eq!(
            ExcerptExtractor::extract_excerpt_from_plain_text(content),
            Some("Just one paragraph without double newlines.".to_string())
        );
    }

    #[test]
    fn test_plain_text_excerpt_empty() {
        assert_eq!(ExcerptExtractor::extract_excerpt_from_plain_text(""), None);
    }

    #[test]
    fn test_plain_text_excerpt_whitespace_only() {
        assert_eq!(
            ExcerptExtractor::extract_excerpt_from_plain_text("   \n\n   "),
            None
        );
    }

    #[test]
    fn test_plain_text_excerpt_single_newlines() {
        let content = "First paragraph.\nSecond line.\n\nThird paragraph.";
        assert_eq!(
            ExcerptExtractor::extract_excerpt_from_plain_text(content),
            Some("First paragraph.\nSecond line.".to_string())
        );
    }

    #[test]
    fn test_plain_text_excerpt_leading_whitespace() {
        let content = "   \n\nFirst paragraph after whitespace.";
        assert_eq!(
            ExcerptExtractor::extract_excerpt_from_plain_text(content),
            Some("First paragraph after whitespace.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_with_blockquote() {
        let content = r#"
# Title

> This is a blockquote
> with multiple lines.

This is the first real paragraph.

This is the second paragraph."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This is the first real paragraph.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_with_code_block() {
        let content = r#"
# Title

```rust
fn main() {
    println!("Hello");
}
```

This is the first paragraph after code block.

This is the second paragraph."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This is the first paragraph after code block.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_with_list() {
        let content = r#"
# Title

- First item
- Second item
- Third item

This is the first paragraph after list.

This is the second paragraph."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This is the first paragraph after list.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_complex_document() {
        let content = r#"+++
title = "Complex Document"
excerpt = "This should be ignored"
+++

# Document Title

> This is a quote
> with multiple lines

## Introduction

This is the first real paragraph with **bold** text and `inline code`.

- List item 1
- List item 2

This is the second paragraph with [a link](https://example.com).

## Conclusion

Final paragraph here."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("This is the first real paragraph with bold text and inline code.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_unicode_content() {
        let content = r#"
# Заголовок

Это первый абзац с **жирным** текстом и *курсивом*.

Второй абзац здесь."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(
            excerpt,
            Some("Это первый абзац с жирным текстом и курсивом.".to_string())
        );
    }

    #[test]
    fn test_excerpt_extraction_mixed_content() {
        let content = r#"
# Title

First paragraph.

```code
Some code here
```

Second paragraph with **bold**."#;

        let extractor = ExcerptExtractor::new();
        let excerpt = extractor.extract_excerpt_from_markdown(content);
        assert_eq!(excerpt, Some("First paragraph.".to_string()));
    }
}