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
use crate::{SmartLink, ToNotedown, AST};
use markdown::{Block, ListItem, Span};

impl ToNotedown for Vec<Block> {
    fn to_notedown(&self) -> AST {
        AST::Statements(self.iter().map(ToNotedown::to_notedown).collect())
    }
}

impl ToNotedown for Block {
    fn to_notedown(&self) -> AST {
        let r = Default::default();
        match self {
            Block::Header(content, level) => AST::Header { level: *level, children: content.to_notedown().to_vec(), r },
            Block::Paragraph(p) => AST::Paragraph { children: p.to_notedown().to_vec(), r },
            Block::CodeBlock(_, _) => unimplemented!(),
            Block::Raw(_) => unimplemented!(),
            Block::Hr => unimplemented!(),
            Block::Blockquote(list) => AST::QuoteList { style: None, body: list.to_notedown().to_vec(), r },
            Block::OrderedList(list, _) => AST::OrderedList { head: 1, body: list.to_notedown().to_vec(), r },
            Block::UnorderedList(list) => AST::OrderlessList { body: list.to_notedown().to_vec(), r },
        }
    }
}

impl ToNotedown for Vec<Span> {
    fn to_notedown(&self) -> AST {
        let mut out = vec![];
        for node in self {
            let ast = node.to_notedown();
            match ast {
                AST::None => continue,
                AST::Statements(v) => out.extend(v),
                _ => out.push(ast),
            }
        }
        return AST::Statements(out);
    }
}

impl ToNotedown for Span {
    fn to_notedown(&self) -> AST {
        let r = Default::default();
        match self {
            Span::Break => unimplemented!(),
            Span::Text(t) => AST::Normal { inner: t.to_owned(), r },
            Span::Code(code) => {
                AST::Highlight { lang: String::from("txt"), code: code.to_owned(), inline: true, high_line: vec![], r }
            }
            Span::Link(text, url, title) => {
                let link = SmartLink::Hyperlinks {
                    from: text.into(),
                    to: Some(url.into()),
                    alt: title.as_ref().map(Into::into),
                    bind: None,
                };
                AST::Link { inner: link, r }
            }
            Span::Image(_, _, _) => unimplemented!(),
            Span::Emphasis(_) => unimplemented!(),
            Span::Strong(_) => unimplemented!(),
        }
    }
}

impl ToNotedown for Vec<ListItem> {
    fn to_notedown(&self) -> AST {
        AST::Statements(self.iter().map(ToNotedown::to_notedown).collect())
    }
}

impl ToNotedown for ListItem {
    fn to_notedown(&self) -> AST {
        match self {
            ListItem::Simple(s) => s.to_notedown(),
            ListItem::Paragraph(p) => AST::Paragraph { children: p.to_notedown().to_vec(), r: Default::default() },
        }
    }
}