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
//! Renders a markdown file by using `pulldown_cmark`

use std::cell::RefCell;
use std::rc::Rc;

use pulldown_cmark::{html, Options, Parser};

use virtual_dom_rs::prelude::*;

use crate::store::Store;

pub struct MdView {
    store: Rc<RefCell<Store>>,
}

impl MdView {
    pub fn new(store: Rc<RefCell<Store>>) -> MdView {
        MdView { store }
    }
}

impl View for MdView {
    fn render(&self) -> VirtualNode {
        let state = self.store.borrow();

        match state.file_contents() {
            Some(file_contents) => {
                let mut options = Options::empty();
                options.insert(Options::ENABLE_STRIKETHROUGH);

                let parser = Parser::new_ext(&file_contents, options);

                let mut html_output = String::new();
                html::push_html(&mut html_output, parser);

                #[cfg(target_arch = "wasm32")]
                html! {
                    <div
                        unsafe_inner_html=html_output
                    ></div>
                }
                #[cfg(not(target_arch = "wasm32"))]
                VirtualNode::text(html_output)
            }
            None => VirtualNode::text(""),
        }
    }
}