mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-01 15:00:57 +00:00

This commit solves issue #719. This is done by making the markdown filter borrow the string and simplifying the filter to accept `&str` instead of `AsRef<str>` Add test for the markdown filter using as input Revert markdown filter changes Revert unnecessary changes Improve test_markdown_owned_string test Use cargo fmt
91 lines
1.9 KiB
Rust
91 lines
1.9 KiB
Rust
#![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\
|
|
<script>alert('Lol, hacked!')</script>\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"
|
|
)
|
|
}
|