rust-analyzer/crates/ide/src/markdown_remove.rs

24 lines
644 B
Rust
Raw Normal View History

2020-10-05 17:52:24 +00:00
//! Removes markdown from strings.
2020-10-06 14:34:38 +00:00
use pulldown_cmark::{Event, Parser, Tag};
2020-10-05 17:52:24 +00:00
/// Removes all markdown, keeping the text and code blocks
///
/// Currently limited in styling, i.e. no ascii tables or lists
pub fn remove_markdown(markdown: &str) -> String {
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 14:34:38 +00:00
Event::SoftBreak | Event::HardBreak | Event::Rule | Event::End(Tag::CodeBlock(_)) => {
out.push('\n')
}
_ => {}
}
}
out
}