2020-10-05 12:52:24 -05:00
|
|
|
//! Removes markdown from strings.
|
2020-10-06 09:34:38 -05:00
|
|
|
use pulldown_cmark::{Event, Parser, Tag};
|
2020-10-05 12:27:29 -05:00
|
|
|
|
2020-10-05 12:52:24 -05:00
|
|
|
/// Removes all markdown, keeping the text and code blocks
|
2020-10-06 09:34:09 -05:00
|
|
|
///
|
|
|
|
/// Currently limited in styling, i.e. no ascii tables or lists
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn remove_markdown(markdown: &str) -> String {
|
2020-10-05 12:27:29 -05:00
|
|
|
let mut out = String::new();
|
|
|
|
let parser = Parser::new(markdown);
|
|
|
|
|
|
|
|
for event in parser {
|
|
|
|
match event {
|
|
|
|
Event::Text(text) | Event::Code(text) => out.push_str(&text),
|
2020-10-06 09:34:38 -05:00
|
|
|
Event::SoftBreak | Event::HardBreak | Event::Rule | Event::End(Tag::CodeBlock(_)) => {
|
|
|
|
out.push('\n')
|
|
|
|
}
|
2020-10-05 12:27:29 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|