We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Split Delimiter

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),
]

We Don't Care About Nested Inline Elements

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.

Assignment

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.

    • If an "old node" is not a TextType.TEXT type, just add it to the new list as-is, we only attempt to split "text" type objects (not bold, italic, etc).
    • If a matching closing delimiter is not found, just raise an exception with a helpful error message, that's invalid Markdown syntax.
    • The .split() string method was useful
    • The .extend() list method was useful

Run and submit the CLI tests from the root of the project.