mirror of
https://github.com/askama-rs/askama.git
synced 2025-09-28 13:30:59 +00:00
Remove markdown
filter and comrak
dependency
This commit is contained in:
parent
9d254a3e36
commit
66b53eecaf
@ -20,7 +20,6 @@ maintenance = { status = "actively-developed" }
|
||||
default = ["config", "humansize", "num-traits", "urlencode"]
|
||||
config = ["askama_derive/config"]
|
||||
humansize = ["askama_derive/humansize", "dep_humansize"]
|
||||
markdown = ["askama_derive/markdown", "comrak"]
|
||||
num-traits = ["askama_derive/num-traits", "dep_num_traits"]
|
||||
serde-json = ["askama_derive/serde-json", "askama_escape/json", "serde", "serde_json"]
|
||||
urlencode = ["askama_derive/urlencode", "percent-encoding"]
|
||||
@ -36,7 +35,6 @@ mime_guess = []
|
||||
[dependencies]
|
||||
askama_derive = { version = "0.13", path = "../askama_derive" }
|
||||
askama_escape = { version = "0.10.3", path = "../askama_escape" }
|
||||
comrak = { version = "0.22", optional = true, default-features = false }
|
||||
dep_humansize = { package = "humansize", version = "2", optional = true }
|
||||
dep_num_traits = { package = "num-traits", version = "0.2.6", optional = true }
|
||||
percent-encoding = { version = "2.1.0", optional = true }
|
||||
|
@ -336,29 +336,6 @@ pub fn wordcount<T: fmt::Display>(s: T) -> Result<usize> {
|
||||
Ok(s.split_whitespace().count())
|
||||
}
|
||||
|
||||
#[cfg(feature = "markdown")]
|
||||
pub fn markdown<E, S>(
|
||||
e: E,
|
||||
s: S,
|
||||
options: Option<&comrak::Options>,
|
||||
) -> Result<MarkupDisplay<E, String>>
|
||||
where
|
||||
E: Escaper,
|
||||
S: AsRef<str>,
|
||||
{
|
||||
use comrak::{markdown_to_html, Options};
|
||||
|
||||
let mut defaults = Options::default();
|
||||
defaults.extension.strikethrough = true;
|
||||
defaults.extension.tagfilter = true;
|
||||
defaults.extension.table = true;
|
||||
defaults.extension.autolink = true;
|
||||
defaults.render.escape = true;
|
||||
|
||||
let s = markdown_to_html(s.as_ref(), options.unwrap_or(&defaults));
|
||||
Ok(MarkupDisplay::new_safe(s, e))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -26,7 +26,6 @@ bytes = { version = "1" }
|
||||
default = ["askama/default"]
|
||||
config = ["askama/config"]
|
||||
humansize = ["askama/humansize"]
|
||||
markdown = ["askama/markdown"]
|
||||
num-traits = ["askama/num-traits"]
|
||||
serde-json = ["askama/serde-json"]
|
||||
urlencode = ["askama/urlencode"]
|
||||
|
@ -28,7 +28,6 @@ tower = "0.4"
|
||||
default = ["askama/default"]
|
||||
config = ["askama/config"]
|
||||
humansize = ["askama/humansize"]
|
||||
markdown = ["askama/markdown"]
|
||||
num-traits = ["askama/num-traits"]
|
||||
serde-json = ["askama/serde-json"]
|
||||
urlencode = ["askama/urlencode"]
|
||||
|
@ -16,7 +16,6 @@ proc-macro = true
|
||||
[features]
|
||||
config = ["serde", "basic-toml"]
|
||||
humansize = []
|
||||
markdown = []
|
||||
urlencode = []
|
||||
serde-json = []
|
||||
num-traits = []
|
||||
|
@ -1187,45 +1187,6 @@ impl<'a> Generator<'a> {
|
||||
DisplayWrap::Unwrapped
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "markdown"))]
|
||||
fn _visit_markdown_filter(
|
||||
&mut self,
|
||||
_buf: &mut Buffer,
|
||||
_args: &[Expr<'_>],
|
||||
) -> Result<DisplayWrap, CompileError> {
|
||||
Err("the `markdown` filter requires the `markdown` feature to be enabled".into())
|
||||
}
|
||||
|
||||
#[cfg(feature = "markdown")]
|
||||
fn _visit_markdown_filter(
|
||||
&mut self,
|
||||
buf: &mut Buffer,
|
||||
args: &[Expr<'_>],
|
||||
) -> Result<DisplayWrap, CompileError> {
|
||||
let (md, options) = match args {
|
||||
[md] => (md, None),
|
||||
[md, options] => (md, Some(options)),
|
||||
_ => return Err("markdown filter expects no more than one option argument".into()),
|
||||
};
|
||||
|
||||
buf.write(&format!(
|
||||
"{CRATE}::filters::markdown({}, &",
|
||||
self.input.escaper
|
||||
));
|
||||
self.visit_expr(buf, md)?;
|
||||
match options {
|
||||
Some(options) => {
|
||||
buf.write(", ::core::option::Option::Some(");
|
||||
self.visit_expr(buf, options)?;
|
||||
buf.write(")");
|
||||
}
|
||||
None => buf.write(", ::core::option::Option::None"),
|
||||
}
|
||||
buf.write(")?");
|
||||
|
||||
Ok(DisplayWrap::Wrapped)
|
||||
}
|
||||
|
||||
fn _visit_as_ref_filter(
|
||||
&mut self,
|
||||
buf: &mut Buffer,
|
||||
@ -1258,8 +1219,6 @@ impl<'a> Generator<'a> {
|
||||
} else if name == "join" {
|
||||
self._visit_join_filter(buf, args)?;
|
||||
return Ok(DisplayWrap::Unwrapped);
|
||||
} else if name == "markdown" {
|
||||
return self._visit_markdown_filter(buf, args);
|
||||
} else if name == "as_ref" {
|
||||
self._visit_as_ref_filter(buf, args)?;
|
||||
return Ok(DisplayWrap::Wrapped);
|
||||
|
@ -150,7 +150,6 @@ const BUILT_IN_FILTERS: &[&str] = &[
|
||||
"wordcount",
|
||||
// optional features, reserve the names anyway:
|
||||
"json",
|
||||
"markdown",
|
||||
];
|
||||
|
||||
const CRATE: &str = if cfg!(feature = "with-actix-web") {
|
||||
|
@ -24,7 +24,6 @@ tokio = { version = "1.0", features = ["macros", "rt"] }
|
||||
default = ["askama/default"]
|
||||
config = ["askama/config"]
|
||||
humansize = ["askama/humansize"]
|
||||
markdown = ["askama/markdown"]
|
||||
num-traits = ["askama/num-traits"]
|
||||
serde-json = ["askama/serde-json"]
|
||||
urlencode = ["askama/urlencode"]
|
||||
|
@ -24,7 +24,6 @@ tokio = { version = "1.0", features = ["macros", "rt"] }
|
||||
default = ["askama/default"]
|
||||
config = ["askama/config"]
|
||||
humansize = ["askama/humansize"]
|
||||
markdown = ["askama/markdown"]
|
||||
num-traits = ["askama/num-traits"]
|
||||
serde-json = ["askama/serde-json"]
|
||||
urlencode = ["askama/urlencode"]
|
||||
|
@ -14,8 +14,8 @@ is passed to the next.
|
||||
```
|
||||
|
||||
Askama has a collection of built-in filters, documented below, but can also include custom filters.
|
||||
Additionally, the `json` and `markdown` filters are included in the built-in filters, but are
|
||||
disabled by default. Enable them with Cargo features (see below for more information).
|
||||
Additionally, the `json` filter is included in the built-in filters, but is disabled by default.
|
||||
Enable it with Cargo features (see below for more information).
|
||||
|
||||
**Table of contents**
|
||||
|
||||
@ -40,7 +40,6 @@ disabled by default. Enable them with Cargo features (see below for more informa
|
||||
|
||||
* **[Optional / feature gated filters][#optional-filters]:**
|
||||
[`json|tojson`][#json],
|
||||
[`markdown`][#markdown],
|
||||
|
||||
* **[Custom filters][#custom-filters]**
|
||||
|
||||
@ -391,30 +390,6 @@ Ugly: <script>var data = "{{data|json}}";</script>
|
||||
Ugly: <script>var data = '{{data|json|safe}}';</script>
|
||||
```
|
||||
|
||||
### `markdown`
|
||||
[#markdown]: #markdown
|
||||
|
||||
Enabling the `markdown` feature will enable the use of the `markdown` filter.
|
||||
This will render a value using a [GitHub flavored CommonMark](https://docs.rs/comrak/0.14.*/comrak/) syntax.
|
||||
By default the extensions “autolink”, “strikethrough”, “tagfilter”, and “table” are enabled.
|
||||
Any raw HTML gets escaped.
|
||||
|
||||
```jinja
|
||||
{{ "**<i>Hello</i>, world!**"|markdown }}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```html
|
||||
<p><strong><i>Hello</i>, world!</strong></p>
|
||||
```
|
||||
|
||||
You can change the default settings by supplying [custom options][ComrakRenderOptions], e.g. to enable unsafe raw HTML.
|
||||
You can find a usage example in our [unit tests][markdown-tests].
|
||||
|
||||
[ComrakRenderOptions]: https://docs.rs/comrak/0.12.*/comrak/struct.ComrakRenderOptions.html
|
||||
[markdown-tests]: https://github.com/djc/askama/blob/5748c357d435b24848d1571df010d777859fede9/testing/tests/markdown.rs#L36-L75
|
||||
|
||||
## Custom Filters
|
||||
[#custom-filters]: #custom-filters
|
||||
|
||||
|
@ -8,13 +8,11 @@ rust-version = "1.65"
|
||||
publish = false
|
||||
|
||||
[features]
|
||||
default = ["serde-json", "markdown"]
|
||||
default = ["serde-json"]
|
||||
serde-json = ["serde_json", "askama/serde-json"]
|
||||
markdown = ["comrak", "askama/markdown"]
|
||||
|
||||
[dependencies]
|
||||
askama = { path = "../askama", version = "0.13" }
|
||||
comrak = { version = "0.22", default-features = false, optional = true }
|
||||
phf = { version = "0.11", features = ["macros" ]}
|
||||
serde_json = { version = "1.0", optional = true }
|
||||
|
||||
|
@ -1,90 +0,0 @@
|
||||
#![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"
|
||||
)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user