Refactor custom functionality in helper function
This commit is contained in:
parent
7b99aee552
commit
4d3479291d
1 changed files with 21 additions and 29 deletions
|
@ -16,43 +16,35 @@ mod markdown {
|
|||
}
|
||||
|
||||
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>>()
|
||||
self.extract_tag(|tag| {
|
||||
match tag {
|
||||
Tag::Link(url, _) => Some(url.into_owned()),
|
||||
_ => None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn codeblocks(&self) -> Vec<String> {
|
||||
self.extract_tag(|tag| {
|
||||
match tag {
|
||||
Tag::CodeBlock(text) => Some(text.into_owned()),
|
||||
_ => None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn extract_tag<F>(&self, f: F) -> Vec<String>
|
||||
where F: FnMut(Tag) -> Option<String>
|
||||
{
|
||||
Parser::new(&self.text[..])
|
||||
.filter_map(|e| {
|
||||
match e {
|
||||
Event::Start(t) => Some(t),
|
||||
Event::End(t) => Some(t),
|
||||
_ => None
|
||||
Event::Start(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>>()
|
||||
.filter_map(f)
|
||||
.collect::<Vec<String>>()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue