

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 can convert TextNodes to HTMLNodes, we need to be able to create TextNodes from raw markdown strings. For example, the string:
This is text with a **bolded phrase** in the middle
Should become:
[
TextNode("This is text with a ", TextType.TEXT),
TextNode("bolded phrase", TextType.BOLD),
TextNode(" in the middle", TextType.TEXT),
]
Markdown parsers often support nested inline elements. For example, you can have a bold word inside of italics:
This is an _italic and **bold** word_.
For simplicity's sake, we won't allow it! If you want to extend the project to support multiple levels of nested inline text types, you're welcome to do so at the end of the project.
Don't worry about links or images yet - they use different syntax and we'll handle them later.
Create a new function (I put this in a new code file, but you can organize your code as you please):
def split_nodes_delimiter(old_nodes: list[TextNode], delimiter: str, text_type: TextType) -> list[TextNode]:
It takes a list of "old nodes", a delimiter, and a text type. It should return a new list of nodes, where any "text" type nodes in the input list are (potentially) split into multiple nodes based on the syntax. For example, given the following input:
node = TextNode("This is text with a `code block` word", TextType.TEXT)
new_nodes = split_nodes_delimiter([node], "`", TextType.CODE)
new_nodes becomes:
[
TextNode("This is text with a ", TextType.TEXT),
TextNode("code block", TextType.CODE),
TextNode(" word", TextType.TEXT),
]
The beauty of this function is that it will take care of inline code, bold, and italic text using the same logic! The delimiter and matching text_type are the only thing that changes, e.g. ** for bold, _ for italic, and a backtick for code. Also, because it operates on an input list, we can call it multiple times to handle different types of delimiters. The order in which you check for different delimiters matters, which actually simplifies implementation.
TextType.TEXT type, just add it to the new list as-is, we only attempt to split "text" type objects (not bold, italic, etc).raise an exception with a helpful error message, that's invalid Markdown syntax.Run and submit the CLI tests from the root of the project.