From c9a7b496a0a18976d7f73bf6b632ffda43b8f3da Mon Sep 17 00:00:00 2001 From: cn-kali-team Date: Thu, 22 Feb 2024 20:20:38 +0800 Subject: [PATCH] page_cover --- src/lib.rs | 5 +- src/page.rs | 392 ++++++++++++++++++++++++++-------------------------- 2 files changed, 200 insertions(+), 197 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7a794c1..3f1d2d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -346,7 +346,10 @@ fn rewriter( ) -> Vec { let mut output = vec![]; let title = title.unwrap_or(blog_env.description); - let icon_url = icon_url.unwrap_or(blog_env.icon); + let mut icon_url = icon_url.unwrap_or(blog_env.icon); + if icon_url.starts_with("/images/") { + icon_url = format!("https://{}{}", blog_env.my_domain, icon_url); + } let mut rewriter = HtmlRewriter::new( Settings { element_content_handlers: vec![ diff --git a/src/page.rs b/src/page.rs index aebe30c..f836b8e 100644 --- a/src/page.rs +++ b/src/page.rs @@ -6,301 +6,301 @@ use std::collections::HashMap; #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct QueryBody { - requests: Vec, + requests: Vec, } impl QueryBody { - pub fn new(id: String) -> QueryBody { - QueryBody { - requests: vec![Requests { - pointer: Pointer { - table: "block".to_string(), - id, - }, - version: -1, - }], + pub fn new(id: String) -> QueryBody { + QueryBody { + requests: vec![Requests { + pointer: Pointer { + table: "block".to_string(), + id, + }, + version: -1, + }], + } } - } } #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Requests { - pointer: Pointer, - version: i32, + pointer: Pointer, + version: i32, } #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Pointer { - table: String, - id: String, + table: String, + id: String, } #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct QueryCollection { - record_map: RecordMap, + record_map: RecordMap, } impl QueryCollection { - pub fn get_sitemap(&self) -> String { - let mut sitemap = String::from("\n\n"); - for (_key, block) in self.record_map.block.iter() { - sitemap.push_str(&block.value.get_loc().unwrap_or_default()); + pub fn get_sitemap(&self) -> String { + let mut sitemap = String::from("\n\n"); + for (_key, block) in self.record_map.block.iter() { + sitemap.push_str(&block.value.get_loc().unwrap_or_default()); + } + sitemap.push_str(""); + sitemap } - sitemap.push_str(""); - sitemap - } - pub fn get_atom(&self, blog_env: &BlogEnv) -> String { - let mut atom = String::from("\n\n"); - atom.push_str(&format!("{}\n", blog_env.title)); - atom.push_str(&format!("{}\n", blog_env.description)); - atom.push_str(&format!( - "\n", - blog_env.my_domain - )); - atom.push_str(&format!( - "\n", - blog_env.my_domain - )); - atom.push_str(&format!("https://{}\n", blog_env.my_domain)); - atom.push_str(&format!( - "{}\n", - blog_env.description - )); - for (_key, block) in self.record_map.block.iter() { - atom.push_str(&block.value.get_atom().unwrap_or_default()); + pub fn get_atom(&self, blog_env: &BlogEnv) -> String { + let mut atom = String::from("\n\n"); + atom.push_str(&format!("{}\n", blog_env.title)); + atom.push_str(&format!("{}\n", blog_env.description)); + atom.push_str(&format!( + "\n", + blog_env.my_domain + )); + atom.push_str(&format!( + "\n", + blog_env.my_domain + )); + atom.push_str(&format!("https://{}\n", blog_env.my_domain)); + atom.push_str(&format!( + "{}\n", + blog_env.description + )); + for (_key, block) in self.record_map.block.iter() { + atom.push_str(&block.value.get_atom().unwrap_or_default()); + } + atom.push_str(""); + atom } - atom.push_str(""); - atom - } - pub fn get_title(&self, id: &uuid::Uuid) -> Option { - if let Some(block) = self.record_map.block.get(&id.to_string()) { - return block.value.get_title(); + pub fn get_title(&self, id: &uuid::Uuid) -> Option { + if let Some(block) = self.record_map.block.get(&id.to_string()) { + return block.value.get_title(); + } + None } - None - } - pub fn get_icon(&self, id: &uuid::Uuid) -> Option { - if let Some(block) = self.record_map.block.get(&id.to_string()) { - return block.value.get_icon(); + pub fn get_icon(&self, id: &uuid::Uuid) -> Option { + if let Some(block) = self.record_map.block.get(&id.to_string()) { + return block.value.get_icon(); + } + None } - None - } } #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct RecordMap { - block: HashMap, + block: HashMap, } #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Block { - value: BlockEnum, + value: BlockEnum, } impl BlockEnum { - fn get_loc(&self) -> Option { - match self { - BlockEnum::Page(p) => Some(p.to_loc()), - _ => None, + fn get_loc(&self) -> Option { + match self { + BlockEnum::Page(p) => Some(p.to_loc()), + _ => None, + } } - } - fn get_atom(&self) -> Option { - match self { - BlockEnum::Page(p) => Some(p.to_entry()), - _ => None, + fn get_atom(&self) -> Option { + match self { + BlockEnum::Page(p) => Some(p.to_entry()), + _ => None, + } } - } - fn get_title(&self) -> Option { - match self { - BlockEnum::Page(p) => Some(p.properties.title.get_title()), - _ => None, + fn get_title(&self) -> Option { + match self { + BlockEnum::Page(p) => Some(p.properties.title.get_title()), + _ => None, + } } - } - fn get_icon(&self) -> Option { - match self { - BlockEnum::Page(p) => { - if let Ok(u) = worker::Url::parse(&p.format.page_cover) { - Some(u.to_string()) - } else { - if p.format.page_cover.starts_with("/images/") { - return Some(p.format.page_cover.to_string()); - } - None + fn get_icon(&self) -> Option { + match self { + BlockEnum::Page(p) => { + if let Ok(u) = worker::Url::parse(&p.format.page_cover) { + Some(u.to_string()) + } else { + if p.format.page_cover.starts_with("/images/") { + return Some(p.format.page_cover.to_string()); + } + None + } + } + _ => None, } - } - _ => None, } - } } #[derive(Serialize, Deserialize, Debug)] #[serde(tag = "type", rename_all = "snake_case")] pub enum BlockEnum { - Page(Page), - ColumnList(ColumnList), - Column(Column), - CollectionView(CollectionView), - #[serde(other)] - Divider, + Page(Page), + ColumnList(ColumnList), + Column(Column), + CollectionView(CollectionView), + #[serde(other)] + Divider, } #[derive(Serialize, Deserialize, Debug)] pub struct CollectionView { - id: String, - version: i32, - view_ids: Vec, - #[serde(with = "date_format")] - created_time: DateTime, - #[serde(with = "date_format")] - last_edited_time: DateTime, + id: String, + version: i32, + view_ids: Vec, + #[serde(with = "date_format")] + created_time: DateTime, + #[serde(with = "date_format")] + last_edited_time: DateTime, } #[derive(Serialize, Deserialize, Debug)] pub struct Column { - id: String, - version: i32, - content: Vec, - #[serde(with = "date_format")] - created_time: DateTime, - #[serde(with = "date_format")] - last_edited_time: DateTime, + id: String, + version: i32, + content: Vec, + #[serde(with = "date_format")] + created_time: DateTime, + #[serde(with = "date_format")] + last_edited_time: DateTime, } #[derive(Serialize, Deserialize, Debug)] pub struct ColumnList { - id: String, - version: i32, - content: Vec, - #[serde(with = "date_format")] - created_time: DateTime, - #[serde(with = "date_format")] - last_edited_time: DateTime, + id: String, + version: i32, + content: Vec, + #[serde(with = "date_format")] + created_time: DateTime, + #[serde(with = "date_format")] + last_edited_time: DateTime, } #[derive(Serialize, Deserialize, Debug)] pub struct Page { - id: String, - version: i32, - properties: Properties, - #[serde(with = "date_format")] - created_time: DateTime, - #[serde(with = "date_format")] - last_edited_time: DateTime, - format: Format, + id: String, + version: i32, + properties: Properties, + #[serde(with = "date_format")] + created_time: DateTime, + #[serde(with = "date_format")] + last_edited_time: DateTime, + format: Format, } #[derive(Serialize, Deserialize, Debug)] pub struct Format { - #[serde(default)] - page_cover: String, + #[serde(default)] + page_cover: String, } impl Page { - fn to_loc(&self) -> String { - format!("\nhttps://MY_DOMAIN/{}\n\t{}\n\tdaily\n\t0.9\n\n", + fn to_loc(&self) -> String { + format!("\nhttps://MY_DOMAIN/{}\n\t{}\n\tdaily\n\t0.9\n\n", self.id.replace('-', ""), self.created_time.format("%Y-%m-%d"), ) - } - fn to_entry(&self) -> String { - let mut entry = String::from("\n\t"); - let page_id = self.id.replace('-', ""); - entry.push_str(&format!( - "{}\n\t", - self.properties.title.get_title() - )); - entry.push_str(&format!( - "\n\t", - page_id - )); - entry.push_str(&format!("https://MY_DOMAIN/{}\n\t", page_id)); - entry.push_str(&format!( - "{}\n\t", - self.created_time.to_rfc3339() - )); - entry.push_str(&format!( - "{}\n\t", - self.last_edited_time.to_rfc3339() - )); - entry.push_str(&format!( - "{}\n\t", - self.properties.title.get_title() - )); - entry.push_str(&format!( - "{}\n", - self.properties.title.get_title() - )); - entry.push_str("\n"); - entry - } + } + fn to_entry(&self) -> String { + let mut entry = String::from("\n\t"); + let page_id = self.id.replace('-', ""); + entry.push_str(&format!( + "{}\n\t", + self.properties.title.get_title() + )); + entry.push_str(&format!( + "\n\t", + page_id + )); + entry.push_str(&format!("https://MY_DOMAIN/{}\n\t", page_id)); + entry.push_str(&format!( + "{}\n\t", + self.created_time.to_rfc3339() + )); + entry.push_str(&format!( + "{}\n\t", + self.last_edited_time.to_rfc3339() + )); + entry.push_str(&format!( + "{}\n\t", + self.properties.title.get_title() + )); + entry.push_str(&format!( + "{}\n", + self.properties.title.get_title() + )); + entry.push_str("\n"); + entry + } } #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Properties { - title: Title, + title: Title, } #[derive(Serialize, Deserialize, Debug)] #[serde(untagged)] pub enum Title { - String(String), - Array(Vec), + String(String), + Array(Vec<Title>), } impl Title { - fn get_title(&self) -> String { - match self { - Title::String(t) => t.to_string(), - Title::Array(ts) => { - return ts - .iter() - .map(|t| t.get_title()) - .collect::<Vec<String>>() - .join(""); - } + fn get_title(&self) -> String { + match self { + Title::String(t) => t.to_string(), + Title::Array(ts) => { + return ts + .iter() + .map(|t| t.get_title()) + .collect::<Vec<String>>() + .join(""); + } + } } - } } mod date_format { - use chrono::{DateTime, FixedOffset, NaiveDateTime, Utc}; - use serde::{self, Deserialize, Deserializer, Serializer}; + use chrono::{DateTime, FixedOffset, NaiveDateTime, Utc}; + use serde::{self, Deserialize, Deserializer, Serializer}; - const FORMAT: &str = "%Y-%m-%d"; + const FORMAT: &str = "%Y-%m-%d"; - pub fn serialize<S>(date: &DateTime<FixedOffset>, serializer: S) -> Result<S::Ok, S::Error> + pub fn serialize<S>(date: &DateTime<FixedOffset>, serializer: S) -> Result<S::Ok, S::Error> where - S: Serializer, - { - let s = date.format(FORMAT).to_string(); - serializer.serialize_str(&s) - } + S: Serializer, + { + let s = date.format(FORMAT).to_string(); + serializer.serialize_str(&s) + } - pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<FixedOffset>, D::Error> + pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<FixedOffset>, D::Error> where - D: Deserializer<'de>, - { - let s = i64::deserialize(deserializer)?; - let tz_offset = FixedOffset::east_opt(8 * 60 * 60).unwrap(); - match NaiveDateTime::from_timestamp_millis(s) { - Some(t) => Ok(DateTime::from_utc(t, tz_offset)), - None => Ok(Utc::now().with_timezone(&tz_offset)), + D: Deserializer<'de>, + { + let s = i64::deserialize(deserializer)?; + let tz_offset = FixedOffset::east_opt(8 * 60 * 60).unwrap(); + match NaiveDateTime::from_timestamp_millis(s) { + Some(t) => Ok(DateTime::from_utc(t, tz_offset)), + None => Ok(Utc::now().with_timezone(&tz_offset)), + } } - } } #[cfg(test)] mod tests { - use crate::page::QueryCollection; + use crate::page::QueryCollection; - #[test] - fn test_notion_json() { - let j = r#" + #[test] + fn test_notion_json() { + let j = r#" { "recordMap": { "__version__": 3, @@ -427,9 +427,9 @@ mod tests { } } }"#; - let p: QueryCollection = serde_json::from_str(j).unwrap(); - for (_key, block) in p.record_map.block { - println!("{}", block.value.get_loc().unwrap_or_default()); + let p: QueryCollection = serde_json::from_str(j).unwrap(); + for (_key, block) in p.record_map.block { + println!("{}", block.value.get_loc().unwrap_or_default()); + } } - } }