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
use std::cell::RefCell;

use comrak::nodes::{Ast, NodeValue};
use comrak::{parse_document, Arena, Options};

use crate::prelude::*;

pub struct Markdown {}

impl Markdown {
    pub fn new<'a>(cx: &'a mut Context, document: &str) -> Handle<'a, Self> {
        Self {}
            .build(cx, |cx| {
                // The returned nodes are created in the supplied Arena, and are bound by its lifetime.
                let arena = Arena::new();

                let mut options = Options::default();
                options.extension.strikethrough = true;

                // Parse the document into a root `AstNode`
                let root = parse_document(&arena, document, &options);

                for node in root.children() {
                    parse_node(cx, node, 0);
                }
            })
            .height(Auto)
    }
}

impl View for Markdown {
    fn element(&self) -> Option<&'static str> {
        Some("markdown")
    }
}

fn parse_node<'a>(
    cx: &mut Context,
    node: &'a comrak::arena_tree::Node<'a, RefCell<Ast>>,
    list_level: usize,
) {
    match &node.data.borrow().value {
        NodeValue::Paragraph => {
            Label::rich(cx, "", |cx| {
                for child in node.children() {
                    parse_node(cx, child, list_level);
                }
            })
            .class("p");
        }

        NodeValue::Heading(heading) => {
            Label::rich(cx, "", |cx| {
                for child in node.children() {
                    parse_node(cx, child, list_level);
                }
            })
            .class(match heading.level {
                1 => "h1",
                2 => "h2",
                3 => "h3",
                4 => "h4",
                5 => "h5",
                6 => "h6",
                _ => "h6",
            });
        }

        NodeValue::Text(text) => {
            TextSpan::new(cx, text, |_| {}).class("span");
        }

        NodeValue::Emph => {
            TextSpan::new(cx, "", |cx| {
                for child in node.children() {
                    parse_node(cx, child, list_level);
                }
            })
            .class("emph");
        }

        NodeValue::Strong => {
            TextSpan::new(cx, "", |cx| {
                for child in node.children() {
                    parse_node(cx, child, list_level);
                }
            })
            .class("strong");
        }

        NodeValue::Strikethrough => {
            TextSpan::new(cx, "", |cx| {
                for child in node.children() {
                    parse_node(cx, child, list_level);
                }
            })
            .class("strikethrough");
        }

        NodeValue::List(_list) => {
            VStack::new(cx, |cx| {
                for child in node.children() {
                    parse_node(cx, child, list_level);
                }
            })
            .height(Auto)
            .left(Pixels(20.0));
        }

        NodeValue::Item(_list) => {
            HStack::new(cx, |cx| {
                Label::new(cx, "\u{2022} ").width(Auto);
                VStack::new(cx, |cx| {
                    for child in node.children() {
                        parse_node(cx, child, list_level + 1);
                    }
                })
                .height(Auto);
            })
            .class("li")
            .height(Auto);
        }

        NodeValue::Code(code) => {
            TextSpan::new(cx, &code.literal.to_owned(), |_| {}).class("code");
        }

        NodeValue::CodeBlock(code_block) => {
            println!("{:?}", code_block);
            let mut code = code_block.literal.to_owned();
            code.pop().unwrap();
            ScrollView::new(cx, 0.0, 0.0, true, false, |cx| {
                Label::new(cx, code).class("code");
            })
            .height(Auto)
            .width(Stretch(1.0));
        }

        NodeValue::Link(link) => {
            let url = link.url.clone();
            TextSpan::new(cx, "", |cx| {
                for child in node.children() {
                    parse_node(cx, child, list_level);
                }
            })
            .cursor(CursorIcon::Hand)
            .pointer_events(PointerEvents::Auto)
            .on_press(move |_| {
                open::that(url.as_str()).unwrap();
            })
            .class("link");
        }

        NodeValue::SoftBreak => {
            TextSpan::new(cx, "\n", |cx| {
                for child in node.children() {
                    parse_node(cx, child, list_level);
                }
            });
        }

        _ => {}
    }
}

pub struct TextSpan {}

impl TextSpan {
    pub fn new<'a>(
        cx: &'a mut Context,
        text: &str,
        children: impl Fn(&mut Context),
    ) -> Handle<'a, Self> {
        Self {}
            .build(cx, |cx| {
                cx.style.text_span.insert(cx.current(), true);
                children(cx);
            })
            .text(text)
            .display(Display::None)
            .pointer_events(PointerEvents::None)
    }
}

impl View for TextSpan {
    fn element(&self) -> Option<&'static str> {
        Some("text-span")
    }
}