From d6b479ec15616ae48dfcdceaa254c2bfa86e92bf Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Tue, 14 Apr 2020 21:11:21 +0800 Subject: [PATCH] support per-post theme_config in custom metadata --- src/blog.rs | 4 +++- src/render.rs | 6 ++++-- src/sn.rs | 7 ++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/blog.rs b/src/blog.rs index 6f32ed9..d9bf530 100644 --- a/src/blog.rs +++ b/src/blog.rs @@ -71,7 +71,9 @@ pub struct Post { // We keep the original content here // so that we could make changes to the Markdown parser // in the future; we won't be stuck with a parsed version - pub content: String + pub content: String, + // Some arbitrary data that could be used by the theme + pub theme_config: Option } impl Post { diff --git a/src/render.rs b/src/render.rs index 2386d40..859c7bd 100644 --- a/src/render.rs +++ b/src/render.rs @@ -94,7 +94,8 @@ struct PostContext { title: String, url: String, timestamp: u64, - content: String + content: String, + theme_config: Option } lazy_static! { @@ -228,7 +229,8 @@ pub async fn render_post(url: Url, post: blog::Post) -> MyResult { title: post.title, url: post.url, timestamp: post.timestamp, - content: post_cache.content + content: post_cache.content, + theme_config: post.theme_config }; HANDLEBARS.render("post.hbs", &context) diff --git a/src/sn.rs b/src/sn.rs index df88045..a76c7b4 100644 --- a/src/sn.rs +++ b/src/sn.rs @@ -96,6 +96,8 @@ struct CustomMetadata { // `unlist` takes precedence over this alias unlisted: Option, url: Option, + // Same as Post.theme_config + theme_config: Option, timestamp: Option // Should be something `js_sys::Date::parse` could handle } @@ -187,11 +189,13 @@ async fn create_or_update_post(req: Request, url: Url) -> MyResult { let text = data.items[0].content.text.clone(); let title = data.items[0].content.title.clone(); let (custom_metadata, text) = parse_custom_metadata_from_content(text); + let theme_config = custom_metadata.as_ref().and_then(|it| it.theme_config.clone()); let metadata = build_metadata(custom_metadata, &uuid, &title); let post = match blog::Post::find_by_uuid(&uuid).await { Ok(mut post) => { post.content = text; post.title = title; + post.theme_config = theme_config; // Update metadata if custom ones are present if metadata.has_custom_url { @@ -210,7 +214,8 @@ async fn create_or_update_post(req: Request, url: Url) -> MyResult { uuid: uuid, title: title, content: text, - timestamp: metadata.timestamp + timestamp: metadata.timestamp, + theme_config: theme_config } } };