diff --git a/spec/classes/page_converter_spec.cr b/spec/classes/page_converter_spec.cr index 30b0a3b..39f89e0 100644 --- a/spec/classes/page_converter_spec.cr +++ b/spec/classes/page_converter_spec.cr @@ -3,56 +3,26 @@ require "../spec_helper" include Nodes describe PageConverter do - it "sets the title and subtitle if present" do + it "sets the page title" do + title = "Hello, world!" paragraph_json = <<-JSON [ { - "text": "Title", + "text": "#{title}", "type": "H3", "markups": [], "iframe": null, "layout": null, "metadata": null - }, - { - "text": "Subtitle", - "type": "H4", - "markups": [], - "iframe": null, - "layout": null, - "metadata": null } ] JSON - data_json = default_data_json(paragraph_json) + data_json = default_data_json(title, paragraph_json) data = PostResponse::Data.from_json(data_json) page = PageConverter.new.convert(data) - page.title.should eq "Title" - page.subtitle.should eq "Subtitle" - end - - it "sets the title to the first paragraph if no title" do - paragraph_json = <<-JSON - [ - { - "text": "Not a title", - "type": "P", - "markups": [], - "iframe": null, - "layout": null, - "metadata": null - } - ] - JSON - data_json = default_data_json(paragraph_json) - data = PostResponse::Data.from_json(data_json) - - page = PageConverter.new.convert(data) - - page.title.should eq "Not a title" - page.subtitle.should eq nil + page.title.should eq title end it "sets the author" do @@ -106,25 +76,18 @@ describe PageConverter do page.created_at.should eq Time.utc(1970, 1, 1, 0, 0, 1) end - it "calls ParagraphConverter to convert the remaining paragraph content" do + it "calls converts the remaining paragraph content" do + title = "Title" paragraph_json = <<-JSON [ { - "text": "Title", + "text": "#{title}", "type": "H3", "markups": [], "iframe": null, "layout": null, "metadata": null }, - { - "text": "Subtitle", - "type": "H4", - "markups": [], - "iframe": null, - "layout": null, - "metadata": null - }, { "text": "Content", "type": "P", @@ -135,7 +98,7 @@ describe PageConverter do } ] JSON - data_json = default_data_json(paragraph_json) + data_json = default_data_json(title, paragraph_json) data = PostResponse::Data.from_json(data_json) page = PageConverter.new.convert(data) @@ -148,11 +111,18 @@ describe PageConverter do end end -def default_data_json(paragraph_json : String) +def default_paragraph_json + "[]" +end + +def default_data_json( + title : String = "This is a story", + paragraph_json : String = default_paragraph_json +) <<-JSON { "post": { - "title": "This is a story", + "title": "#{title}", "createdAt": 1628974309758, "creator": { "id": "abc123", diff --git a/spec/components/page_content_spec.cr b/spec/components/page_content_spec.cr index 72727e2..50520a6 100644 --- a/spec/components/page_content_spec.cr +++ b/spec/components/page_content_spec.cr @@ -6,7 +6,6 @@ describe PageContent do it "renders a single parent/child node structure" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -24,7 +23,6 @@ describe PageContent do it "renders multiple childrens" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -53,7 +51,6 @@ describe PageContent do it "renders an anchor" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -69,7 +66,6 @@ describe PageContent do it "renders a blockquote" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -87,7 +83,6 @@ describe PageContent do it "renders code" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -105,7 +100,6 @@ describe PageContent do it "renders empasis" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -127,7 +121,6 @@ describe PageContent do children = [Text.new("A caption")] of Child page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -155,7 +148,6 @@ describe PageContent do it "renders a GitHub Gist" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -173,7 +165,6 @@ describe PageContent do it "renders an H3" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -191,7 +182,6 @@ describe PageContent do it "renders an H4" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -209,7 +199,6 @@ describe PageContent do it "renders an image" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -231,7 +220,6 @@ describe PageContent do it "renders embedded content" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -256,7 +244,6 @@ describe PageContent do it "renders an embedded link container" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -280,7 +267,6 @@ describe PageContent do it "renders an mixtape embed container" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -311,7 +297,6 @@ describe PageContent do it "renders an ordered list" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -330,7 +315,6 @@ describe PageContent do it "renders an preformatted text" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -348,7 +332,6 @@ describe PageContent do it "renders an preformatted text" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -366,7 +349,6 @@ describe PageContent do it "renders strong text" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -384,7 +366,6 @@ describe PageContent do it "renders an unordered list" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ @@ -403,7 +384,6 @@ describe PageContent do it "renders a user anchor" do page = Page.new( title: "Title", - subtitle: nil, author: user_anchor_factory, created_at: Time.local, nodes: [ diff --git a/src/classes/page_converter.cr b/src/classes/page_converter.cr index b4ee7d4..70f932f 100644 --- a/src/classes/page_converter.cr +++ b/src/classes/page_converter.cr @@ -1,57 +1,20 @@ -struct HeaderData - getter title : String - getter subtitle : String? - - def initialize(@title : String, @subtitle : String?) - end - - def first_content_paragraph_index : Int32 - if title.blank? - 0 - elsif subtitle.nil? || subtitle.blank? - 1 - else - 2 - end - end -end - class PageConverter def convert(data : PostResponse::Data) : Page - paragraphs = data.post.content.bodyModel.paragraphs + title, content = title_and_content(data) author = data.post.creator created_at = Time.unix_ms(data.post.createdAt) - header = header_data(paragraphs) - if header.first_content_paragraph_index.zero? - content = [] of PostResponse::Paragraph - else - content = paragraphs[header.first_content_paragraph_index..] - end Page.new( - title: header.title, - subtitle: header.subtitle, + title: title, author: author, created_at: Time.unix_ms(data.post.createdAt), nodes: ParagraphConverter.new.convert(content) ) end - def header_data(paragraphs : Array(PostResponse::Paragraph)) : HeaderData - if paragraphs.empty? - return HeaderData.new("", nil) - end - first_two_paragraphs = paragraphs.first(2) - first_two_types = first_two_paragraphs.map(&.type) - if first_two_types == [PostResponse::ParagraphType::H3, PostResponse::ParagraphType::H4] - HeaderData.new( - title: first_two_paragraphs[0].text, - subtitle: first_two_paragraphs[1].text, - ) - else - HeaderData.new( - title: first_two_paragraphs[0].text, - subtitle: nil, - ) - end + def title_and_content(data : PostResponse::Data) : {String, Array(PostResponse::Paragraph)} + title = data.post.title + paragraphs = data.post.content.bodyModel.paragraphs + non_content_paragraphs = paragraphs.reject { |para| para.text == title } + {title, non_content_paragraphs} end end diff --git a/src/models/page.cr b/src/models/page.cr index 6bcb69c..1c22559 100644 --- a/src/models/page.cr +++ b/src/models/page.cr @@ -1,13 +1,11 @@ class Page getter nodes : Nodes::Children getter title : String - getter subtitle : String? getter author : PostResponse::Creator getter created_at : Time def initialize( @title : String, - @subtitle : String?, @author : PostResponse::Creator, @created_at : Time, @nodes : Nodes::Children = [] of Nodes::Child diff --git a/src/pages/articles/show_page.cr b/src/pages/articles/show_page.cr index d3e5dc8..33aadd8 100644 --- a/src/pages/articles/show_page.cr +++ b/src/pages/articles/show_page.cr @@ -7,9 +7,6 @@ class Articles::ShowPage < MainLayout def content h1 page.title - if subtitle = page.subtitle - para subtitle, class: "subtitle" - end para class: "meta" do text "#{author_link(page.author)} on #{page.created_at.to_s("%F")}" end