Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Keats committed Apr 26, 2022
1 parent 9ab1bf2 commit c14b1fa
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ also specify classes on headers now
- Make `ignored_content` work with nested paths and directories
- `zola serve/build` can now run from anywhere in a zola directory
- Add XML support to `load_data`
- `skip_prefixes` is now checked before parsing external link URLs

## 0.15.3 (2022-01-23)

Expand Down
3 changes: 0 additions & 3 deletions components/content/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,3 @@ pub use pagination::Paginator;
pub use section::Section;
pub use taxonomies::{Taxonomy, TaxonomyItem};
pub use types::*;

// TODO
// 3. add more tests
37 changes: 1 addition & 36 deletions components/utils/src/site.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
use libs::percent_encoding::percent_decode;
use libs::unicode_segmentation::UnicodeSegmentation;
use std::collections::HashMap;
use std::hash::BuildHasher;

use errors::{anyhow, Result};

// TODO: move to content
/// Get word count and estimated reading time
pub fn get_reading_analytics(content: &str) -> (usize, usize) {
let word_count: usize = content.unicode_words().count();

// https://help.medium.com/hc/en-us/articles/214991667-Read-time
// 275 seems a bit too high though
(word_count, ((word_count + 199) / 200))
}

/// Result of a successful resolution of an internal link.
#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -58,7 +48,7 @@ pub fn resolve_internal_link<S: BuildHasher>(
mod tests {
use std::collections::HashMap;

use super::{get_reading_analytics, resolve_internal_link};
use super::{resolve_internal_link};

#[test]
fn can_resolve_valid_internal_link() {
Expand Down Expand Up @@ -104,29 +94,4 @@ mod tests {
let res = resolve_internal_link("@/pages/about.md#hello", &HashMap::new());
assert!(res.is_err());
}

#[test]
fn reading_analytics_empty_text() {
let (word_count, reading_time) = get_reading_analytics(" ");
assert_eq!(word_count, 0);
assert_eq!(reading_time, 0);
}

#[test]
fn reading_analytics_short_text() {
let (word_count, reading_time) = get_reading_analytics("Hello World");
assert_eq!(word_count, 2);
assert_eq!(reading_time, 1);
}

#[test]
fn reading_analytics_long_text() {
let mut content = String::new();
for _ in 0..1000 {
content.push_str(" Hello world");
}
let (word_count, reading_time) = get_reading_analytics(&content);
assert_eq!(word_count, 2000);
assert_eq!(reading_time, 10);
}
}

0 comments on commit c14b1fa

Please sign in to comment.