feat: render content as markdown text in archive page

This commit is contained in:
Kilerd Chan 2018-10-10 13:10:47 +08:00
parent 1d38b63974
commit e902768a82
3 changed files with 17 additions and 4 deletions

View File

@ -16,3 +16,4 @@ lazy_static = "1.1"
serde = "1.0"
serde_derive = "1.0"
tera = "*"
pulldown-cmark = { version = "0.1.2", default-features = false }

View File

@ -14,6 +14,8 @@ extern crate rocket_contrib;
#[macro_use]
extern crate serde_derive;
extern crate pulldown_cmark;
mod pg_pool;
mod schema;
mod models;
@ -30,13 +32,15 @@ use tera::Context;
use rocket_contrib::Template;
use rocket::response::Redirect;
use pulldown_cmark::{html, Parser};
#[get("/")]
fn index(conn: DbConn) -> Template {
let mut context = Context::new();
let result = posts.filter(published.eq(true)).load::<Post>(&*conn).expect("cannot load posts");
context.add("posts", &result);
context.insert("posts", &result);
println!("{:?}", result);
@ -47,9 +51,15 @@ fn index(conn: DbConn) -> Template {
fn single_archives(conn: DbConn, archives_id: i32) -> Template {
let mut context = Context::new();
let result = posts.find(archives_id).first::<Post>(&*conn).expect("");
let result: Post = posts.find(archives_id).first::<Post>(&*conn).expect("");
context.add("post", &result);
let parser = Parser::new(&result.body);
let mut content_buf = String::new();
html::push_html(&mut content_buf, parser);
context.insert("post", &result);
context.insert("content", &content_buf);
Template::render("archives", &context)
}

View File

@ -1,4 +1,6 @@
<div>
<h1>{{ post.title }}</h1>
<p>{{ post.body }}</p>
<div>
{{ content }}
</div>
</div>