Browse Source

Fix post regex to correctly capture multiple paragraphs

master
Dylan Baker 5 years ago
parent
commit
b1a20ac244
1 changed files with 29 additions and 1 deletions
  1. 29
    1
      src/post.rs

+ 29
- 1
src/post.rs View File

@@ -21,7 +21,7 @@ pub fn parse_post(path: path::PathBuf) -> Post {
21 21
     let contents = fs::read_to_string(&path).expect("Couldn't read post file");
22 22
 
23 23
     lazy_static! {
24
-        static ref re: Regex = Regex::new(r"^# (?P<title>.*)\n\n(?P<body>.*)").unwrap();
24
+        static ref re: Regex = Regex::new(r"^# (?P<title>.*)\n\n(?s)(?P<body>.*)").unwrap();
25 25
         static ref slug_re: Regex = Regex::new(r"(?P<slug>\S+).md").unwrap();
26 26
     }
27 27
 
@@ -116,4 +116,32 @@ mod tests {
116 116
 
117 117
         fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
118 118
     }
119
+
120
+    #[test]
121
+    fn test_post_with_multiple_paragraphs() {
122
+        let temp_dir = env::temp_dir();
123
+        let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
124
+        fs::create_dir(&working_dir).unwrap();
125
+        env::set_current_dir(&working_dir).unwrap();
126
+
127
+        let cwd = env::current_dir().unwrap();
128
+        fs::create_dir(cwd.join("posts")).unwrap();
129
+
130
+        let slug = Uuid::new_v4().to_string();
131
+        let filename = format!("{}.md", slug);
132
+        fs::write(
133
+            cwd.join("posts").join(&filename),
134
+            "# This is a post\n\nHere is a line\n\nHere is another line\n\nAnd a third",
135
+        ).unwrap();
136
+
137
+        let post = parse_post(cwd.join("posts").join(&filename));
138
+        assert_eq!("This is a post", post.title);
139
+        assert_eq!(
140
+            "Here is a line\n\nHere is another line\n\nAnd a third",
141
+            post.body
142
+        );
143
+        assert_eq!(slug, post.slug);
144
+
145
+        fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
146
+    }
119 147
 }

Loading…
Cancel
Save