Rewrite markdown parsing, so we can extract links and quotes (and maybe more)
This commit is contained in:
parent
c35cd7ef15
commit
7b99aee552
1 changed files with 55 additions and 20 deletions
|
@ -1,25 +1,60 @@
|
|||
|
||||
pub fn extract_links(s: &String) -> Vec<String> {
|
||||
mod markdown {
|
||||
use pulldown_cmark::Parser;
|
||||
use pulldown_cmark::Event;
|
||||
use pulldown_cmark::Tag;
|
||||
|
||||
Parser::new(&s[..])
|
||||
.filter_map(|e| {
|
||||
match e {
|
||||
Event::Start(t) => Some(t),
|
||||
Event::End(t) => Some(t),
|
||||
_ => None
|
||||
}
|
||||
})
|
||||
.filter_map(|tag| {
|
||||
match tag {
|
||||
Tag::Link(url, text) => Some((url, text)),
|
||||
_ => None
|
||||
}
|
||||
})
|
||||
.map(|(url, text)| {
|
||||
text.into_owned()
|
||||
}).collect::<Vec<String>>()
|
||||
}
|
||||
pub struct MarkdownParser<'a> {
|
||||
text: &'a String
|
||||
}
|
||||
|
||||
impl<'a> MarkdownParser<'a> {
|
||||
|
||||
pub fn new(s: &'a String) -> MarkdownParser {
|
||||
MarkdownParser {
|
||||
text: s
|
||||
}
|
||||
}
|
||||
|
||||
pub fn links(&self) -> Vec<String> {
|
||||
Parser::new(&self.text[..])
|
||||
.filter_map(|e| {
|
||||
match e {
|
||||
Event::Start(t) => Some(t),
|
||||
Event::End(t) => Some(t),
|
||||
_ => None
|
||||
}
|
||||
})
|
||||
.filter_map(|tag| {
|
||||
match tag {
|
||||
Tag::Link(url, text) => Some((url, text)),
|
||||
_ => None
|
||||
}
|
||||
})
|
||||
.map(|(url, text)| {
|
||||
text.into_owned()
|
||||
}).collect::<Vec<String>>()
|
||||
}
|
||||
|
||||
pub fn codeblocks(&self) -> Vec<String> {
|
||||
Parser::new(&self.text[..])
|
||||
.filter_map(|e| {
|
||||
match e {
|
||||
Event::Start(t) => Some(t),
|
||||
Event::End(t) => Some(t),
|
||||
_ => None
|
||||
}
|
||||
})
|
||||
.filter_map(|tag| {
|
||||
match tag {
|
||||
Tag::CodeBlock(text) => Some(text),
|
||||
_ => None
|
||||
}
|
||||
})
|
||||
.map(|text| {
|
||||
text.into_owned()
|
||||
}).collect::<Vec<String>>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue