streamdown-parser 0.1.4

Streaming markdown parser for streamdown
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
//! Inline markdown parser.
//!
//! This module handles parsing of inline markdown formatting including
//! bold, italic, underline, strikethrough, inline code, links, images,
//! and footnotes.

use crate::tokenizer::{Token, Tokenizer};
use streamdown_ansi::codes::digit_to_superscript;

/// Result of parsing inline content.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InlineElement {
    /// Plain text
    Text(String),
    /// Bold text
    Bold(String),
    /// Italic text
    Italic(String),
    /// Bold and italic text
    BoldItalic(String),
    /// Underlined text
    Underline(String),
    /// Strikethrough text
    Strikeout(String),
    /// Inline code
    Code(String),
    /// A link
    Link { text: String, url: String },
    /// An image
    Image { alt: String, url: String },
    /// Footnote reference (as superscript)
    Footnote(String),
}

/// State for tracking active formatting.
#[derive(Debug, Clone, Default)]
struct FormatState {
    /// Bold is active
    bold: bool,
    /// Italic is active
    italic: bool,
    /// Underline is active
    underline: bool,
    /// Strikeout is active
    strikeout: bool,
    /// In inline code (with backtick count)
    code_backticks: Option<usize>,
    /// Code content buffer
    code_buffer: String,
}

impl FormatState {
    fn new() -> Self {
        Self::default()
    }

    #[allow(dead_code)]
    fn any_active(&self) -> bool {
        self.bold
            || self.italic
            || self.underline
            || self.strikeout
            || self.code_backticks.is_some()
    }

    fn reset(&mut self) {
        self.bold = false;
        self.italic = false;
        self.underline = false;
        self.strikeout = false;
        self.code_backticks = None;
        self.code_buffer.clear();
    }
}

/// Inline markdown parser.
///
/// Parses inline formatting and returns structured elements.
#[derive(Debug)]
pub struct InlineParser {
    tokenizer: Tokenizer,
    state: FormatState,
    /// Whether to process links
    pub process_links: bool,
    /// Whether to process images  
    pub process_images: bool,
}

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

impl InlineParser {
    /// Create a new inline parser.
    pub fn new() -> Self {
        Self {
            tokenizer: Tokenizer::new(),
            state: FormatState::new(),
            process_links: true,
            process_images: true,
        }
    }

    /// Create parser with specific settings.
    pub fn with_settings(process_links: bool, process_images: bool) -> Self {
        Self {
            tokenizer: Tokenizer::with_settings(process_links, process_images),
            state: FormatState::new(),
            process_links,
            process_images,
        }
    }

    /// Parse a line of markdown and return inline elements.
    ///
    /// This is the main entry point for inline parsing.
    pub fn parse(&mut self, line: &str) -> Vec<InlineElement> {
        let tokens = self.tokenizer.tokenize(line);
        self.parse_tokens(&tokens)
    }

    /// Parse a sequence of tokens into inline elements.
    fn parse_tokens(&mut self, tokens: &[Token]) -> Vec<InlineElement> {
        let mut elements = Vec::new();
        let mut buffer = String::new();
        let mut i = 0;

        while i < tokens.len() {
            let token = &tokens[i];

            // If we're in code mode, handle specially
            if let Some(expected_backticks) = self.state.code_backticks {
                match token {
                    Token::Backticks(n) if *n == expected_backticks => {
                        // End of inline code
                        let code = std::mem::take(&mut self.state.code_buffer);
                        // Trim single leading/trailing space (Markdown spec)
                        let code = code.strip_prefix(' ').unwrap_or(&code);
                        let code = code.strip_suffix(' ').unwrap_or(code);
                        elements.push(InlineElement::Code(code.to_string()));
                        self.state.code_backticks = None;
                    }
                    _ => {
                        // Add to code buffer
                        match token {
                            Token::Text(s) => self.state.code_buffer.push_str(s),
                            Token::Backticks(n) => {
                                self.state.code_buffer.push_str(&"`".repeat(*n));
                            }
                            _ => {
                                if let Some(marker) = token.marker_str() {
                                    self.state.code_buffer.push_str(marker);
                                }
                            }
                        }
                    }
                }
                i += 1;
                continue;
            }

            match token {
                Token::Text(s) => {
                    buffer.push_str(s);
                }

                Token::Backticks(n) => {
                    // Flush buffer
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }
                    // Start inline code
                    self.state.code_backticks = Some(*n);
                }

                Token::TripleAsterisk => {
                    // Flush buffer first
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }

                    if self.state.bold && self.state.italic {
                        // End both
                        self.state.bold = false;
                        self.state.italic = false;
                    } else if !self.state.bold && !self.state.italic {
                        // Start both
                        self.state.bold = true;
                        self.state.italic = true;
                    } else {
                        // Mixed state - just emit as text
                        buffer.push_str("***");
                    }
                }

                Token::DoubleAsterisk => {
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }
                    self.state.bold = !self.state.bold;
                }

                Token::Asterisk => {
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }
                    self.state.italic = !self.state.italic;
                }

                Token::DoubleAsteriskUnderscore => {
                    // **_ = start bold + start italic
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }
                    if !self.state.bold {
                        self.state.bold = true;
                    }
                    self.state.italic = !self.state.italic;
                }

                Token::UnderscoreDoubleAsterisk => {
                    // _** = end italic + end bold
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }
                    self.state.italic = false;
                    self.state.bold = false;
                }

                Token::TripleUnderscore => {
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }

                    if self.state.underline && self.state.italic {
                        self.state.underline = false;
                        self.state.italic = false;
                    } else if !self.state.underline && !self.state.italic {
                        self.state.underline = true;
                        self.state.italic = true;
                    } else {
                        buffer.push_str("___");
                    }
                }

                Token::DoubleUnderscore => {
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }
                    self.state.underline = !self.state.underline;
                }

                Token::Underscore => {
                    // Check context - underscore in middle of word shouldn't trigger italic.
                    // We check the ADJACENT character, not the entire token, because tokens
                    // may contain spaces (e.g., "use sem" before "_search tool").
                    let prev_char_is_alnum = i > 0
                        && matches!(&tokens[i - 1], Token::Text(s) if s.chars().last().map(|c| c.is_alphanumeric()).unwrap_or(false));
                    let next_char_is_alnum = i + 1 < tokens.len()
                        && matches!(&tokens[i + 1], Token::Text(s) if s.chars().next().map(|c| c.is_alphanumeric()).unwrap_or(false));

                    if prev_char_is_alnum && next_char_is_alnum {
                        // Underscore in middle of word - treat as text
                        buffer.push('_');
                    } else {
                        if !buffer.is_empty() {
                            self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                        }
                        self.state.italic = !self.state.italic;
                    }
                }

                Token::DoubleTilde => {
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }
                    self.state.strikeout = !self.state.strikeout;
                }

                Token::Link { text, url } => {
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }
                    elements.push(InlineElement::Link {
                        text: text.clone(),
                        url: url.clone(),
                    });
                }

                Token::Image { alt, url } => {
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }
                    elements.push(InlineElement::Image {
                        alt: alt.clone(),
                        url: url.clone(),
                    });
                }

                Token::Footnote(num) => {
                    if !buffer.is_empty() {
                        self.emit_formatted(&mut elements, std::mem::take(&mut buffer));
                    }
                    // Convert number to superscript
                    let superscript = number_to_superscript(*num);
                    elements.push(InlineElement::Footnote(superscript));
                }
            }

            i += 1;
        }

        // Flush remaining buffer
        if !buffer.is_empty() {
            self.emit_formatted(&mut elements, buffer);
        }

        // Flush any unclosed code block
        if self.state.code_backticks.is_some() {
            let code = std::mem::take(&mut self.state.code_buffer);
            if !code.is_empty() {
                elements.push(InlineElement::Code(code));
            }
            self.state.code_backticks = None;
        }

        // Reset state for next line
        self.state.reset();

        elements
    }

    /// Emit formatted text based on current state.
    fn emit_formatted(&self, elements: &mut Vec<InlineElement>, text: String) {
        if text.is_empty() {
            return;
        }

        if self.state.bold && self.state.italic {
            elements.push(InlineElement::BoldItalic(text));
        } else if self.state.bold {
            elements.push(InlineElement::Bold(text));
        } else if self.state.italic {
            elements.push(InlineElement::Italic(text));
        } else if self.state.underline {
            elements.push(InlineElement::Underline(text));
        } else if self.state.strikeout {
            elements.push(InlineElement::Strikeout(text));
        } else {
            elements.push(InlineElement::Text(text));
        }
    }

    /// Reset the parser state.
    pub fn reset(&mut self) {
        self.state.reset();
    }
}

/// Convert a number to superscript string.
fn number_to_superscript(num: u32) -> String {
    num.to_string()
        .chars()
        .map(|c| {
            let digit = c.to_digit(10).unwrap_or(0) as u8;
            digit_to_superscript(digit)
        })
        .collect()
}

/// Format a line with inline markdown.
///
/// This is a convenience function that parses a line and returns
/// the formatted result as ANSI-styled text.
pub fn format_line(line: &str, process_links: bool, process_images: bool) -> String {
    use streamdown_ansi::codes::*;
    use streamdown_ansi::style::*;

    let mut parser = InlineParser::with_settings(process_links, process_images);
    let elements = parser.parse(line);

    let mut result = String::new();

    for element in elements {
        match element {
            InlineElement::Text(s) => result.push_str(&s),
            InlineElement::Bold(s) => {
                result.push_str(BOLD.0);
                result.push_str(&s);
                result.push_str(BOLD.1);
            }
            InlineElement::Italic(s) => {
                result.push_str(ITALIC.0);
                result.push_str(&s);
                result.push_str(ITALIC.1);
            }
            InlineElement::BoldItalic(s) => {
                result.push_str(BOLD.0);
                result.push_str(ITALIC.0);
                result.push_str(&s);
                result.push_str(ITALIC.1);
                result.push_str(BOLD.1);
            }
            InlineElement::Underline(s) => {
                result.push_str(UNDERLINE.0);
                result.push_str(&s);
                result.push_str(UNDERLINE.1);
            }
            InlineElement::Strikeout(s) => {
                result.push_str(STRIKEOUT.0);
                result.push_str(&s);
                result.push_str(STRIKEOUT.1);
            }
            InlineElement::Code(s) => {
                result.push_str(DIM_ON);
                result.push_str(&s);
                result.push_str(DIM_OFF);
            }
            InlineElement::Link { text, url } => {
                result.push_str(LINK.0);
                result.push_str(&url);
                result.push('\x1b');
                result.push_str(UNDERLINE.0);
                result.push_str(&text);
                result.push_str(UNDERLINE.1);
                result.push_str(LINK.1);
            }
            InlineElement::Image { alt, url: _ } => {
                result.push_str(DIM_ON);
                result.push_str("[\u{1F5BC} ");
                result.push_str(&alt);
                result.push(']');
                result.push_str(DIM_OFF);
            }
            InlineElement::Footnote(s) => {
                result.push_str(&s);
            }
        }
    }

    result
}

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

    #[test]
    fn test_parse_plain_text() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("Hello world");
        assert_eq!(
            elements,
            vec![InlineElement::Text("Hello world".to_string())]
        );
    }

    #[test]
    fn test_parse_bold() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("Hello **bold** world");
        assert_eq!(
            elements,
            vec![
                InlineElement::Text("Hello ".to_string()),
                InlineElement::Bold("bold".to_string()),
                InlineElement::Text(" world".to_string()),
            ]
        );
    }

    #[test]
    fn test_parse_italic() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("Hello *italic* world");
        assert_eq!(
            elements,
            vec![
                InlineElement::Text("Hello ".to_string()),
                InlineElement::Italic("italic".to_string()),
                InlineElement::Text(" world".to_string()),
            ]
        );
    }

    #[test]
    fn test_parse_bold_italic() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("Hello ***bold italic*** world");
        assert_eq!(
            elements,
            vec![
                InlineElement::Text("Hello ".to_string()),
                InlineElement::BoldItalic("bold italic".to_string()),
                InlineElement::Text(" world".to_string()),
            ]
        );
    }

    #[test]
    fn test_parse_strikethrough() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("Hello ~~strike~~ world");
        assert_eq!(
            elements,
            vec![
                InlineElement::Text("Hello ".to_string()),
                InlineElement::Strikeout("strike".to_string()),
                InlineElement::Text(" world".to_string()),
            ]
        );
    }

    #[test]
    fn test_parse_inline_code() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("Use `code` here");
        assert_eq!(
            elements,
            vec![
                InlineElement::Text("Use ".to_string()),
                InlineElement::Code("code".to_string()),
                InlineElement::Text(" here".to_string()),
            ]
        );
    }

    #[test]
    fn test_parse_double_backtick_code() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("Use `` `backticks` `` here");
        assert_eq!(
            elements,
            vec![
                InlineElement::Text("Use ".to_string()),
                InlineElement::Code("`backticks`".to_string()),
                InlineElement::Text(" here".to_string()),
            ]
        );
    }

    #[test]
    fn test_parse_link() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("Check [this](http://example.com) out");

        assert!(elements.iter().any(|e| matches!(
            e,
            InlineElement::Link { text, url }
            if text == "this" && url == "http://example.com"
        )));
    }

    #[test]
    fn test_parse_image() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("See ![alt text](http://img.png) here");

        assert!(elements.iter().any(|e| matches!(
            e,
            InlineElement::Image { alt, url }
            if alt == "alt text" && url == "http://img.png"
        )));
    }

    #[test]
    fn test_parse_footnote() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("Some text[^1] here");

        assert!(elements
            .iter()
            .any(|e| matches!(e, InlineElement::Footnote(s) if s == "¹")));
    }

    #[test]
    fn test_parse_footnote_multi_digit() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("Reference[^42]");

        assert!(elements
            .iter()
            .any(|e| matches!(e, InlineElement::Footnote(s) if s == "⁴²")));
    }

    #[test]
    fn test_underscore_in_word() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("some_variable_name");
        // Underscores in middle of word should not trigger formatting
        assert_eq!(
            elements,
            vec![InlineElement::Text("some_variable_name".to_string())]
        );
    }

    #[test]
    fn test_underscore_in_word_with_surrounding_text() {
        // This is the key test case - underscore in word with spaces around
        // Previously this would incorrectly parse "_search tool" as italic
        let mut parser = InlineParser::new();
        let elements = parser.parse("use sem_search tool");
        assert_eq!(
            elements,
            vec![InlineElement::Text("use sem_search tool".to_string())]
        );
    }

    #[test]
    fn test_underscore_at_start_of_text() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("sem_search");
        assert_eq!(
            elements,
            vec![InlineElement::Text("sem_search".to_string())]
        );
    }

    #[test]
    fn test_underscore_at_end_of_text() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("sem_search is useful");
        assert_eq!(
            elements,
            vec![InlineElement::Text("sem_search is useful".to_string())]
        );
    }

    #[test]
    fn test_multiple_underscores_in_text() {
        let mut parser = InlineParser::new();
        let elements = parser.parse("use my_var_name here");
        assert_eq!(
            elements,
            vec![InlineElement::Text("use my_var_name here".to_string())]
        );
    }

    #[test]
    fn test_underscore_italic_still_works() {
        // Real italic with underscores should still work
        let mut parser = InlineParser::new();
        let elements = parser.parse("this is _italic_ text");
        assert_eq!(
            elements,
            vec![
                InlineElement::Text("this is ".to_string()),
                InlineElement::Italic("italic".to_string()),
                InlineElement::Text(" text".to_string()),
            ]
        );
    }

    #[test]
    fn test_underscore_italic_at_boundaries() {
        // Italic at word boundaries (space before underscore)
        let mut parser = InlineParser::new();
        let elements = parser.parse("word _italic_");
        assert_eq!(
            elements,
            vec![
                InlineElement::Text("word ".to_string()),
                InlineElement::Italic("italic".to_string()),
            ]
        );
    }

    #[test]
    fn test_mixed_underscore_scenarios() {
        // Mix of variable names and real italic
        let mut parser = InlineParser::new();
        let elements = parser.parse("use my_func for _emphasis_");
        assert_eq!(
            elements,
            vec![
                InlineElement::Text("use my_func for ".to_string()),
                InlineElement::Italic("emphasis".to_string()),
            ]
        );
    }

    #[test]
    fn test_format_line() {
        let result = format_line("Hello **bold** world", true, true);
        assert!(result.contains("bold"));
        assert!(result.contains("\x1b[1m")); // Bold on
        assert!(result.contains("\x1b[22m")); // Bold off
    }

    #[test]
    fn test_number_to_superscript() {
        assert_eq!(number_to_superscript(0), "");
        assert_eq!(number_to_superscript(1), "¹");
        assert_eq!(number_to_superscript(2), "²");
        assert_eq!(number_to_superscript(42), "⁴²");
        assert_eq!(number_to_superscript(123), "¹²³");
    }
}