diff --git a/src/module/helpers/content.rs b/src/module/helpers/content.rs index a082763b..6dde2c54 100644 --- a/src/module/helpers/content.rs +++ b/src/module/helpers/content.rs @@ -16,43 +16,35 @@ mod markdown { } pub fn links(&self) -> Vec { - 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::>() + self.extract_tag(|tag| { + match tag { + Tag::Link(url, _) => Some(url.into_owned()), + _ => None + } + }) } pub fn codeblocks(&self) -> Vec { + self.extract_tag(|tag| { + match tag { + Tag::CodeBlock(text) => Some(text.into_owned()), + _ => None + } + }) + } + + fn extract_tag(&self, f: F) -> Vec + where F: FnMut(Tag) -> Option + { 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::>() + .filter_map(f) + .collect::>() } }