-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathutils.rs
212 lines (187 loc) · 6.97 KB
/
utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::path::{Path, PathBuf};
use libs::unicode_segmentation::UnicodeSegmentation;
use libs::walkdir::WalkDir;
use config::Config;
use utils::fs::is_temp_file;
use utils::table_of_contents::Heading;
pub fn has_anchor(headings: &[Heading], anchor: &str) -> bool {
for heading in headings {
if heading.id == anchor {
return true;
}
if has_anchor(&heading.children, anchor) {
return true;
}
}
false
}
/// Looks into the current folder for the path and see if there's anything that is not a .md
/// file. Those will be copied next to the rendered .html file
/// If `recursive` is set to `true`, it will add all subdirectories assets as well. This should
/// only be set when finding page assets currently.
/// TODO: remove this flag once sections with assets behave the same as pages with assets
pub fn find_related_assets(path: &Path, config: &Config, recursive: bool) -> Vec<PathBuf> {
let mut assets = vec![];
let mut builder = WalkDir::new(path).follow_links(true);
if !recursive {
builder = builder.max_depth(1);
}
for entry in builder.into_iter().filter_map(std::result::Result::ok) {
let entry_path = entry.path();
println!("Entry path: {}", entry_path.display());
if entry_path.is_file() && !is_temp_file(entry_path) {
match entry_path.extension() {
Some(e) => match e.to_str() {
Some("md") => continue,
_ => assets.push(entry_path.to_path_buf()),
},
None => assets.push(entry_path.to_path_buf()),
}
}
}
if let Some(ref globset) = config.ignored_content_globset {
assets.retain(|p| !globset.is_match(p));
}
assets
}
/// 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))
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::{create_dir, File};
use config::Config;
use tempfile::tempdir;
#[test]
fn can_find_related_assets_recursive() {
let tmp_dir = tempdir().expect("create temp dir");
let path = tmp_dir.path();
File::create(path.join("index.md")).unwrap();
File::create(path.join("example.js")).unwrap();
File::create(path.join("graph.jpg")).unwrap();
File::create(path.join("fail.png")).unwrap();
File::create(path.join("extensionless")).unwrap();
create_dir(path.join("subdir")).expect("create subdir temp dir");
File::create(path.join("subdir").join("index.md")).unwrap();
File::create(path.join("subdir").join("example.js")).unwrap();
let assets = find_related_assets(path, &Config::default(), true);
assert_eq!(assets.len(), 4);
assert_eq!(assets.iter().filter(|p| p.extension().unwrap_or_default() != "md").count(), 4);
for asset in ["example.js", "graph.jpg", "fail.png", "subdir/example.js"] {
assert!(assets.iter().any(|p| p.strip_prefix(path).unwrap() == Path::new(asset)))
}
}
#[test]
fn can_find_related_assets_non_recursive() {
let tmp_dir = tempdir().expect("create temp dir");
let path = tmp_dir.path();
File::create(path.join("index.md")).unwrap();
File::create(path.join("example.js")).unwrap();
File::create(path.join("graph.jpg")).unwrap();
File::create(path.join("fail.png")).unwrap();
File::create(path.join("extensionless")).unwrap();
create_dir(path.join("subdir")).expect("create subdir temp dir");
File::create(path.join("subdir").join("index.md")).unwrap();
File::create(path.join("subdir").join("example.js")).unwrap();
let assets = find_related_assets(path, &Config::default(), false);
assert_eq!(assets.len(), 3);
assert_eq!(assets.iter().filter(|p| p.extension().unwrap_or_default() != "md").count(), 3);
for asset in ["example.js", "graph.jpg", "fail.png"] {
assert!(assets.iter().any(|p| p.strip_prefix(path).unwrap() == Path::new(asset)))
}
}
#[test]
fn can_find_anchor_at_root() {
let input = vec![
Heading {
level: 1,
id: "1".to_string(),
permalink: String::new(),
title: String::new(),
children: vec![],
},
Heading {
level: 2,
id: "1-1".to_string(),
permalink: String::new(),
title: String::new(),
children: vec![],
},
Heading {
level: 3,
id: "1-1-1".to_string(),
permalink: String::new(),
title: String::new(),
children: vec![],
},
Heading {
level: 2,
id: "1-2".to_string(),
permalink: String::new(),
title: String::new(),
children: vec![],
},
];
assert!(has_anchor(&input, "1-2"));
}
#[test]
fn can_find_anchor_in_children() {
let input = vec![Heading {
level: 1,
id: "1".to_string(),
permalink: String::new(),
title: String::new(),
children: vec![
Heading {
level: 2,
id: "1-1".to_string(),
permalink: String::new(),
title: String::new(),
children: vec![],
},
Heading {
level: 3,
id: "1-1-1".to_string(),
permalink: String::new(),
title: String::new(),
children: vec![],
},
Heading {
level: 2,
id: "1-2".to_string(),
permalink: String::new(),
title: String::new(),
children: vec![],
},
],
}];
assert!(has_anchor(&input, "1-2"));
}
#[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);
}
}