From f7ad92f4bf77140a71d8abed1a8bdf4a4c175851 Mon Sep 17 00:00:00 2001 From: Edward Loveall Date: Sat, 16 Oct 2021 16:19:44 -0400 Subject: [PATCH] Parsing Fix: Add H2 Paragraph type The post id 34dead42a28 contained a new paragraph type: H2. Previously the only known header types were H3 and H4. In this case, the paragraph doesn't actually get rendered because it's the page title which is removed from the page nodes (see commits 6baba803 and then fba87c10). However, it somehow an author is able to get an H2 paragraph into the page, it will display as an

just as H3 displays as

and H4 displays as

. --- spec/classes/paragraph_converter_spec.cr | 9 +++++++++ spec/components/page_content_spec.cr | 17 +++++++++++++++++ src/classes/paragraph_converter.cr | 4 ++++ src/components/page_content.cr | 4 ++++ src/models/nodes.cr | 3 +++ src/models/post_response.cr | 1 + 6 files changed, 38 insertions(+) diff --git a/spec/classes/paragraph_converter_spec.cr b/spec/classes/paragraph_converter_spec.cr index bac37f5..c3e30cb 100644 --- a/spec/classes/paragraph_converter_spec.cr +++ b/spec/classes/paragraph_converter_spec.cr @@ -190,6 +190,14 @@ describe ParagraphConverter do it "converts all the tags" do paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON [ + { + "text": "text", + "type": "H2", + "markups": [], + "iframe": null, + "layout": null, + "metadata": null + }, { "text": "text", "type": "H3", @@ -303,6 +311,7 @@ describe ParagraphConverter do ] JSON expected = [ + Heading1.new([Text.new("text")] of Child), Heading2.new([Text.new("text")] of Child), Heading3.new([Text.new("text")] of Child), Paragraph.new([Text.new("text")] of Child), diff --git a/spec/components/page_content_spec.cr b/spec/components/page_content_spec.cr index 50520a6..7b40351 100644 --- a/spec/components/page_content_spec.cr +++ b/spec/components/page_content_spec.cr @@ -162,6 +162,23 @@ describe PageContent do HTML end + it "renders an H2" do + page = Page.new( + title: "Title", + author: user_anchor_factory, + created_at: Time.local, + nodes: [ + Heading1.new(children: [ + Text.new(content: "Title!"), + ] of Child), + ] of Child + ) + + html = PageContent.new(page: page).render_to_string + + html.should eq %(

Title!

) + end + it "renders an H3" do page = Page.new( title: "Title", diff --git a/src/classes/paragraph_converter.cr b/src/classes/paragraph_converter.cr index 6070cf6..77ab31c 100644 --- a/src/classes/paragraph_converter.cr +++ b/src/classes/paragraph_converter.cr @@ -10,6 +10,10 @@ class ParagraphConverter paragraph = paragraphs.shift children = MarkupConverter.convert(paragraph.text, paragraph.markups) node = BlockQuote.new(children: children) + when PostResponse::ParagraphType::H2 + paragraph = paragraphs.shift + children = MarkupConverter.convert(paragraph.text, paragraph.markups) + node = Heading1.new(children: children) when PostResponse::ParagraphType::H3 paragraph = paragraphs.shift children = MarkupConverter.convert(paragraph.text, paragraph.markups) diff --git a/src/components/page_content.cr b/src/components/page_content.cr index 90a5be8..7ffb41f 100644 --- a/src/components/page_content.cr +++ b/src/components/page_content.cr @@ -81,6 +81,10 @@ class PageContent < BaseComponent script src: child.src end + def render_child(node : Heading1) + h1 { render_children(node.children) } + end + def render_child(node : Heading2) h2 { render_children(node.children) } end diff --git a/src/models/nodes.cr b/src/models/nodes.cr index 9518225..fec0a30 100644 --- a/src/models/nodes.cr +++ b/src/models/nodes.cr @@ -40,6 +40,9 @@ module Nodes class FigureCaption < Container end + class Heading1 < Container + end + class Heading2 < Container end diff --git a/src/models/post_response.cr b/src/models/post_response.cr index 28c6768..99a0931 100644 --- a/src/models/post_response.cr +++ b/src/models/post_response.cr @@ -42,6 +42,7 @@ class PostResponse enum ParagraphType BQ + H2 H3 H4 IFRAME