From 558c8ebe4c1782f2864f68c99aa1e2aff11ff7e6 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Sat, 20 Apr 2024 12:03:04 -0400 Subject: [PATCH] finished inline markdown --- src/inline_markdown.py | 34 +++++++++++++++++----------------- src/test_inline_markdown.py | 22 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/inline_markdown.py b/src/inline_markdown.py index ba10d0a..94d0d13 100644 --- a/src/inline_markdown.py +++ b/src/inline_markdown.py @@ -1,12 +1,13 @@ import re -from textnode import TextNode +from textnode import (TextNode, text_type_bold, text_type_code, + text_type_italic, text_type_text, text_type_image, text_type_link) def split_nodes_delimiter(old_nodes, delimiter, text_type): new_nodes = [] for old_node in old_nodes: - if old_node.text_type != "text": + if old_node.text_type != text_type_text: new_nodes.append(old_node) continue split_nodes = [] @@ -17,7 +18,7 @@ def split_nodes_delimiter(old_nodes, delimiter, text_type): if sections[i] == "": continue if i % 2 == 0: - split_nodes.append(TextNode(sections[i], "text")) + split_nodes.append(TextNode(sections[i], text_type_text)) else: split_nodes.append(TextNode(sections[i], text_type)) new_nodes.extend(split_nodes) @@ -28,7 +29,7 @@ def split_nodes_image(old_nodes): new_nodes = [] for old_node in old_nodes: images = extract_markdown_images(old_node.text) - if old_node.text_type != "text" or len(images) == 0: + if old_node.text_type != text_type_text or len(images) == 0: new_nodes.append(old_node) continue if len(old_node.text) == 0: @@ -50,9 +51,9 @@ def split_nodes_image(old_nodes): if sections[i] == "": continue if isinstance(sections[i], str): - split_nodes.append(TextNode(sections[i], "text")) + split_nodes.append(TextNode(sections[i], text_type_text)) else: - split_nodes.append(TextNode(sections[i][0], "image", sections[i][1])) + split_nodes.append(TextNode(sections[i][0], text_type_image, sections[i][1])) new_nodes.extend(split_nodes) return new_nodes @@ -60,7 +61,7 @@ def split_nodes_link(old_nodes): new_nodes = [] for old_node in old_nodes: links = extract_markdown_links(old_node.text) - if old_node.text_type != "text" or len(links) == 0: + if old_node.text_type != text_type_text or len(links) == 0: new_nodes.append(old_node) continue if len(old_node.text) == 0: @@ -82,9 +83,9 @@ def split_nodes_link(old_nodes): if sections[i] == "": continue if isinstance(sections[i], str): - split_nodes.append(TextNode(sections[i], "text")) + split_nodes.append(TextNode(sections[i], text_type_text)) else: - split_nodes.append(TextNode(sections[i][0], "link", sections[i][1])) + split_nodes.append(TextNode(sections[i][0], text_type_link, sections[i][1])) new_nodes.extend(split_nodes) return new_nodes @@ -96,11 +97,10 @@ def extract_markdown_images(text): def extract_markdown_links(text): return re.findall(r"\[(.*?)\]\((.*?)\)", text) - -node_image_first = TextNode( - "![image](https://storage.googleapis.com/qvault-webapp-dynamic-assets/course_assets/zjjcJKZ.png) and another ![second image](https://storage.googleapis.com/qvault-webapp-dynamic-assets/course_assets/3elNhQu.png). Isn't this awesome!", - "text" - ), - -new_node = split_nodes_image(node_image_first) -print(new_node) \ No newline at end of file +def text_to_textnodes(text): + nodes = [TextNode(text, text_type_text)] + nodes = split_nodes_delimiter(nodes, "**", text_type_bold) + nodes = split_nodes_delimiter(nodes, "*", text_type_italic) + nodes = split_nodes_delimiter(nodes, "`", text_type_code) + nodes = split_nodes_image(nodes) + nodes = split_nodes_link(nodes) \ No newline at end of file diff --git a/src/test_inline_markdown.py b/src/test_inline_markdown.py index 2125234..26c0479 100644 --- a/src/test_inline_markdown.py +++ b/src/test_inline_markdown.py @@ -1,7 +1,7 @@ import unittest from inline_markdown import (extract_markdown_images, extract_markdown_links, - split_nodes_delimiter, split_nodes_image, split_nodes_link) + split_nodes_delimiter, split_nodes_image, split_nodes_link, text_to_textnodes) from textnode import (TextNode, text_type_bold, text_type_code, text_type_italic, text_type_text, text_type_image, text_type_link) @@ -169,6 +169,26 @@ class TestInlineMarkdown(unittest.TestCase): new_nodes_extra, ) + def test_text_to_textnodes(self): + nodes = text_to_textnodes( + "This is **text** with an *italic* word and a `code block` and an ![image](https://i.imgur.com/zjjcJKZ.png) and a [link](https://boot.dev)" + ) + self.assertListEqual( + [ + TextNode("This is ", text_type_text), + TextNode("text", text_type_bold), + TextNode(" with an ", text_type_text), + TextNode("italic", text_type_italic), + TextNode(" word and a ", text_type_text), + TextNode("code block", text_type_code), + TextNode(" and an ", text_type_text), + TextNode("image", text_type_image, "https://i.imgur.com/zjjcJKZ.png"), + TextNode(" and a ", text_type_text), + TextNode("link", text_type_link, "https://boot.dev"), + ], + nodes, + ) + def test_extract_markdown_images(self):