From 69a9a352a041a8e0f190e69ae916d15e35113bb8 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 20 Sep 2018 18:27:56 +0200 Subject: [PATCH] Only compute reading analytics once... --- components/content/src/page.rs | 17 ++++++++++++++--- components/content/src/section.rs | 19 ++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/components/content/src/page.rs b/components/content/src/page.rs index 7c6554dd..9be00d53 100644 --- a/components/content/src/page.rs +++ b/components/content/src/page.rs @@ -54,6 +54,11 @@ pub struct Page { pub heavier: Option>, /// Toc made from the headers of the markdown file pub toc: Vec
, + /// How many words in the raw content + pub word_count: Option, + /// How long would it take to read the raw content. + /// See `get_reading_analytics` on how it is calculated + pub reading_time: Option, } @@ -77,6 +82,8 @@ impl Page { lighter: None, heavier: None, toc: vec![], + word_count: None, + reading_time: None, } } @@ -91,6 +98,9 @@ impl Page { let (meta, content) = split_page_content(file_path, content)?; let mut page = Page::new(file_path, meta); page.raw_content = content; + let (word_count, reading_time) = get_reading_analytics(&page.raw_content); + page.word_count = Some(word_count); + page.reading_time = Some(reading_time); page.slug = { if let Some(ref slug) = page.meta.slug { slug.trim().to_string() @@ -240,6 +250,8 @@ impl Default for Page { lighter: None, heavier: None, toc: vec![], + word_count: None, + reading_time: None, } } } @@ -268,9 +280,8 @@ impl ser::Serialize for Page { state.serialize_field("summary", &self.summary)?; state.serialize_field("taxonomies", &self.meta.taxonomies)?; state.serialize_field("extra", &self.meta.extra)?; - let (word_count, reading_time) = get_reading_analytics(&self.raw_content); - state.serialize_field("word_count", &word_count)?; - state.serialize_field("reading_time", &reading_time)?; + state.serialize_field("word_count", &self.word_count)?; + state.serialize_field("reading_time", &self.reading_time)?; state.serialize_field("earlier", &self.earlier)?; state.serialize_field("later", &self.later)?; state.serialize_field("lighter", &self.lighter)?; diff --git a/components/content/src/section.rs b/components/content/src/section.rs index 0cdc70e5..e5e5825c 100644 --- a/components/content/src/section.rs +++ b/components/content/src/section.rs @@ -43,6 +43,11 @@ pub struct Section { pub subsections: Vec
, /// Toc made from the headers of the markdown file pub toc: Vec
, + /// How many words in the raw content + pub word_count: Option, + /// How long would it take to read the raw content. + /// See `get_reading_analytics` on how it is calculated + pub reading_time: Option, } impl Section { @@ -62,6 +67,8 @@ impl Section { ignored_pages: vec![], subsections: vec![], toc: vec![], + word_count: None, + reading_time: None, } } @@ -69,6 +76,9 @@ impl Section { let (meta, content) = split_section_content(file_path, content)?; let mut section = Section::new(file_path, meta); section.raw_content = content.clone(); + let (word_count, reading_time) = get_reading_analytics(§ion.raw_content); + section.word_count = Some(word_count); + section.reading_time = Some(reading_time); section.path = format!("{}/", section.file.components.join("/")); section.components = section.path.split('/') .map(|p| p.to_string()) @@ -202,6 +212,8 @@ impl Section { subsections, pages: vec![], ignored_pages: vec![], + word_count: self.word_count.clone(), + reading_time: self.reading_time.clone(), } } } @@ -219,9 +231,8 @@ impl ser::Serialize for Section { state.serialize_field("permalink", &self.permalink)?; state.serialize_field("pages", &self.pages)?; state.serialize_field("subsections", &self.subsections)?; - let (word_count, reading_time) = get_reading_analytics(&self.raw_content); - state.serialize_field("word_count", &word_count)?; - state.serialize_field("reading_time", &reading_time)?; + state.serialize_field("word_count", &self.word_count)?; + state.serialize_field("reading_time", &self.reading_time)?; state.serialize_field("toc", &self.toc)?; let assets = self.serialize_assets(); state.serialize_field("assets", &assets)?; @@ -245,6 +256,8 @@ impl Default for Section { ignored_pages: vec![], subsections: vec![], toc: vec![], + reading_time: None, + word_count: None, } } }