

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Split Delimiter
incomplete
2: Regex
incomplete
3: Extract Links
incomplete
4: Split Images and Links
incomplete
5: Text to TextNodes
incomplete
This lesson's interactive features are locked, please to keep using them
Now that we have the extraction functions, we will need to be able to split raw markdown text into TextNodes based on images and links.
def split_nodes_image(old_nodes: list[TextNode]) -> list[TextNode]:def split_nodes_link(old_nodes: list[TextNode]) -> list[TextNode]:They should behave very similarly to split_nodes_delimiter, but obviously don't need a delimiter or a text type as input, because they always operate on images or links respectively. Here's some example usage:
node = TextNode(
"This is text with a link [to boot dev](https://www.boot.dev) and [to youtube](https://www.youtube.com/@bootdotdev)",
TextType.TEXT,
)
new_nodes = split_nodes_link([node])
# [
# TextNode("This is text with a link ", TextType.TEXT),
# TextNode("to boot dev", TextType.LINK, "https://www.boot.dev"),
# TextNode(" and ", TextType.TEXT),
# TextNode(
# "to youtube", TextType.LINK, "https://www.youtube.com/@bootdotdev"
# ),
# ]
def test_split_images(self):
node = TextNode(
"This is text with an  and another ",
TextType.TEXT,
)
new_nodes = split_nodes_image([node])
self.assertListEqual(
[
TextNode("This is text with an ", TextType.TEXT),
TextNode("image", TextType.IMAGE, "https://i.imgur.com/zjjcJKZ.png"),
TextNode(" and another ", TextType.TEXT),
TextNode("second image", TextType.IMAGE, "https://i.imgur.com/3elNhQu.png"),
],
new_nodes,
)
Run and submit the CLI tests from the root of the project.
Here are some spoilers that might help you out:
TextNode in itTextNodes that have empty text to the final listsplit_nodes_image and split_nodes_link functions will be very similar. You can try to share code between them if you want, but I was a copy/paste grug dev for this step.1 if you only want to split the string once at most. For each image extracted from the text, I split the text before and after the image markdown. For example:sections = original_text.split(f"", 1)