web-framework-markdown 0.0.1

An abstract API to create markdown components in any rust framework
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use core::ops::Range;

use core::marker::PhantomData;

use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;

use pulldown_cmark::{Alignment, CodeBlockKind, Event, Tag, TagEnd};

#[derive(Eq, PartialEq)]
enum MathMode {
    Inline,
    Display,
}

#[cfg(feature = "maths")]
use katex;

use super::HtmlElement::*;
use super::{Context, ElementAttributes, HtmlError, LinkDescription, MdComponentProps};

use crate::component::{ComponentCall, CustomHtmlTag};

// load the default syntect options to highlight code
lazy_static::lazy_static! {
    static ref SYNTAX_SET: SyntaxSet = {
        SyntaxSet::load_defaults_newlines()
    };
    static ref THEME_SET: ThemeSet = {
        ThemeSet::load_defaults()
    };
}

impl HtmlError {
    fn not_implemented(message: impl ToString) -> Self {
        HtmlError::NotImplemented(message.to_string())
    }
    fn syntax(message: impl ToString) -> Self {
        HtmlError::Syntax(message.to_string())
    }
    fn component(name: impl ToString, msg: impl ToString) -> Self {
        HtmlError::CustomComponent {
            name: name.to_string(),
            msg: msg.to_string(),
        }
    }
}

impl ToString for HtmlError {
    fn to_string(&self) -> String {
        match self {
            HtmlError::Math => "invalid math".to_string(),
            HtmlError::NotImplemented(s) => format!("`{s}`: not implemented"),
            HtmlError::CustomComponent { name, msg } => {
                format!("Custom component `{name}` failed: `{msg}`")
            }
            HtmlError::Syntax(s) => format!("syntax error: {s}"),
            HtmlError::Link(s) => format!("invalid link: {s}"),
        }
    }
}

/// `highlight_code(content, ss, ts)` render the content `content`
/// with syntax highlighting
fn highlight_code(theme_name: Option<&str>, content: &str, kind: &CodeBlockKind) -> Option<String> {
    let lang = match kind {
        CodeBlockKind::Fenced(x) => x,
        CodeBlockKind::Indented => return None,
    };

    let theme_name = theme_name.clone().unwrap_or("base16-ocean.light");
    let theme = THEME_SET
        .themes
        .get(theme_name)
        .expect("unknown theme")
        .clone();

    Some(
        syntect::html::highlighted_html_for_string(
            content,
            &SYNTAX_SET,
            SYNTAX_SET.find_syntax_by_token(lang)?,
            &theme,
        )
        .ok()?,
    )
}

/// renders a source code in a code block, with syntax highlighting if possible.
/// `cx`: the current markdown context
/// `source`: the source to render
/// `range`: the position of the code in the original source
fn render_code_block<'a, 'callback, F: Context<'a, 'callback>>(
    cx: F,
    source: String,
    k: &CodeBlockKind,
    range: Range<usize>,
) -> F::View {
    let code_attributes = ElementAttributes {
        on_click: Some(cx.make_md_handler(range, true)),
        ..Default::default()
    };

    match highlight_code(cx.props().theme, &source, &k) {
        None => cx.el_with_attributes(
            Code,
            cx.el(Code, cx.el_text(source.into())),
            code_attributes,
        ),
        Some(x) => cx.el_span_with_inner_html(x, code_attributes),
    }
}

#[cfg(feature = "maths")]
/// `render_maths(content)` returns a html node
/// with the latex content `content` compiled inside
fn render_maths<'a, 'callback, F: Context<'a, 'callback>>(
    cx: F,
    content: &str,
    display_mode: MathMode,
    range: Range<usize>,
) -> Result<F::View, HtmlError> {
    let opts = katex::Opts::builder()
        .display_mode(display_mode == MathMode::Display)
        .build()
        .unwrap();

    let class_name = match display_mode {
        MathMode::Inline => "math-inline",
        MathMode::Display => "math-flow",
    };

    let callback = cx.make_md_handler(range, true);

    let attributes = ElementAttributes {
        classes: vec![class_name.to_string()],
        on_click: Some(callback),
        ..Default::default()
    };

    match katex::render_with_opts(content, opts) {
        Ok(x) => Ok(cx.el_span_with_inner_html(x, attributes)),
        Err(_) => Err(HtmlError::Math),
    }
}
#[cfg(not(feature = "maths"))]
fn render_maths<'a, 'callback, F: Context<'a, 'callback>>(
    _cx: F,
    _content: &str,
    _display_mode: MathMode,
    _range: Range<usize>,
) -> Result<F::View, HtmlError> {
    Err(HtmlError::Math)
}

/// `align_string(align)` gives the css string
/// that is used to align text according to `align`
fn align_string(align: Alignment) -> &'static str {
    match align {
        Alignment::Left => "text-align: left",
        Alignment::Right => "text-align: right",
        Alignment::Center => "text-align: center",
        Alignment::None => "",
    }
}

/// Manage the creation of a [`F::View`]
/// from a stream of markdown events
pub struct Renderer<'a, 'callback, 'c, I, F>
where
    I: Iterator<Item = (Event<'a>, Range<usize>)>,
    'callback: 'a,
    F: Context<'a, 'callback>,
{
    __marker: PhantomData<&'callback ()>,
    /// the markdown context
    cx: F,
    /// the stream of markdown [`Event`]s
    stream: &'c mut I,
    /// the alignment settings inside the current table
    column_alignment: Option<Vec<Alignment>>,
    /// the current horizontal index of the cell we are in.
    /// TODO: remove it
    cell_index: usize,
    /// the root tag that this renderer is rendering
    end_tag: Option<TagEnd>,
    /// the current component we are inside of.
    /// custom components doesn't allow nesting.
    current_component: Option<String>,
}

/// returns true if `raw_html`:
/// - starts with '<'
/// - ends with '>'
/// - does not have any '<' or '>' in between
fn can_be_custom_component(raw_html: &str) -> bool {
    let chars: Vec<_> = raw_html.trim().chars().collect();
    let len = chars.len();
    if len == 0 {
        return false;
    };
    let (fst, middle, last) = (chars[0], &chars[1..len - 1], chars[len - 1]);
    fst == '<' && last == '>' && middle.into_iter().all(|c| c != &'<' && c != &'>')
}

impl<'a, 'callback, 'c, I, F> Iterator for Renderer<'a, 'callback, 'c, I, F>
where
    I: Iterator<Item = (Event<'a>, Range<usize>)>,
    'callback: 'a,
    F: Context<'a, 'callback>,
{
    type Item = F::View;

    fn next(&mut self) -> Option<Self::Item> {
        use Event::*;
        let (item, range): (Event<'a>, Range<usize>) = self.stream.next()?;
        let range = range.clone();

        let cx = self.cx;

        let rendered = match item {
            Start(t) => self.render_tag(t, range),
            End(end) => {
                // check if the closing tag is the tag that was open
                // when this renderer was created
                match self.end_tag {
                    Some(t) if t == end => return None,
                    Some(t) => panic!("{end:?} is a wrong closing tag, expected {t:?}"),
                    None => panic!("didn't expect a closing tag"),
                }
            }
            Text(s) => Ok(cx.render_text(s, range)),
            Code(s) => Ok(cx.render_code(s, range)),
            InlineHtml(s) => {
                let attributes = ElementAttributes {
                    on_click: Some(self.cx.make_md_handler(range, false)),
                    ..ElementAttributes::default()
                };
                Ok(self.cx.el_span_with_inner_html(s.to_string(), attributes))
            }
            Html(_) => panic!("html outside html block"),
            FootnoteReference(_) => Err(HtmlError::not_implemented("footnotes refs")),
            SoftBreak => Ok(self.next()?),
            HardBreak => Ok(self.cx.el_br()),
            Rule => Ok(cx.render_rule(range)),
            TaskListMarker(m) => Ok(cx.render_tasklist_marker(m, range)),
            InlineMath(content) => render_maths(self.cx, &content, MathMode::Inline, range),
            DisplayMath(content) => render_maths(self.cx, &content, MathMode::Display, range),
        };

        Some(rendered.unwrap_or_else(|e| {
            self.cx.el_with_attributes(
                Span,
                self.cx
                    .el_fragment(vec![self.cx.el_text(e.to_string().into()), self.cx.el_br()]),
                ElementAttributes {
                    classes: vec!["markdown-error".to_string()],
                    on_click: None,
                    ..Default::default()
                },
            )
        }))
    }
}

impl<'a, 'callback, 'c, I, F> Renderer<'a, 'callback, 'c, I, F>
where
    I: Iterator<Item = (Event<'a>, Range<usize>)>,
    F: Context<'a, 'callback>,
{
    /// creates a new renderer from a stream of events.
    /// It returns an iterator of [`F::View`]
    pub fn new(cx: F, events: &'c mut I) -> Self {
        Self {
            __marker: PhantomData,
            cx,
            stream: events,
            column_alignment: None,
            cell_index: 0,
            end_tag: None,
            current_component: None,
        }
    }

    /// try to render `raw_html` as a custom component.
    /// - if it looks like `<Component/>` and Component is registered,
    ///     it will render the corresponding component
    /// - it it looks like `<Component>`, and Component is registered,
    /// it will extract markdown until it finds `<Component/>`
    /// In any other cases, it will render the strinng as raw html.
    fn html(&mut self, raw_html: &str, _range: Range<usize>) -> Result<F::View, HtmlError> {
        // TODO: refactor

        match &self.current_component {
            Some(current_name) => {
                if self.end_tag.is_some() {
                    return Err(HtmlError::component(
                        raw_html,
                        "please make sure there is a newline before the end of your component",
                    ));
                }
                match raw_html.parse() {
                    Ok(CustomHtmlTag::End(name)) if &name == current_name => {
                        Ok(self.next().unwrap_or(self.cx.el_empty()))
                    }
                    Ok(_) => Err(HtmlError::component(
                        current_name,
                        "expected end of component",
                    )),
                    Err(e) => Err(HtmlError::syntax(e)),
                }
            }
            None => {
                if can_be_custom_component(raw_html) {
                    match raw_html.parse() {
                        Ok(CustomHtmlTag::Inline(s)) => self.custom_component_inline(s),
                        Ok(CustomHtmlTag::End(name)) => {
                            Err(HtmlError::component(name, "expected start, not end"))
                        }
                        Ok(CustomHtmlTag::Start(s)) => self.custom_component(s),
                        Err(e) => Err(HtmlError::syntax(e)),
                    }
                } else {
                    Ok(self
                        .cx
                        .el_span_with_inner_html(raw_html.to_string(), Default::default()))
                }
            }
        }
    }

    /// renders a custom component with childrens
    fn custom_component(&mut self, description: ComponentCall) -> Result<F::View, HtmlError> {
        let name: &str = &description.name;
        if !self.cx.has_custom_component(name) {
            return Err(HtmlError::component(name, "not a valid component"));
        }

        let sub_renderer = Renderer {
            __marker: PhantomData,
            cx: self.cx,
            stream: self.stream,
            column_alignment: self.column_alignment.clone(),
            cell_index: 0,
            end_tag: self.end_tag,
            current_component: Some(description.name.clone()),
        };
        let children = self.cx.el_fragment(sub_renderer.collect());

        let props = MdComponentProps {
            attributes: description.attributes,
            children,
        };

        match self.cx.render_custom_component(name, props) {
            Ok(x) => Ok(x),
            Err(e) => Err(HtmlError::CustomComponent {
                name: name.to_string(),
                msg: e.0,
            }),
        }
    }

    /// renders a custom component without childrens
    fn custom_component_inline(
        &mut self,
        description: ComponentCall,
    ) -> Result<F::View, HtmlError> {
        let name: &str = &description.name;
        if !self.cx.has_custom_component(name) {
            return Err(HtmlError::component(name, "not a valid component"));
        }

        let props = MdComponentProps {
            attributes: description.attributes,
            children: self.cx.el_empty(),
        };

        match self.cx.render_custom_component(name, props) {
            Ok(x) => Ok(x),
            Err(e) => Err(HtmlError::CustomComponent {
                name: name.to_string(),
                msg: e.0,
            }),
        }
    }

    /// renders events in a new renderer,
    /// recursively, until the end of the tag
    fn children(&mut self, tag: Tag<'a>) -> F::View {
        let sub_renderer = Renderer {
            __marker: PhantomData,
            cx: self.cx,
            stream: self.stream,
            column_alignment: self.column_alignment.clone(),
            cell_index: 0,
            end_tag: Some(tag.to_end()),
            current_component: self.current_component.clone(),
        };
        self.cx.el_fragment(sub_renderer.collect())
    }

    /// extract the text from the next text event
    fn children_text(&mut self, tag: Tag<'a>) -> Option<String> {
        let text = match self.stream.next() {
            Some((Event::Text(s), _)) => Some(s.to_string()),
            None => None,
            _ => panic!("expected string event, got something else"),
        };

        self.assert_closing_tag(tag.to_end());
        text
    }

    // check that the closing tag is what was expected
    fn assert_closing_tag(&mut self, end: TagEnd) {
        let end_tag = &self
            .stream
            .next()
            .expect("this event should be the closing tag")
            .0;
        assert!(end_tag == &Event::End(end));
    }

    fn render_tag(&mut self, tag: Tag<'a>, range: Range<usize>) -> Result<F::View, HtmlError> {
        let mut cx = self.cx;
        Ok(match tag.clone() {
            Tag::HtmlBlock => {
                let raw_html = match self.stream.next() {
                    Some((Event::Html(s), _)) => s.to_string(),
                    None => panic!("empty html"),
                    _ => panic!("expected html event, got something else"),
                };
                self.assert_closing_tag(TagEnd::HtmlBlock);
                self.html(&raw_html, range)?
            }
            Tag::Paragraph => cx.el(Paragraph, self.children(tag)),
            Tag::Heading { level, .. } => cx.el(Heading(level as u8), self.children(tag)),
            Tag::BlockQuote(_) => cx.el(BlockQuote, self.children(tag)),
            Tag::CodeBlock(k) => {
                render_code_block(cx, self.children_text(tag).unwrap_or_default(), &k, range)
            }
            Tag::List(Some(n0)) => cx.el(Ol(n0 as i32), self.children(tag)),
            Tag::List(None) => cx.el(Ul, self.children(tag)),
            Tag::Item => cx.el(Li, self.children(tag)),
            Tag::Table(align) => {
                self.column_alignment = Some(align);
                cx.el(Table, self.children(tag))
            }
            Tag::TableHead => cx.el(Thead, self.children(tag)),
            Tag::TableRow => cx.el(Trow, self.children(tag)),
            Tag::TableCell => {
                let align = self.column_alignment.clone().unwrap()[self.cell_index];
                self.cell_index += 1;
                cx.el_with_attributes(
                    Tcell,
                    self.children(tag),
                    ElementAttributes {
                        style: Some(align_string(align).to_string()),
                        ..Default::default()
                    },
                )
            }
            Tag::Emphasis => cx.el(Italics, self.children(tag)),
            Tag::Strong => cx.el(Bold, self.children(tag)),
            Tag::Strikethrough => cx.el(StrikeThrough, self.children(tag)),
            Tag::Image {
                link_type,
                dest_url,
                title,
                ..
            } => {
                let description = LinkDescription {
                    url: dest_url.to_string(),
                    title: title.to_string(),
                    content: self.children(tag),
                    link_type,
                    image: true,
                };
                cx.render_link(description).map_err(HtmlError::Link)?
            }
            Tag::Link {
                link_type,
                dest_url,
                title,
                ..
            } => {
                let description = LinkDescription {
                    url: dest_url.to_string(),
                    title: title.to_string(),
                    content: self.children(tag),
                    link_type,
                    image: false,
                };
                cx.render_link(description).map_err(HtmlError::Link)?
            }
            Tag::FootnoteDefinition(_) => {
                return Err(HtmlError::not_implemented("footnote not implemented"))
            }
            Tag::MetadataBlock { .. } => {
                if let Some(text) = self.children_text(tag) {
                    cx.set_frontmatter(text)
                }
                cx.el_empty()
            }
            Tag::DefinitionList => {
                return Err(HtmlError::not_implemented(
                    "definition list not implemented",
                ))
            }
            Tag::DefinitionListTitle => {
                return Err(HtmlError::not_implemented(
                    "definition list not implemented",
                ))
            }
            Tag::DefinitionListDefinition => {
                return Err(HtmlError::not_implemented(
                    "definition list not implemented",
                ))
            }
            Tag::Superscript => {
                return Err(HtmlError::not_implemented("superscript not implemented"))
            }
            Tag::Subscript => return Err(HtmlError::not_implemented("subscript not implemented")),
        })
    }
}