scrybe-core 0.1.1

Scrybe core — AST, Document, ContentAddressable, Plugin trait, Workspace
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Shawn Hartsock and contributors

//! Markdown AST type definitions for Scrybe.
//!
//! The [`Ast`] type is built from pulldown-cmark events and represents
//! the document structure as a tree of [`Node`]s. Rendering lives in
//! `scrybe-render` (P1.3).

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

/// A single node in the Markdown AST.
#[derive(Debug, Clone, PartialEq)]
pub enum Node {
    /// A heading with a numeric level (1–6) and inline children.
    Heading { level: u8, children: Vec<Node> },
    /// A block of inline content.
    Paragraph { children: Vec<Node> },
    /// A fenced code block with an optional language tag.
    FencedCode { lang: String, content: String },
    /// Inline code span.
    InlineCode { content: String },
    /// A block quote containing block children.
    BlockQuote { children: Vec<Node> },
    /// An ordered or unordered list.
    List { ordered: bool, items: Vec<Node> },
    /// A list item containing block children.
    ListItem { children: Vec<Node> },
    /// Emphasised (italic) inline content.
    Emphasis { children: Vec<Node> },
    /// Strong (bold) inline content.
    Strong { children: Vec<Node> },
    /// A hyperlink.
    Link {
        href: String,
        title: String,
        children: Vec<Node>,
    },
    /// An image.
    Image { src: String, alt: String },
    /// A thematic break (`---` / `***`).
    HorizontalRule,
    /// A hard line break (`\\\n`).
    HardBreak,
    /// A soft line break (single newline in source).
    SoftBreak,
    /// Plain text.
    Text(String),
    /// Raw HTML.
    Html(String),
}

// ---------------------------------------------------------------------------
// Stack frame used while building the AST
// ---------------------------------------------------------------------------

#[derive(Debug)]
enum Frame {
    Root,
    Heading { level: u8 },
    Paragraph,
    BlockQuote,
    List { ordered: bool },
    ListItem,
    Emphasis,
    Strong,
    Link { href: String, title: String },
    Image { src: String, alt: String },
    FencedCode { lang: String },
}

/// A parsed Markdown document as a tree of [`Node`]s.
#[derive(Debug, Clone, PartialEq)]
pub struct Ast {
    /// Top-level nodes of the document.
    pub nodes: Vec<Node>,
}

impl Ast {
    /// Parses Markdown *source* into an AST.
    pub fn parse(source: &str) -> Self {
        let opts =
            Options::ENABLE_STRIKETHROUGH | Options::ENABLE_TABLES | Options::ENABLE_FOOTNOTES;
        let parser = Parser::new_ext(source, opts);

        // Each stack entry is (frame, children-accumulated-so-far).
        let mut stack: Vec<(Frame, Vec<Node>)> = vec![(Frame::Root, Vec::new())];

        // Accumulates text inside a FencedCode block before we close it.
        let mut code_buf = String::new();

        for event in parser {
            match event {
                // --- Opening tags ---
                Event::Start(tag) => match tag {
                    Tag::Heading { level, .. } => {
                        stack.push((
                            Frame::Heading {
                                level: heading_level(level),
                            },
                            Vec::new(),
                        ));
                    }
                    Tag::Paragraph => {
                        stack.push((Frame::Paragraph, Vec::new()));
                    }
                    Tag::BlockQuote(_) => {
                        stack.push((Frame::BlockQuote, Vec::new()));
                    }
                    Tag::List(start) => {
                        stack.push((
                            Frame::List {
                                ordered: start.is_some(),
                            },
                            Vec::new(),
                        ));
                    }
                    Tag::Item => {
                        stack.push((Frame::ListItem, Vec::new()));
                    }
                    Tag::Emphasis => {
                        stack.push((Frame::Emphasis, Vec::new()));
                    }
                    Tag::Strong => {
                        stack.push((Frame::Strong, Vec::new()));
                    }
                    Tag::Link {
                        dest_url, title, ..
                    } => {
                        stack.push((
                            Frame::Link {
                                href: dest_url.to_string(),
                                title: title.to_string(),
                            },
                            Vec::new(),
                        ));
                    }
                    Tag::Image {
                        dest_url, title, ..
                    } => {
                        // pulldown-cmark puts alt text as Text events before End(Image).
                        // We collect them as the alt string.
                        stack.push((
                            Frame::Image {
                                src: dest_url.to_string(),
                                alt: title.to_string(),
                            },
                            Vec::new(),
                        ));
                    }
                    Tag::CodeBlock(kind) => {
                        let lang = match kind {
                            pulldown_cmark::CodeBlockKind::Fenced(s) => s.to_string(),
                            pulldown_cmark::CodeBlockKind::Indented => String::new(),
                        };
                        code_buf.clear();
                        stack.push((Frame::FencedCode { lang }, Vec::new()));
                    }
                    // Ignore tags we don't model (tables, footnotes, etc.)
                    _ => {}
                },

                // --- Closing tags ---
                Event::End(tag_end) => {
                    let node = match tag_end {
                        TagEnd::Heading(_) => {
                            if let Some((Frame::Heading { level }, children)) = stack.pop() {
                                Some(Node::Heading { level, children })
                            } else {
                                None
                            }
                        }
                        TagEnd::Paragraph => {
                            if let Some((Frame::Paragraph, children)) = stack.pop() {
                                Some(Node::Paragraph { children })
                            } else {
                                None
                            }
                        }
                        TagEnd::BlockQuote(_) => {
                            if let Some((Frame::BlockQuote, children)) = stack.pop() {
                                Some(Node::BlockQuote { children })
                            } else {
                                None
                            }
                        }
                        TagEnd::List(_) => {
                            if let Some((Frame::List { ordered }, items)) = stack.pop() {
                                Some(Node::List { ordered, items })
                            } else {
                                None
                            }
                        }
                        TagEnd::Item => {
                            if let Some((Frame::ListItem, children)) = stack.pop() {
                                Some(Node::ListItem { children })
                            } else {
                                None
                            }
                        }
                        TagEnd::Emphasis => {
                            if let Some((Frame::Emphasis, children)) = stack.pop() {
                                Some(Node::Emphasis { children })
                            } else {
                                None
                            }
                        }
                        TagEnd::Strong => {
                            if let Some((Frame::Strong, children)) = stack.pop() {
                                Some(Node::Strong { children })
                            } else {
                                None
                            }
                        }
                        TagEnd::Link => {
                            if let Some((Frame::Link { href, title }, children)) = stack.pop() {
                                Some(Node::Link {
                                    href,
                                    title,
                                    children,
                                })
                            } else {
                                None
                            }
                        }
                        TagEnd::Image => {
                            if let Some((Frame::Image { src, alt }, _children)) = stack.pop() {
                                // For images the alt text came through as Text events;
                                // ignore those children and use the title as alt.
                                Some(Node::Image { src, alt })
                            } else {
                                None
                            }
                        }
                        TagEnd::CodeBlock => {
                            if let Some((Frame::FencedCode { lang }, _)) = stack.pop() {
                                let content = std::mem::take(&mut code_buf);
                                // Strip the trailing newline that pulldown-cmark always adds.
                                let content = content.trim_end_matches('\n').to_string();
                                Some(Node::FencedCode { lang, content })
                            } else {
                                None
                            }
                        }
                        // Unmodelled end tags — discard
                        _ => None,
                    };

                    if let Some(n) = node {
                        if let Some((_, ref mut parent_children)) = stack.last_mut() {
                            parent_children.push(n);
                        }
                    }
                }

                // --- Leaf events ---
                Event::Text(s) => {
                    // If we're inside a code block, accumulate into code_buf.
                    if matches!(stack.last(), Some((Frame::FencedCode { .. }, _))) {
                        code_buf.push_str(&s);
                    } else if let Some((_, ref mut children)) = stack.last_mut() {
                        children.push(Node::Text(s.to_string()));
                    }
                }
                Event::Code(s) => {
                    if let Some((_, ref mut children)) = stack.last_mut() {
                        children.push(Node::InlineCode {
                            content: s.to_string(),
                        });
                    }
                }
                Event::Html(s) | Event::InlineHtml(s) => {
                    if let Some((_, ref mut children)) = stack.last_mut() {
                        children.push(Node::Html(s.to_string()));
                    }
                }
                Event::SoftBreak => {
                    if let Some((_, ref mut children)) = stack.last_mut() {
                        children.push(Node::SoftBreak);
                    }
                }
                Event::HardBreak => {
                    if let Some((_, ref mut children)) = stack.last_mut() {
                        children.push(Node::HardBreak);
                    }
                }
                Event::Rule => {
                    if let Some((_, ref mut children)) = stack.last_mut() {
                        children.push(Node::HorizontalRule);
                    }
                }
                // Ignore footnote references, task list markers, etc.
                _ => {}
            }
        }

        // Drain the root frame.
        let nodes = match stack.into_iter().next() {
            Some((Frame::Root, children)) => children,
            _ => Vec::new(),
        };

        Self { nodes }
    }

    /// Returns the title: text content of the first H1 node, if any.
    pub fn title(&self) -> Option<String> {
        for node in &self.nodes {
            if let Node::Heading { level: 1, children } = node {
                let text = collect_text(children);
                if !text.is_empty() {
                    return Some(text);
                }
            }
        }
        None
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn heading_level(level: HeadingLevel) -> u8 {
    match level {
        HeadingLevel::H1 => 1,
        HeadingLevel::H2 => 2,
        HeadingLevel::H3 => 3,
        HeadingLevel::H4 => 4,
        HeadingLevel::H5 => 5,
        HeadingLevel::H6 => 6,
    }
}

/// Recursively collect plain text from a node list.
fn collect_text(nodes: &[Node]) -> String {
    let mut out = String::new();
    for node in nodes {
        match node {
            Node::Text(s) => out.push_str(s),
            Node::InlineCode { content } => out.push_str(content),
            Node::Emphasis { children }
            | Node::Strong { children }
            | Node::Link { children, .. } => {
                out.push_str(&collect_text(children));
            }
            _ => {}
        }
    }
    out
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_heading_parsing() {
        let ast = Ast::parse("# Hello\n\n## World\n");
        assert_eq!(ast.nodes.len(), 2);
        assert!(matches!(&ast.nodes[0], Node::Heading { level: 1, .. }));
        assert!(matches!(&ast.nodes[1], Node::Heading { level: 2, .. }));
    }

    #[test]
    fn test_heading_text_children() {
        let ast = Ast::parse("# My Title\n");
        if let Node::Heading { level, children } = &ast.nodes[0] {
            assert_eq!(*level, 1);
            assert!(matches!(&children[0], Node::Text(s) if s == "My Title"));
        } else {
            panic!("expected Heading");
        }
    }

    #[test]
    fn test_title_from_h1() {
        let ast = Ast::parse("# The Title\n\nSome paragraph.\n");
        assert_eq!(ast.title(), Some("The Title".to_string()));
    }

    #[test]
    fn test_title_none_when_no_h1() {
        let ast = Ast::parse("## Subheading only\n");
        assert_eq!(ast.title(), None);
    }

    #[test]
    fn test_fenced_code() {
        let src = "```rust\nfn main() {}\n```\n";
        let ast = Ast::parse(src);
        assert_eq!(ast.nodes.len(), 1);
        if let Node::FencedCode { lang, content } = &ast.nodes[0] {
            assert_eq!(lang, "rust");
            assert_eq!(content, "fn main() {}");
        } else {
            panic!("expected FencedCode, got {:?}", ast.nodes[0]);
        }
    }

    #[test]
    fn test_fenced_code_no_lang() {
        let src = "```\nhello\n```\n";
        let ast = Ast::parse(src);
        if let Node::FencedCode { lang, content } = &ast.nodes[0] {
            assert_eq!(lang, "");
            assert_eq!(content, "hello");
        } else {
            panic!("expected FencedCode");
        }
    }

    #[test]
    fn test_paragraph_and_inline_code() {
        let src = "Use `cargo test` now.\n";
        let ast = Ast::parse(src);
        assert_eq!(ast.nodes.len(), 1);
        if let Node::Paragraph { children } = &ast.nodes[0] {
            let has_inline = children
                .iter()
                .any(|n| matches!(n, Node::InlineCode { content } if content == "cargo test"));
            assert!(has_inline);
        } else {
            panic!("expected Paragraph");
        }
    }

    #[test]
    fn test_nested_list() {
        let src = "- alpha\n- beta\n";
        let ast = Ast::parse(src);
        assert_eq!(ast.nodes.len(), 1);
        if let Node::List { ordered, items } = &ast.nodes[0] {
            assert!(!ordered);
            assert_eq!(items.len(), 2);
            for item in items {
                assert!(matches!(item, Node::ListItem { .. }));
            }
        } else {
            panic!("expected List");
        }
    }

    #[test]
    fn test_ordered_list() {
        let src = "1. one\n2. two\n";
        let ast = Ast::parse(src);
        if let Node::List { ordered, .. } = &ast.nodes[0] {
            assert!(ordered);
        } else {
            panic!("expected List");
        }
    }

    #[test]
    fn test_blockquote() {
        let src = "> a quote\n";
        let ast = Ast::parse(src);
        assert!(matches!(&ast.nodes[0], Node::BlockQuote { .. }));
    }

    #[test]
    fn test_horizontal_rule() {
        let src = "---\n";
        let ast = Ast::parse(src);
        assert!(matches!(&ast.nodes[0], Node::HorizontalRule));
    }
}