Add some default shortcodes
This commit is contained in:
parent
a6b8caf6de
commit
d03974270d
|
@ -26,7 +26,7 @@ mod site;
|
|||
mod markdown;
|
||||
mod section;
|
||||
|
||||
pub use site::Site;
|
||||
pub use site::{Site, GUTENBERG_TERA};
|
||||
pub use config::{Config, get_config};
|
||||
pub use front_matter::{FrontMatter, split_content};
|
||||
pub use page::{Page, populate_previous_and_next_pages};
|
||||
|
|
|
@ -236,7 +236,7 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
|
|||
|
||||
match error {
|
||||
Some(e) => Err(e),
|
||||
None => Ok(html),
|
||||
None => Ok(html.replace("<p></p>", "")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -245,18 +245,12 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
|
|||
mod tests {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use site::GUTENBERG_TERA;
|
||||
use tera::Tera;
|
||||
|
||||
use config::Config;
|
||||
use super::{markdown_to_html, parse_shortcode};
|
||||
|
||||
fn create_test_tera() -> Tera {
|
||||
let mut tera = Tera::default();
|
||||
tera.add_raw_template("shortcodes/youtube.html", "Youtube video: {{id}}").unwrap();
|
||||
tera.add_raw_template("shortcodes/quote.html", "Quote: {{body}} - {{author}}").unwrap();
|
||||
tera
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_simple_shortcode_one_arg() {
|
||||
let (name, args) = parse_shortcode(r#"{{ youtube(id="w7Ft2ymGmfc") }}"#);
|
||||
|
@ -326,28 +320,53 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_markdown_to_html_simple_shortcode() {
|
||||
fn test_markdown_to_html_with_shortcode() {
|
||||
let res = markdown_to_html(r#"
|
||||
Hello
|
||||
{{ youtube(id="w7Ft2ymGmfc") }}
|
||||
"#, &HashMap::new(), &create_test_tera(), &Config::default()).unwrap();
|
||||
assert_eq!(res, "<p>Hello\n</p>Youtube video: w7Ft2ymGmfc");
|
||||
|
||||
{{ youtube(id="ub36ffWAqgQ") }}
|
||||
"#, &HashMap::new(), &GUTENBERG_TERA, &Config::default()).unwrap();
|
||||
assert!(res.contains("<p>Hello</p>\n<div >"));
|
||||
assert!(res.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_markdown_to_html_with_several_shortcode_in_row() {
|
||||
let res = markdown_to_html(r#"
|
||||
Hello
|
||||
|
||||
{{ youtube(id="ub36ffWAqgQ") }}
|
||||
|
||||
{{ youtube(id="ub36ffWAqgQ", autoplay=true) }}
|
||||
|
||||
{{ vimeo(id="210073083") }}
|
||||
|
||||
{{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }}
|
||||
|
||||
"#, &HashMap::new(), &GUTENBERG_TERA, &Config::default()).unwrap();
|
||||
assert!(res.contains("<p>Hello</p>\n<div >"));
|
||||
assert!(res.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
|
||||
assert!(res.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ?autoplay=1""#));
|
||||
assert!(res.contains(r#"//player.vimeo.com/video/210073083""#));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_markdown_to_html_shortcode_in_code_block() {
|
||||
let res = markdown_to_html(r#"```{{ youtube(id="w7Ft2ymGmfc") }}```"#, &HashMap::new(), &create_test_tera(), &Config::default()).unwrap();
|
||||
let res = markdown_to_html(r#"```{{ youtube(id="w7Ft2ymGmfc") }}```"#, &HashMap::new(), &GUTENBERG_TERA, &Config::default()).unwrap();
|
||||
assert_eq!(res, "<p><code>{{ youtube(id="w7Ft2ymGmfc") }}</code></p>\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_markdown_to_html_shortcode_with_body() {
|
||||
let mut tera = Tera::default();
|
||||
tera.extend(&GUTENBERG_TERA).unwrap();
|
||||
tera.add_raw_template("shortcodes/quote.html", "<blockquote>{{ body }} - {{ author}}</blockquote>").unwrap();
|
||||
let res = markdown_to_html(r#"
|
||||
Hello
|
||||
{% quote(author="Keats") %}
|
||||
A quote
|
||||
{% end %}
|
||||
"#, &HashMap::new(), &create_test_tera(), &Config::default()).unwrap();
|
||||
assert_eq!(res, "<p>Hello\n</p>Quote: A quote - Keats");
|
||||
"#, &HashMap::new(), &tera, &Config::default()).unwrap();
|
||||
assert_eq!(res, "<p>Hello\n</p><blockquote>A quote - Keats</blockquote>");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,12 +16,16 @@ use section::{Section};
|
|||
|
||||
|
||||
lazy_static! {
|
||||
static ref GUTENBERG_TERA: Tera = {
|
||||
pub static ref GUTENBERG_TERA: Tera = {
|
||||
let mut tera = Tera::default();
|
||||
tera.add_raw_templates(vec![
|
||||
("rss.xml", include_str!("templates/rss.xml")),
|
||||
("sitemap.xml", include_str!("templates/sitemap.xml")),
|
||||
("robots.txt", include_str!("templates/robots.txt")),
|
||||
|
||||
("shortcodes/youtube.html", include_str!("templates/shortcodes/youtube.html")),
|
||||
("shortcodes/vimeo.html", include_str!("templates/shortcodes/vimeo.html")),
|
||||
("shortcodes/gist.html", include_str!("templates/shortcodes/gist.html")),
|
||||
]).unwrap();
|
||||
tera
|
||||
};
|
||||
|
|
3
src/templates/shortcodes/gist.html
Normal file
3
src/templates/shortcodes/gist.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
<div>
|
||||
<script src="{{ url }}.js{% if file %}?file={{file}}{% endif %}"></script>
|
||||
</div>
|
4
src/templates/shortcodes/vimeo.html
Normal file
4
src/templates/shortcodes/vimeo.html
Normal file
|
@ -0,0 +1,4 @@
|
|||
<div {% if class %}class="{{class}}"{% endif %}>
|
||||
<iframe src="//player.vimeo.com/video/{{id}}" webkitallowfullscreen mozallowfullscreen allowfullscreen>
|
||||
</iframe>
|
||||
</div>
|
4
src/templates/shortcodes/youtube.html
Normal file
4
src/templates/shortcodes/youtube.html
Normal file
|
@ -0,0 +1,4 @@
|
|||
<div {% if class %}class="{{class}}"{% endif %}>
|
||||
<iframe src="https://www.youtube.com/embed/{{id}}{% if autoplay %}?autoplay=1{% endif %}" webkitallowfullscreen mozallowfullscreen allowfullscreen>
|
||||
</iframe>
|
||||
</div>
|
|
@ -5,3 +5,10 @@ date = "2017-04-01"
|
|||
+++
|
||||
|
||||
A simple page
|
||||
|
||||
{{ youtube(id="e1C9kpMV2e8") }}
|
||||
{{ youtube(id="e1C9kpMV2e8", autoplay=true) }}
|
||||
|
||||
{{ vimeo(id="210073083") }}
|
||||
|
||||
{{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }}
|
||||
|
|
Loading…
Reference in a new issue