finished inline markdown
This commit is contained in:
parent
139a70db51
commit
558c8ebe4c
@ -1,12 +1,13 @@
|
|||||||
import re
|
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):
|
def split_nodes_delimiter(old_nodes, delimiter, text_type):
|
||||||
new_nodes = []
|
new_nodes = []
|
||||||
for old_node in old_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)
|
new_nodes.append(old_node)
|
||||||
continue
|
continue
|
||||||
split_nodes = []
|
split_nodes = []
|
||||||
@ -17,7 +18,7 @@ def split_nodes_delimiter(old_nodes, delimiter, text_type):
|
|||||||
if sections[i] == "":
|
if sections[i] == "":
|
||||||
continue
|
continue
|
||||||
if i % 2 == 0:
|
if i % 2 == 0:
|
||||||
split_nodes.append(TextNode(sections[i], "text"))
|
split_nodes.append(TextNode(sections[i], text_type_text))
|
||||||
else:
|
else:
|
||||||
split_nodes.append(TextNode(sections[i], text_type))
|
split_nodes.append(TextNode(sections[i], text_type))
|
||||||
new_nodes.extend(split_nodes)
|
new_nodes.extend(split_nodes)
|
||||||
@ -28,7 +29,7 @@ def split_nodes_image(old_nodes):
|
|||||||
new_nodes = []
|
new_nodes = []
|
||||||
for old_node in old_nodes:
|
for old_node in old_nodes:
|
||||||
images = extract_markdown_images(old_node.text)
|
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)
|
new_nodes.append(old_node)
|
||||||
continue
|
continue
|
||||||
if len(old_node.text) == 0:
|
if len(old_node.text) == 0:
|
||||||
@ -50,9 +51,9 @@ def split_nodes_image(old_nodes):
|
|||||||
if sections[i] == "":
|
if sections[i] == "":
|
||||||
continue
|
continue
|
||||||
if isinstance(sections[i], str):
|
if isinstance(sections[i], str):
|
||||||
split_nodes.append(TextNode(sections[i], "text"))
|
split_nodes.append(TextNode(sections[i], text_type_text))
|
||||||
else:
|
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)
|
new_nodes.extend(split_nodes)
|
||||||
return new_nodes
|
return new_nodes
|
||||||
|
|
||||||
@ -60,7 +61,7 @@ def split_nodes_link(old_nodes):
|
|||||||
new_nodes = []
|
new_nodes = []
|
||||||
for old_node in old_nodes:
|
for old_node in old_nodes:
|
||||||
links = extract_markdown_links(old_node.text)
|
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)
|
new_nodes.append(old_node)
|
||||||
continue
|
continue
|
||||||
if len(old_node.text) == 0:
|
if len(old_node.text) == 0:
|
||||||
@ -82,9 +83,9 @@ def split_nodes_link(old_nodes):
|
|||||||
if sections[i] == "":
|
if sections[i] == "":
|
||||||
continue
|
continue
|
||||||
if isinstance(sections[i], str):
|
if isinstance(sections[i], str):
|
||||||
split_nodes.append(TextNode(sections[i], "text"))
|
split_nodes.append(TextNode(sections[i], text_type_text))
|
||||||
else:
|
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)
|
new_nodes.extend(split_nodes)
|
||||||
return new_nodes
|
return new_nodes
|
||||||
|
|
||||||
@ -96,11 +97,10 @@ def extract_markdown_images(text):
|
|||||||
def extract_markdown_links(text):
|
def extract_markdown_links(text):
|
||||||
return re.findall(r"\[(.*?)\]\((.*?)\)", text)
|
return re.findall(r"\[(.*?)\]\((.*?)\)", text)
|
||||||
|
|
||||||
|
def text_to_textnodes(text):
|
||||||
node_image_first = TextNode(
|
nodes = [TextNode(text, text_type_text)]
|
||||||
"![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!",
|
nodes = split_nodes_delimiter(nodes, "**", text_type_bold)
|
||||||
"text"
|
nodes = split_nodes_delimiter(nodes, "*", text_type_italic)
|
||||||
),
|
nodes = split_nodes_delimiter(nodes, "`", text_type_code)
|
||||||
|
nodes = split_nodes_image(nodes)
|
||||||
new_node = split_nodes_image(node_image_first)
|
nodes = split_nodes_link(nodes)
|
||||||
print(new_node)
|
|
@ -1,7 +1,7 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from inline_markdown import (extract_markdown_images, extract_markdown_links,
|
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,
|
from textnode import (TextNode, text_type_bold, text_type_code,
|
||||||
text_type_italic, text_type_text, text_type_image, text_type_link)
|
text_type_italic, text_type_text, text_type_image, text_type_link)
|
||||||
|
|
||||||
@ -169,6 +169,26 @@ class TestInlineMarkdown(unittest.TestCase):
|
|||||||
new_nodes_extra,
|
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):
|
def test_extract_markdown_images(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user