mirror of
https://github.com/getzola/zola
synced 2024-12-05 01:49:12 +00:00
Add config option to ignore code blocks for word count (#2599)
* update docs * add config option, logic, and tests * remove config option
This commit is contained in:
parent
9dad659665
commit
abc851049d
1 changed files with 18 additions and 1 deletions
|
@ -60,7 +60,10 @@ pub fn find_related_assets(path: &Path, config: &Config, recursive: bool) -> Vec
|
|||
|
||||
/// Get word count and estimated reading time
|
||||
pub fn get_reading_analytics(content: &str) -> (usize, usize) {
|
||||
let word_count: usize = content.unicode_words().count();
|
||||
// code fences "toggle" the state from non-code to code and back, so anything inbetween the
|
||||
// first fence and the next can be ignored
|
||||
let split = content.split("```");
|
||||
let word_count = split.step_by(2).map(|section| section.unicode_words().count()).sum();
|
||||
|
||||
// https://help.medium.com/hc/en-us/articles/214991667-Read-time
|
||||
// 275 seems a bit too high though
|
||||
|
@ -241,4 +244,18 @@ mod tests {
|
|||
assert_eq!(word_count, 2000);
|
||||
assert_eq!(reading_time, 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reading_analytics_no_code() {
|
||||
let (word_count, reading_time) =
|
||||
get_reading_analytics("hello world ``` code goes here ``` goodbye world");
|
||||
assert_eq!(word_count, 4);
|
||||
assert_eq!(reading_time, 1);
|
||||
|
||||
let (word_count, reading_time) = get_reading_analytics(
|
||||
"hello world ``` code goes here ``` goodbye world ``` dangling fence",
|
||||
);
|
||||
assert_eq!(word_count, 4);
|
||||
assert_eq!(reading_time, 1);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue