finished inline markdown

This commit is contained in:
John O'Keefe 2024-04-20 12:03:04 -04:00
parent 139a70db51
commit 558c8ebe4c
2 changed files with 38 additions and 18 deletions

View File

@ -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)
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)

View File

@ -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):