| 1 | use scraper::Html; |
| 2 | |
| 3 | pub 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)] |
| 13 | mod 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 & co</p>"), |
| 20 | "Hello big world & co" |
| 21 | ); |
| 22 | } |
| 23 | } |