From 50c64bc8650574d1e9243a50477eb7dbd36e36c4 Mon Sep 17 00:00:00 2001 From: Jakub Stachurski Date: Tue, 9 Jan 2024 17:24:11 +0100 Subject: Make the `markdown` filter compatible with `String` 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` 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 --- askama_derive/src/generator.rs | 2 +- testing/tests/markdown.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index df15bdc..760a1f6 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -1200,7 +1200,7 @@ impl<'a> Generator<'a> { }; buf.write(&format!( - "::askama::filters::markdown({}, ", + "::askama::filters::markdown({}, &", self.input.escaper )); self.visit_expr(buf, md)?; diff --git a/testing/tests/markdown.rs b/testing/tests/markdown.rs index 75163c9..04c9ec9 100644 --- a/testing/tests/markdown.rs +++ b/testing/tests/markdown.rs @@ -70,3 +70,21 @@ before\ 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(), + "

The markdown filter indeed works with String

\n" + ) +} -- cgit