Only compute reading analytics once...

This commit is contained in:
Vincent Prouillet 2018-09-20 18:27:56 +02:00
parent 6903975202
commit 69a9a352a0
2 changed files with 30 additions and 6 deletions

View file

@ -54,6 +54,11 @@ pub struct Page {
pub heavier: Option<Box<Page>>,
/// Toc made from the headers of the markdown file
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,
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)?;

View file

@ -43,6 +43,11 @@ pub struct Section {
pub subsections: Vec<Section>,
/// Toc made from the headers of the markdown file
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 {
@ -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(&section.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,
}
}
}