BlueGit

ict rusty-teams

Files

rusty-teams / crates / store / src / text.rs

text.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
text.rs 477 B Download Raw
1use scraper::Html;
2
3pub fn plain_text(html: &str) -> String {
4 Html::parse_fragment(html)
5 .root_element()
6 .text()
7 .flat_map(str::split_whitespace)
8 .collect::<Vec<_>>()
9 .join(" ")
10}
11
12#[cfg(test)]
13mod tests {
14 use super::*;
15
16 #[test]
17 fn strips_tags_and_collapses_whitespace() {
18 assert_eq!(
19 plain_text("<p>Hello <b>big</b></p>\n<p>world &amp; co</p>"),
20 "Hello big world & co"
21 );
22 }
23}