Browse Source

Only compute reading analytics once...

index-subcmd
Vincent Prouillet 6 years ago
parent
commit
69a9a352a0
2 changed files with 30 additions and 6 deletions
  1. +14
    -3
      components/content/src/page.rs
  2. +16
    -3
      components/content/src/section.rs

+ 14
- 3
components/content/src/page.rs View File

@@ -54,6 +54,11 @@ pub struct Page {
pub heavier: Option<Box<Page>>, pub heavier: Option<Box<Page>>,
/// Toc made from the headers of the markdown file /// Toc made from the headers of the markdown file
pub toc: Vec<Header>, pub toc: Vec<Header>,
/// How many words in the raw content
pub word_count: Option<usize>,
/// How long would it take to read the raw content.
/// See `get_reading_analytics` on how it is calculated
pub reading_time: Option<usize>,
} }




@@ -77,6 +82,8 @@ impl Page {
lighter: None, lighter: None,
heavier: None, heavier: None,
toc: vec![], toc: vec![],
word_count: None,
reading_time: None,
} }
} }


@@ -91,6 +98,9 @@ impl Page {
let (meta, content) = split_page_content(file_path, content)?; let (meta, content) = split_page_content(file_path, content)?;
let mut page = Page::new(file_path, meta); let mut page = Page::new(file_path, meta);
page.raw_content = content; 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 = { page.slug = {
if let Some(ref slug) = page.meta.slug { if let Some(ref slug) = page.meta.slug {
slug.trim().to_string() slug.trim().to_string()
@@ -240,6 +250,8 @@ impl Default for Page {
lighter: None, lighter: None,
heavier: None, heavier: None,
toc: vec![], 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("summary", &self.summary)?;
state.serialize_field("taxonomies", &self.meta.taxonomies)?; state.serialize_field("taxonomies", &self.meta.taxonomies)?;
state.serialize_field("extra", &self.meta.extra)?; 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("earlier", &self.earlier)?;
state.serialize_field("later", &self.later)?; state.serialize_field("later", &self.later)?;
state.serialize_field("lighter", &self.lighter)?; state.serialize_field("lighter", &self.lighter)?;


+ 16
- 3
components/content/src/section.rs View File

@@ -43,6 +43,11 @@ pub struct Section {
pub subsections: Vec<Section>, pub subsections: Vec<Section>,
/// Toc made from the headers of the markdown file /// Toc made from the headers of the markdown file
pub toc: Vec<Header>, pub toc: Vec<Header>,
/// How many words in the raw content
pub word_count: Option<usize>,
/// How long would it take to read the raw content.
/// See `get_reading_analytics` on how it is calculated
pub reading_time: Option<usize>,
} }


impl Section { impl Section {
@@ -62,6 +67,8 @@ impl Section {
ignored_pages: vec![], ignored_pages: vec![],
subsections: vec![], subsections: vec![],
toc: 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 (meta, content) = split_section_content(file_path, content)?;
let mut section = Section::new(file_path, meta); let mut section = Section::new(file_path, meta);
section.raw_content = content.clone(); section.raw_content = content.clone();
let (word_count, reading_time) = get_reading_analytics(&section.raw_content);
section.word_count = Some(word_count);
section.reading_time = Some(reading_time);
section.path = format!("{}/", section.file.components.join("/")); section.path = format!("{}/", section.file.components.join("/"));
section.components = section.path.split('/') section.components = section.path.split('/')
.map(|p| p.to_string()) .map(|p| p.to_string())
@@ -202,6 +212,8 @@ impl Section {
subsections, subsections,
pages: vec![], pages: vec![],
ignored_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("permalink", &self.permalink)?;
state.serialize_field("pages", &self.pages)?; state.serialize_field("pages", &self.pages)?;
state.serialize_field("subsections", &self.subsections)?; 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)?; state.serialize_field("toc", &self.toc)?;
let assets = self.serialize_assets(); let assets = self.serialize_assets();
state.serialize_field("assets", &assets)?; state.serialize_field("assets", &assets)?;
@@ -245,6 +256,8 @@ impl Default for Section {
ignored_pages: vec![], ignored_pages: vec![],
subsections: vec![], subsections: vec![],
toc: vec![], toc: vec![],
reading_time: None,
word_count: None,
} }
} }
} }


Loading…
Cancel
Save