Parsing metadata

This commit is contained in:
Florian RICHER 2023-11-26 16:04:31 +01:00
parent 34e81035d0
commit 4a5e2cd99b
4 changed files with 54 additions and 13 deletions

View file

@ -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,
}
}