Browse Source

Add inline option of markdown filter

Fix #197
index-subcmd
Vincent Prouillet 6 years ago
parent
commit
29ce510aa3
3 changed files with 30 additions and 1 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +22
    -1
      components/templates/src/filters.rs
  3. +7
    -0
      docs/content/documentation/templates/overview.md

+ 1
- 0
CHANGELOG.md View File

@@ -3,6 +3,7 @@
## 0.3.1 (unreleased)

- Update Tera to fix regression
- Add option for inline in markdown filter

## 0.3.0 (2018-01-25)



+ 22
- 1
components/templates/src/filters.rs View File

@@ -5,13 +5,25 @@ use pulldown_cmark as cmark;
use tera::{Value, to_value, Result as TeraResult};


pub fn markdown(value: Value, _: HashMap<String, Value>) -> TeraResult<Value> {
pub fn markdown(value: Value, args: HashMap<String, Value>) -> TeraResult<Value> {
let s = try_get_value!("markdown", "value", String, value);
let inline = match args.get("inline") {
Some(val) => try_get_value!("markdown", "inline", bool, val),
None => false,
};

let mut html = String::new();
let parser = cmark::Parser::new(&s);
cmark::html::push_html(&mut html, parser);

if inline {
html = html
.trim_left_matches("<p>")
// pulldown_cmark finishes a paragraph with `</p>\n`
.trim_right_matches("</p>\n")
.to_string();
}

Ok(to_value(&html).unwrap())
}

@@ -50,6 +62,15 @@ mod tests {
assert_eq!(result.unwrap(), to_value(&"<h1>Hey</h1>\n").unwrap());
}

#[test]
fn markdown_filter_inline() {
let mut args = HashMap::new();
args.insert("inline".to_string(), to_value(true).unwrap());
let result = markdown(to_value(&"Using `map`, `filter`, and `fold` instead of `for`").unwrap(), args);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(&"Using <code>map</code>, <code>filter</code>, and <code>fold</code> instead of <code>for</code>").unwrap());
}

#[test]
fn base64_encode_filter() {
// from https://tools.ietf.org/html/rfc4648#section-10


+ 7
- 0
docs/content/documentation/templates/overview.md View File

@@ -30,6 +30,13 @@ Gutenberg adds a few filters, in addition of the ones already present in Tera.
Converts the given variable to HTML using Markdown. This doesn't apply any of the
features that Gutenberg adds to Markdown: internal links, shortcodes etc won't work.

By default, the filter will wrap all text into a paragraph. To disable that, you can
pass `true` to the inline argument:

```jinja2
{{ some_text | markdown(inline=true) }}
```

### base64_encode
Encode the variable to base64.



Loading…
Cancel
Save