Parsing metadata
This commit is contained in:
parent
34e81035d0
commit
4a5e2cd99b
4 changed files with 54 additions and 13 deletions
|
@ -1,3 +1,6 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct PostMetadata {
|
||||
image_path: Option<String>,
|
||||
title: String,
|
||||
|
@ -6,6 +9,7 @@ pub struct PostMetadata {
|
|||
project_link: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Post {
|
||||
metadata: PostMetadata,
|
||||
content: String,
|
||||
|
@ -15,22 +19,23 @@ cfg_if::cfg_if! {
|
|||
if #[cfg(feature = "ssr")] {
|
||||
impl From<String> for Post {
|
||||
fn from(content: String) -> Self {
|
||||
use gray_matter::{Matter, engine::YAML};
|
||||
let matter = Matter::<YAML>::new();
|
||||
|
||||
let post_data = matter
|
||||
.parse_with_struct::<PostMetadata>(&content)
|
||||
.expect("Unable to parse md frontmatter");
|
||||
|
||||
let metadata = post_data.data;
|
||||
let content = post_data.content;
|
||||
|
||||
use pulldown_cmark::{Parser, Options, html};
|
||||
|
||||
let parser = Parser::new_ext(&content, Options::all());
|
||||
|
||||
let mut html_output = String::new();
|
||||
html::push_html(&mut html_output, parser);
|
||||
println!("\nHTML output:\n{}\n", &html_output);
|
||||
|
||||
Self {
|
||||
metadata: PostMetadata {
|
||||
image_path: None,
|
||||
title: String::from(""),
|
||||
date: String::from(""),
|
||||
description: String::from(""),
|
||||
project_link: None,
|
||||
},
|
||||
metadata,
|
||||
content: html_output,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,10 @@ cfg_if! {
|
|||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let _ = app::models::Post::get_all();
|
||||
let posts = app::models::Post::get_all();
|
||||
for post in posts.unwrap() {
|
||||
println!("{:?}", post);
|
||||
}
|
||||
|
||||
// Setting this to None means we'll be using cargo-leptos and its env vars.
|
||||
let conf = get_configuration(None).await.unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue