support per-post theme_config in custom metadata

This commit is contained in:
Peter Cai 2020-04-14 21:11:21 +08:00
parent 1183250d3b
commit d6b479ec15
No known key found for this signature in database
GPG Key ID: 71F5FB4E4F3FD54F
3 changed files with 13 additions and 4 deletions

View File

@ -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<serde_json::Value>
}
impl Post {

View File

@ -94,7 +94,8 @@ struct PostContext {
title: String,
url: String,
timestamp: u64,
content: String
content: String,
theme_config: Option<serde_json::Value>
}
lazy_static! {
@ -228,7 +229,8 @@ pub async fn render_post(url: Url, post: blog::Post) -> MyResult<String> {
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)

View File

@ -96,6 +96,8 @@ struct CustomMetadata {
// `unlist` takes precedence over this alias
unlisted: Option<bool>,
url: Option<String>,
// Same as Post.theme_config
theme_config: Option<serde_json::Value>,
timestamp: Option<String> // Should be something `js_sys::Date::parse` could handle
}
@ -187,11 +189,13 @@ async fn create_or_update_post(req: Request, url: Url) -> MyResult<Response> {
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<Response> {
uuid: uuid,
title: title,
content: text,
timestamp: metadata.timestamp
timestamp: metadata.timestamp,
theme_config: theme_config
}
}
};