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.
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, delimiter, text_type):
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, all in one! The logic is identical, 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.