Browse Source

Newline to <br>, ignore non-json files

master
Dylan Baker 3 years ago
parent
commit
3f2118fe9d
2 changed files with 9 additions and 4 deletions
  1. 8
    3
      src/main.rs
  2. 1
    1
      templates/admin/index.html

+ 8
- 3
src/main.rs View File

@@ -1,4 +1,5 @@
1 1
 use std::env;
2
+use std::ffi::OsStr;
2 3
 use std::fs::{read_dir, File};
3 4
 use std::io::prelude::*;
4 5
 use std::io::{Error, ErrorKind};
@@ -57,9 +58,12 @@ async fn read_all_posts() -> Result<Vec<Post>, Error> {
57 58
 
58 59
     for file in read_dir(path)? {
59 60
         let file = file?;
60
-        let contents = read_to_string(file.path()).await?;
61
-        let post: Post = serde_json::from_str(&contents)?;
62
-        posts.push(post);
61
+        if let Some("json") = file.path().extension().and_then(OsStr::to_str) {
62
+            let contents = read_to_string(file.path()).await?;
63
+            let mut post: Post = serde_json::from_str(&contents)?;
64
+            post.body = post.body.replace("\n", "<br>");
65
+            posts.push(post);
66
+        }
63 67
     }
64 68
 
65 69
     Ok(posts)
@@ -101,6 +105,7 @@ async fn main() -> std::io::Result<()> {
101 105
             let mut post: Post = req.body_form().await?;
102 106
             post.id = Uuid::new_v4().to_string();
103 107
             post.date = Local::now().date().naive_local().to_string();
108
+            post.body = post.body.trim().to_owned();
104 109
             post.save()?;
105 110
             Ok(tide::Redirect::new("/admin"))
106 111
         });

+ 1
- 1
templates/admin/index.html View File

@@ -21,7 +21,7 @@
21 21
   <div class="posts__post post">
22 22
     <h3 class="post__heading">{{ post.title }} // {{ post.date }}</h3>
23 23
     <div class="post__body">
24
-      {{ post.body }}
24
+      {{ post.body | safe }}
25 25
     </div>
26 26
   </div>
27 27
   {% endfor %}

Loading…
Cancel
Save