|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- extern crate gutenberg;
- extern crate tera;
- extern crate tempdir;
-
- use std::collections::HashMap;
- use std::fs::{File, create_dir};
- use std::path::Path;
-
- use tempdir::TempDir;
- use tera::Tera;
-
- use gutenberg::{Page, Config};
-
-
- #[test]
- fn test_can_parse_a_valid_page() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- slug = "hello-world"
- +++
- Hello world"#;
- let res = Page::parse(Path::new("post.md"), content, &Config::default());
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
-
- assert_eq!(page.meta.title.unwrap(), "Hello".to_string());
- assert_eq!(page.meta.slug.unwrap(), "hello-world".to_string());
- assert_eq!(page.raw_content, "Hello world".to_string());
- assert_eq!(page.content, "<p>Hello world</p>\n".to_string());
- }
-
- #[test]
- fn test_can_find_one_parent_directory() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- slug = "hello-world"
- +++
- Hello world"#;
- let res = Page::parse(Path::new("content/posts/intro.md"), content, &Config::default());
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- assert_eq!(page.components, vec!["posts".to_string()]);
- }
-
- #[test]
- fn test_can_find_multiple_parent_directories() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- slug = "hello-world"
- +++
- Hello world"#;
- let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &Config::default());
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- assert_eq!(page.components, vec!["posts".to_string(), "intro".to_string()]);
- }
-
- #[test]
- fn test_can_make_url_from_sections_and_slug() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- slug = "hello-world"
- +++
- Hello world"#;
- let mut conf = Config::default();
- conf.base_url = "http://hello.com/".to_string();
- let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &conf);
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- assert_eq!(page.path, "posts/intro/hello-world");
- assert_eq!(page.permalink, "http://hello.com/posts/intro/hello-world");
- }
-
- #[test]
- fn test_can_make_permalink_with_non_trailing_slash_base_url() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- slug = "hello-world"
- +++
- Hello world"#;
- let mut conf = Config::default();
- conf.base_url = "http://hello.com".to_string();
- let res = Page::parse(Path::new("content/posts/intro/hello-world.md"), content, &conf);
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- assert_eq!(page.path, "posts/intro/hello-world");
- assert_eq!(page.permalink, format!("{}{}", conf.base_url, "/posts/intro/hello-world"));
- }
-
- #[test]
- fn test_can_make_url_from_slug_only() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- slug = "hello-world"
- +++
- Hello world"#;
- let res = Page::parse(Path::new("start.md"), content, &Config::default());
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- assert_eq!(page.path, "hello-world");
- assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "hello-world"));
- }
-
- #[test]
- fn test_errors_on_invalid_front_matter_format() {
- let content = r#"
- title = "Hello"
- description = "hey there"
- slug = "hello-world"
- +++
- Hello world"#;
- let res = Page::parse(Path::new("start.md"), content, &Config::default());
- assert!(res.is_err());
- }
-
- #[test]
- fn test_can_make_slug_from_non_slug_filename() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- +++
- Hello world"#;
- let res = Page::parse(Path::new("file with space.md"), content, &Config::default());
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- assert_eq!(page.slug, "file-with-space");
- assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "file-with-space"));
- }
-
- #[test]
- fn test_trim_slug_if_needed() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- +++
- Hello world"#;
- let res = Page::parse(Path::new(" file with space.md"), content, &Config::default());
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- assert_eq!(page.slug, "file-with-space");
- assert_eq!(page.permalink, format!("{}{}", Config::default().base_url, "file-with-space"));
- }
-
- #[test]
- fn test_reading_analytics_short() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- +++
- Hello world"#;
- let res = Page::parse(Path::new("hello.md"), content, &Config::default());
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- let (word_count, reading_time) = page.get_reading_analytics();
- assert_eq!(word_count, 2);
- assert_eq!(reading_time, 0);
- }
-
- #[test]
- fn test_reading_analytics_long() {
- let mut content = r#"
- +++
- title = "Hello"
- description = "hey there"
- +++
- Hello world"#.to_string();
- for _ in 0..1000 {
- content.push_str(" Hello world");
- }
- let res = Page::parse(Path::new("hello.md"), &content, &Config::default());
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- let (word_count, reading_time) = page.get_reading_analytics();
- assert_eq!(word_count, 2002);
- assert_eq!(reading_time, 10);
- }
-
- #[test]
- fn test_automatic_summary_is_empty_string() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- +++
- Hello world"#.to_string();
- let res = Page::parse(Path::new("hello.md"), &content, &Config::default());
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- assert_eq!(page.summary, None);
- }
-
- #[test]
- fn test_can_specify_summary() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- +++
- Hello world
- <!-- more -->
- "#.to_string();
- let res = Page::parse(Path::new("hello.md"), &content, &Config::default());
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- assert_eq!(page.summary, Some("<p>Hello world</p>\n".to_string()));
- }
-
- #[test]
- fn test_can_auto_detect_when_highlighting_needed() {
- let content = r#"
- +++
- title = "Hello"
- description = "hey there"
- +++
- ```
- Hey there
- ```
- "#.to_string();
- let mut config = Config::default();
- config.highlight_code = Some(true);
- let res = Page::parse(Path::new("hello.md"), &content, &config);
- assert!(res.is_ok());
- let mut page = res.unwrap();
- page.render_markdown(&HashMap::default(), &Tera::default(), &Config::default()).unwrap();
- assert!(page.content.starts_with("<pre"));
- }
-
- #[test]
- fn test_page_with_assets_gets_right_parent_path() {
- let tmp_dir = TempDir::new("example").expect("create temp dir");
- let path = tmp_dir.path();
- create_dir(&path.join("content")).expect("create content temp dir");
- create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
- let nested_path = path.join("content").join("posts").join("assets");
- create_dir(&nested_path).expect("create nested temp dir");
- File::create(nested_path.join("index.md")).unwrap();
- File::create(nested_path.join("example.js")).unwrap();
- File::create(nested_path.join("graph.jpg")).unwrap();
- File::create(nested_path.join("fail.png")).unwrap();
-
- let res = Page::parse(
- nested_path.join("index.md").as_path(),
- "+++\nurl=\"hey\"+++\n",
- &Config::default()
- );
- assert!(res.is_ok());
- let page = res.unwrap();
- assert_eq!(page.parent_path, path.join("content").join("posts"));
- }
-
- #[test]
- fn test_file_not_named_index_with_assets() {
- let tmp_dir = TempDir::new("example").expect("create temp dir");
- File::create(tmp_dir.path().join("something.md")).unwrap();
- File::create(tmp_dir.path().join("example.js")).unwrap();
- File::create(tmp_dir.path().join("graph.jpg")).unwrap();
- File::create(tmp_dir.path().join("fail.png")).unwrap();
-
- let page = Page::from_file(tmp_dir.path().join("something.md"), &Config::default());
- assert!(page.is_err());
- }
|