From c44046e6ceebcd42e7eed9519e98431027920252 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Tue, 30 Apr 2024 11:58:24 -0400 Subject: [PATCH] fixed paragraph and heading --- src/inline_markdown.py | 3 ++- src/markdown_blocks.py | 28 ++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/inline_markdown.py b/src/inline_markdown.py index 94d0d13..22675ee 100644 --- a/src/inline_markdown.py +++ b/src/inline_markdown.py @@ -103,4 +103,5 @@ def text_to_textnodes(text): nodes = split_nodes_delimiter(nodes, "*", text_type_italic) nodes = split_nodes_delimiter(nodes, "`", text_type_code) nodes = split_nodes_image(nodes) - nodes = split_nodes_link(nodes) \ No newline at end of file + nodes = split_nodes_link(nodes) + return nodes \ No newline at end of file diff --git a/src/markdown_blocks.py b/src/markdown_blocks.py index 7601119..c3beca4 100644 --- a/src/markdown_blocks.py +++ b/src/markdown_blocks.py @@ -1,6 +1,8 @@ import re from htmlnode import LeafNode, ParentNode +from inline_markdown import text_to_textnodes +from textnode import text_node_to_html_node block_type_paragraph = "paragraph" block_type_heading = "heading" @@ -60,14 +62,28 @@ def block_to_block_type(block): else: return block_type_paragraph +def text_to_children(text): + text_nodes = text_to_textnodes(text) + children = [] + for text_node in text_nodes: + html_node = text_node_to_html_node(text_node) + children.append(html_node) + return children + def paragraph_to_html_node(block): - return LeafNode("p", block) + lines = block.split("\n") + paragraph = " ".join(lines) + children = text_to_children(paragraph) + return ParentNode("p", children) def heading_to_html_node(block): regexed_block = re.search("(^#{1,6}) (.*)", block) - count = len(regexed_block.groups(1)) - data = regexed_block.groups(2) - return LeafNode(f"h{count}", data) + if regexed_block == None: + raise ValueError("Invalid heading level") + count = len(regexed_block.groups()[0]) + data = regexed_block.groups()[1] + data = text_to_children(data) + return ParentNode(f"h{count}", data) def code_to_html_node(block): code_data = re.search("^```(.*)```$", block).groups(1)[0] @@ -125,5 +141,5 @@ def markdown_to_html_node(markdown): return ParentNode("div", nodes) -quote_block = "> quote first line\n> quote second line" -print(quote_to_html_node(quote_block)) \ No newline at end of file +quote_block = "### This is a h3 heading." +print(heading_to_html_node(quote_block)) \ No newline at end of file