aboutsummaryrefslogtreecommitdiffstats
path: root/testing/tests/markdown.rs
blob: 04c9ec97ac20356e86718c2b7c0abe1e9a0d215b (plain) (blame)
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
#![cfg(feature = "markdown")]

use askama::Template;
use comrak::Options;

#[derive(Template)]
#[template(source = "{{before}}{{content|markdown}}{{after}}", ext = "html")]
struct MarkdownTemplate<'a> {
    before: &'a str,
    after: &'a str,
    content: &'a str,
}

#[test]
fn test_markdown() {
    let s = MarkdownTemplate {
        before: "before",
        after: "after",
        content: "* 1\n* <script>alert('Lol, hacked!')</script>\n* 3",
    };
    assert_eq!(
        s.render().unwrap(),
        "\
before\
<ul>\n\
<li>1</li>\n\
<li>\n\
&lt;script&gt;alert('Lol, hacked!')&lt;/script&gt;\n\
</li>\n\
<li>3</li>\n\
</ul>\n\
after",
    );
}

#[derive(Template)]
#[template(
    source = "{{before}}{{content|markdown(options)}}{{after}}",
    ext = "html"
)]
struct MarkdownWithOptionsTemplate<'a> {
    before: &'a str,
    after: &'a str,
    content: &'a str,
    options: &'a Options,
}

#[test]
fn test_markdown_with_options() {
    let mut options = Options::default();
    options.render.unsafe_ = true;

    let s = MarkdownWithOptionsTemplate {
        before: "before",
        after: "after",
        content: "* 1\n* <script>alert('Lol, hacked!')</script>\n* 3",
        options: &options,
    };
    assert_eq!(
        s.render().unwrap(),
        "\
before\
<ul>\n\
<li>1</li>\n\
<li>\n\
<script>alert('Lol, hacked!')</script>\n\
</li>\n\
<li>3</li>\n\
</ul>\n\
after",
    );
}

#[derive(Template)]
#[template(source = "{{content|markdown}}", ext = "html")]
struct MarkdownStringTemplate {
    content: String,
}

// Tests if the markdown filter accepts String
#[test]
fn test_markdown_owned_string() {
    let template = MarkdownStringTemplate {
        content: "The markdown filter _indeed_ works with __String__".into(),
    };
    assert_eq!(
        template.render().unwrap(),
        "<p>The markdown filter <em>indeed</em> works with <strong>String</strong></p>\n"
    )
}