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

29
Cargo.lock generated
View file

@ -1034,6 +1034,18 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "gray_matter"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cf2fb99fac0b821a4e61c61abff076324bb0e5c3b4a83815bbc3518a38971ad"
dependencies = [
"serde",
"serde_json",
"toml",
"yaml-rust",
]
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.3.22" version = "0.3.22"
@ -1571,6 +1583,12 @@ dependencies = [
"serde_test", "serde_test",
] ]
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.11" version = "0.4.11"
@ -1822,6 +1840,7 @@ dependencies = [
"futures", "futures",
"glob", "glob",
"gloo-net 0.4.0", "gloo-net 0.4.0",
"gray_matter",
"leptos", "leptos",
"leptos_actix", "leptos_actix",
"leptos_icons", "leptos_icons",
@ -1829,6 +1848,7 @@ dependencies = [
"leptos_router", "leptos_router",
"log", "log",
"pulldown-cmark", "pulldown-cmark",
"serde",
"simple_logger", "simple_logger",
"wasm-bindgen", "wasm-bindgen",
] ]
@ -3018,6 +3038,15 @@ version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9828b178da53440fa9c766a3d2f73f7cf5d0ac1fe3980c1e5018d899fd19e07b" checksum = "9828b178da53440fa9c766a3d2f73f7cf5d0ac1fe3980c1e5018d899fd19e07b"
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]
[[package]] [[package]]
name = "yansi" name = "yansi"
version = "1.0.0-rc.1" version = "1.0.0-rc.1"

View file

@ -36,8 +36,10 @@ actix-files = { version = "0.6", optional = true }
actix-web = { version = "4", features = ["macros"], optional = true } actix-web = { version = "4", features = ["macros"], optional = true }
futures = { version = "0.3", optional = true } futures = { version = "0.3", optional = true }
simple_logger = { version = "4.2", optional = true } simple_logger = { version = "4.2", optional = true }
pulldown-cmark = { version = "0.9", optional = true }
glob = { version = "0.3", optional = true } glob = { version = "0.3", optional = true }
serde = { version = "1.0", optional = true }
pulldown-cmark = { version = "0.9", optional = true } # markdown parser
gray_matter = { version = "0.2", optional = true } # frontmatter parser
[features] [features]
default = ["csr"] default = ["csr"]
@ -66,8 +68,10 @@ ssr = [
"dep:actix-files", "dep:actix-files",
"dep:futures", "dep:futures",
"dep:simple_logger", "dep:simple_logger",
"dep:pulldown-cmark",
"dep:glob", "dep:glob",
"dep:serde",
"dep:pulldown-cmark",
"dep:gray_matter",
] ]
[package.metadata.cargo-all-features] [package.metadata.cargo-all-features]

View file

@ -1,3 +1,6 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PostMetadata { pub struct PostMetadata {
image_path: Option<String>, image_path: Option<String>,
title: String, title: String,
@ -6,6 +9,7 @@ pub struct PostMetadata {
project_link: Option<String>, project_link: Option<String>,
} }
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Post { pub struct Post {
metadata: PostMetadata, metadata: PostMetadata,
content: String, content: String,
@ -15,22 +19,23 @@ cfg_if::cfg_if! {
if #[cfg(feature = "ssr")] { if #[cfg(feature = "ssr")] {
impl From<String> for Post { impl From<String> for Post {
fn from(content: String) -> Self { 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}; use pulldown_cmark::{Parser, Options, html};
let parser = Parser::new_ext(&content, Options::all()); let parser = Parser::new_ext(&content, Options::all());
let mut html_output = String::new(); let mut html_output = String::new();
html::push_html(&mut html_output, parser); html::push_html(&mut html_output, parser);
println!("\nHTML output:\n{}\n", &html_output);
Self { Self {
metadata: PostMetadata { metadata,
image_path: None,
title: String::from(""),
date: String::from(""),
description: String::from(""),
project_link: None,
},
content: html_output, content: html_output,
} }
} }

View file

@ -11,7 +11,10 @@ cfg_if! {
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { 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. // Setting this to None means we'll be using cargo-leptos and its env vars.
let conf = get_configuration(None).await.unwrap(); let conf = get_configuration(None).await.unwrap();