Ver código fonte

Implement {{ date }} variable for entries

master
Dylan Baker 6 anos atrás
pai
commit
117c7fff9d
1 arquivos alterados com 22 adições e 0 exclusões
  1. 22
    0
      src/entry.rs

+ 22
- 0
src/entry.rs Ver arquivo

@@ -24,6 +24,10 @@ pub struct Entry {
24 24
 
25 25
 impl Entry {
26 26
     fn render(&self, template: &str, config: &Config) -> String {
27
+        let formatted_date = match self.date {
28
+            Some(date) => date.format("%Y-%m-%d").to_string(),
29
+            None => String::from(""),
30
+        };
27 31
         template
28 32
             .replace(
29 33
                 "{{ page_title }}",
@@ -35,6 +39,7 @@ impl Entry {
35 39
                 "{{ body }}",
36 40
                 &markdown_to_html(&self.body, &ComrakOptions::default()),
37 41
             )
42
+            .replace("{{ date }}", &formatted_date)
38 43
     }
39 44
 }
40 45
 
@@ -377,4 +382,21 @@ mod tests {
377 382
         assert_eq!(post.body, "This is the body");
378 383
         assert_eq!(post.date, None);
379 384
     }
385
+
386
+    #[test]
387
+    fn test_display_entry_date() {
388
+        let post = Entry {
389
+            kind: EntryKind::Post,
390
+            title: String::from("Test Title"),
391
+            slug: String::from("one"),
392
+            body: String::from("This is the body"),
393
+            date: Some(NaiveDate::from_ymd(2000, 1, 1)),
394
+        };
395
+
396
+        let config = Config {
397
+            site_name: String::from("Test Site"),
398
+        };
399
+
400
+        assert_eq!("2000-01-01", post.render("{{ date }}", &config));
401
+    }
380 402
 }

Carregando…
Cancelar
Salvar