Page meta from frontmatter
This commit is contained in:
@@ -12,19 +12,24 @@ use axum::{
|
||||
use chrono::{DateTime, Utc};
|
||||
use libservice::ServiceConnectInfo;
|
||||
use lru::LruCache;
|
||||
use maud::{Markup, Render, html};
|
||||
use maud::{Markup, PreEscaped, Render, html};
|
||||
use parking_lot::Mutex;
|
||||
use serde::Deserialize;
|
||||
use std::{collections::HashMap, num::NonZero, sync::Arc, time::Duration};
|
||||
use tracing::{debug, trace};
|
||||
|
||||
use crate::components::{md::Markdown, misc::Backlinks};
|
||||
use crate::components::{
|
||||
md::{FrontMatter, Markdown},
|
||||
misc::Backlinks,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
|
||||
pub struct PageMetadata {
|
||||
pub title: String,
|
||||
pub author: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub image: Option<String>,
|
||||
pub slug: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for PageMetadata {
|
||||
@@ -34,6 +39,7 @@ impl Default for PageMetadata {
|
||||
author: None,
|
||||
description: None,
|
||||
image: None,
|
||||
slug: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,16 +109,37 @@ impl Page {
|
||||
(self.generate_html)(self)
|
||||
}
|
||||
|
||||
pub fn from_markdown(meta: PageMetadata, md: impl Into<String>) -> Self {
|
||||
pub fn from_markdown(md: impl Into<String>, default_image: Option<String>) -> Self {
|
||||
let md: String = md.into();
|
||||
let md = Markdown::parse(&md);
|
||||
|
||||
let mut meta = md
|
||||
.children
|
||||
.get(0)
|
||||
.map(|x| x.cast::<FrontMatter>())
|
||||
.flatten()
|
||||
.map(|x| serde_yaml::from_str::<PageMetadata>(&x.content))
|
||||
.unwrap_or(Ok(Default::default()))
|
||||
.unwrap_or(PageMetadata {
|
||||
title: "Invalid frontmatter!".into(),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
if meta.image.is_none() {
|
||||
meta.image = default_image
|
||||
}
|
||||
|
||||
let html = PreEscaped(md.render());
|
||||
|
||||
// TODO: define metadata and backlinks in markdown
|
||||
Page {
|
||||
meta,
|
||||
generate_html: Box::new(move |page| {
|
||||
html! {
|
||||
(Backlinks(&[("/", "home")], &page.meta.title))
|
||||
(Markdown(&md))
|
||||
@if let Some(slug) = &page.meta.slug {
|
||||
(Backlinks(&[("/", "home")], slug))
|
||||
}
|
||||
|
||||
(html)
|
||||
}
|
||||
}),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user