From d690cb6871fe9520d62a6b8f6f628ab35ac61e08 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Tue, 2 Apr 2024 13:28:30 -0400 Subject: [PATCH] added extract functions --- src/inline_markdown.py | 10 ++++++++++ src/test_inline_markdown.py | 33 ++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/inline_markdown.py b/src/inline_markdown.py index afe208d..1da9d3c 100644 --- a/src/inline_markdown.py +++ b/src/inline_markdown.py @@ -1,3 +1,5 @@ +import re + from textnode import TextNode @@ -21,3 +23,11 @@ def split_nodes_delimiter(old_nodes, delimiter, text_type): final_node_list.extend(node_list) return final_node_list + + +def extract_markdown_images(text): + return re.findall(r"!\[(.*?)\]\((.*?)\)", text) + + +def extract_markdown_links(text): + return re.findall(r"\[(.*?)\]\((.*?)\)", text) diff --git a/src/test_inline_markdown.py b/src/test_inline_markdown.py index 55f48d5..52ec859 100644 --- a/src/test_inline_markdown.py +++ b/src/test_inline_markdown.py @@ -1,14 +1,9 @@ import unittest -from inline_markdown import split_nodes_delimiter - -from textnode import ( - TextNode, - text_type_bold, - text_type_code, - text_type_italic, - text_type_text, -) +from inline_markdown import (extract_markdown_images, extract_markdown_links, + split_nodes_delimiter) +from textnode import (TextNode, text_type_bold, text_type_code, + text_type_italic, text_type_text) class TestInlineMarkdown(unittest.TestCase): @@ -79,5 +74,25 @@ class TestInlineMarkdown(unittest.TestCase): ) +def test_extract_markdown_images(self): + matches = extract_markdown_images( + "This is text with an ![image](https://i.imgur.com/zjjcJKZ.png)" + ) + self.assertListEqual([("image", "https://i.imgur.com/zjjcJKZ.png")], matches) + + +def test_extract_markdown_links(self): + matches = extract_markdown_links( + "This is text with a [link](https://boot.dev) and [another link](https://blog.boot.dev)" + ) + self.assertListEqual( + [ + ("link", "https://boot.dev"), + ("another link", "https://blog.boot.dev"), + ], + matches, + ) + + if __name__ == "__main__": unittest.main()