hash_tag/
lib.rs

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
use wasm_bindgen::prelude::*;

fn get_lines(md: &str) -> Vec<String> {
    let mut lines: Vec<String> = Vec::new();
    let mut line = String::from("");
    for c in md.chars() {
        if c == '\n' {
            if !line.is_empty() {
                lines.push(line.clone());
            }
            line.clear();
        } else {
            line.push(c);
        }
    }

    if !line.is_empty() {
        lines.push(line);
    }

    lines
}

fn close_list_tags(tag_ul: &mut bool, tag_ol: &mut bool, html: &mut String) {
    if *tag_ul {
        html.push_str("</ul>");
        *tag_ul = false;
    }
    if *tag_ol {
        html.push_str("</ol>");
        *tag_ol = false;
    }
}

fn process_line(l: &str) -> String {
    let mut chars = l.chars().peekable();
    let mut line = String::from("");

    let mut tag_code = false;
    let mut tag_b = false;
    let mut tag_i = false;

    while let Some(c) = chars.next() {
        if c == '`' {
            tag_code = !tag_code;
            if tag_code {
                line.push_str("<code>");
            } else {
                line.push_str("</code>");
            }
        } else if c == '*' {
            if chars.peek() == Some(&'*') {
                chars.next();
                tag_b = !tag_b;
                if tag_b {
                    line.push_str("<b>");
                } else {
                    line.push_str("</b>");
                }
            }
        } else if c == '_' {
            tag_i = !tag_i;
            if tag_i {
                line.push_str("<i>");
            } else {
                line.push_str("</i>");
            }
        } else if c == '[' {
            let mut text = String::new();
            while let Some(&next) = chars.peek() {
                if next == ']' {
                    chars.next();
                    break;
                }
                text.push(next);
                chars.next();
            }

            if chars.peek() == Some(&'(') {
                chars.next();
                let mut url = String::new();
                while let Some(&next) = chars.peek() {
                    if next == ')' {
                        chars.next();
                        break;
                    }
                    url.push(next);
                    chars.next();
                }

                if !url.is_empty() {
                    line.push_str(&format!(
                        "<a class=\"link\" href=\"{url}\" target=\"_blank\">{text}</a>"
                    ));
                } else {
                    line.push('[');
                    line.push_str(&text);
                    line.push(']');
                }
            } else {
                line.push('[');
                line.push_str(&text);
                line.push(']');
            }
        } else {
            line.push(c);
        }
    }

    line.to_string()
}

#[wasm_bindgen]
pub fn parse(md: String) -> String {
    let mut html = String::from("");

    let lines: Vec<String> = get_lines(&md);

    let mut tag_pre = false;
    let mut tag_ul = false;
    let mut tag_ol = false;

    for l in lines.iter() {
        for (j, c) in l.chars().enumerate() {
            if j == 0 && c == '#' {
                // heading
                close_list_tags(&mut tag_ul, &mut tag_ol, &mut html);
                let count_hash = l.chars().filter(|&c| c == '#').count();
                let line = process_line(&l[count_hash + 1..]);
                let line = format!("<h{count_hash}>{line}</h{count_hash}>");
                html.push_str(&line);
            } else if j == 0 && c == '>' {
                // blockquote
                close_list_tags(&mut tag_ul, &mut tag_ol, &mut html);
                let line = process_line(&l[2..]);
                let line = format!("<blockquote>{line}</blockquote>");
                html.push_str(&line);
            } else if j == 0 && c == '-' {
                // unordered list
                if l.trim().chars().all(|ch| ch == '-') {
                    html.push_str("<hr>");
                    break;
                }
                if !tag_ul {
                    html.push_str("<ul>");
                }
                tag_ul = true;
                let line = process_line(&l[2..]);
                let line = format!("<li>{line}</li>");
                html.push_str(&line);
            } else if j == 0 && c.is_ascii_digit() {
                // ordered list
                if !tag_ol {
                    html.push_str("<ol>");
                }
                tag_ol = true;
                let line = process_line(&l[3..]);
                let line = format!("<li>{line}</li>");
                html.push_str(&line);
            } else if j == 0 && c == '`' {
                // codeblock
                close_list_tags(&mut tag_ul, &mut tag_ol, &mut html);
                let count_backtick = l.chars().filter(|&c| c == '`').count();
                if count_backtick == 3 {
                    tag_pre = !tag_pre;
                    if tag_pre {
                        html.push_str("<pre>");
                    } else {
                        html.push_str("</pre>");
                    }
                } else if count_backtick % 2 == 0 {
                    let line = process_line(l);
                    let line = format!("<p>{line}</p>");
                    html.push_str(&line);
                }
            } else if j == 0 && tag_pre {
                // paragraph
                let line = format!("<code>{l}</code>\n");
                html.push_str(&line);
            } else if j == 0 && !tag_pre {
                // paragraph
                close_list_tags(&mut tag_ul, &mut tag_ol, &mut html);
                let line = process_line(l);
                let line = format!("<p>{line}</p>");
                html.push_str(&line);
            }
        }
    }

    html
}