From 561483cf9f608157148fc58177d8264f001e3265 Mon Sep 17 00:00:00 2001 From: Edward Loveall Date: Wed, 15 Sep 2021 15:44:28 -0400 Subject: [PATCH] Link to the author's page Right now this links to the user's medium page. It may link to an internal page in the future. Instead of the Page taking the author as a string, it now takes a PostResponse::Creator object. The Articles::ShowPage then converts the Creator (a name and user_id) to an author link. Finally, I did some refactoring of UserAnchor (which I thought I was going to use for this) to change it's userId attribute to user_id as is Crystal convention. --- spec/classes/markup_converter_spec.cr | 4 +-- spec/classes/page_converter_spec.cr | 3 +- spec/components/page_content_spec.cr | 51 ++++++++++++++++----------- src/classes/markup_converter.cr | 4 +-- src/classes/page_converter.cr | 2 +- src/models/nodes.cr | 4 +-- src/models/page.cr | 4 +-- src/pages/articles/show_page.cr | 7 +++- 8 files changed, 47 insertions(+), 32 deletions(-) diff --git a/spec/classes/markup_converter_spec.cr b/spec/classes/markup_converter_spec.cr index 20546df..79b956e 100644 --- a/spec/classes/markup_converter_spec.cr +++ b/spec/classes/markup_converter_spec.cr @@ -112,7 +112,7 @@ describe MarkupConverter do result.should eq([ Text.new("Hi "), - UserAnchor.new(children: [Text.new("Dr Nick")] of Child, userId: "abc123"), + UserAnchor.new(children: [Text.new("Dr Nick")] of Child, user_id: "abc123"), Text.new("!"), ]) end @@ -189,7 +189,7 @@ describe MarkupConverter do Strong.new([ Text.new("jack"), ] of Child), - ] of Child, userId: "abc123"), + ] of Child, user_id: "abc123"), ]) end end diff --git a/spec/classes/page_converter_spec.cr b/spec/classes/page_converter_spec.cr index 27bc613..30b0a3b 100644 --- a/spec/classes/page_converter_spec.cr +++ b/spec/classes/page_converter_spec.cr @@ -77,7 +77,8 @@ describe PageConverter do page = PageConverter.new.convert(data) - page.author.should eq "Author" + page.author.name.should eq "Author" + page.author.id.should eq "abc123" end it "sets the publish date/time" do diff --git a/spec/components/page_content_spec.cr b/spec/components/page_content_spec.cr index 635052b..e64658d 100644 --- a/spec/components/page_content_spec.cr +++ b/spec/components/page_content_spec.cr @@ -7,7 +7,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Paragraph.new(children: [ @@ -25,7 +25,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Paragraph.new(children: [ @@ -54,7 +54,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Anchor.new(children: [Text.new("link")] of Child, href: "https://example.com"), @@ -70,7 +70,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ BlockQuote.new(children: [ @@ -88,7 +88,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Code.new(children: [ @@ -106,7 +106,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Paragraph.new(children: [ @@ -128,7 +128,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Figure.new(children: [ @@ -156,7 +156,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ GithubGist.new(href: "https://gist.github.com/user/some_id"), @@ -174,7 +174,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Heading2.new(children: [ @@ -192,7 +192,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Heading3.new(children: [ @@ -210,7 +210,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Paragraph.new(children: [ @@ -232,7 +232,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ EmbeddedContent.new( @@ -257,7 +257,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Paragraph.new(children: [ @@ -281,7 +281,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Paragraph.new(children: [ @@ -310,7 +310,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ OrderedList.new(children: [ @@ -329,7 +329,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Paragraph.new(children: [ @@ -347,7 +347,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Preformatted.new(children: [ @@ -365,7 +365,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ Strong.new(children: [ @@ -383,7 +383,7 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ UnorderedList.new(children: [ @@ -402,10 +402,10 @@ describe PageContent do page = Page.new( title: "Title", subtitle: nil, - author: "Author", + author: user_anchor_factory, created_at: Time.local, nodes: [ - UserAnchor.new(children: [Text.new("Some User")] of Child, userId: "abc123"), + UserAnchor.new(children: [Text.new("Some User")] of Child, user_id: "abc123"), ] of Child ) @@ -418,3 +418,12 @@ end def stripped_html(html : String) html.gsub(/\n\s*/, "").strip end + +def user_anchor_factory(username = "someone", user_id = "abc123") + PostResponse::Creator.from_json <<-JSON + { + "id": "#{user_id}", + "name": "#{username}" + } + JSON +end diff --git a/src/classes/markup_converter.cr b/src/classes/markup_converter.cr index 50ba18c..5a47460 100644 --- a/src/classes/markup_converter.cr +++ b/src/classes/markup_converter.cr @@ -54,8 +54,8 @@ class MarkupConverter when PostResponse::MarkupType::A if href = markup.href Anchor.new(href: href, children: [child] of Child) - elsif userId = markup.userId - UserAnchor.new(userId: userId, children: [child] of Child) + elsif user_id = markup.userId + UserAnchor.new(user_id: user_id, children: [child] of Child) else Empty.new end diff --git a/src/classes/page_converter.cr b/src/classes/page_converter.cr index 43c01ba..b4ee7d4 100644 --- a/src/classes/page_converter.cr +++ b/src/classes/page_converter.cr @@ -19,7 +19,7 @@ end class PageConverter def convert(data : PostResponse::Data) : Page paragraphs = data.post.content.bodyModel.paragraphs - author = data.post.creator.name + author = data.post.creator created_at = Time.unix_ms(data.post.createdAt) header = header_data(paragraphs) if header.first_content_paragraph_index.zero? diff --git a/src/models/nodes.cr b/src/models/nodes.cr index 4721e33..9518225 100644 --- a/src/models/nodes.cr +++ b/src/models/nodes.cr @@ -193,8 +193,8 @@ module Nodes getter href : String - def initialize(@children : Children, userId : String) - @href = USER_BASE_URL + userId + def initialize(@children : Children, user_id : String) + @href = USER_BASE_URL + user_id end def ==(other : UserAnchor) diff --git a/src/models/page.cr b/src/models/page.cr index 5a9041e..6bcb69c 100644 --- a/src/models/page.cr +++ b/src/models/page.cr @@ -2,13 +2,13 @@ class Page getter nodes : Nodes::Children getter title : String getter subtitle : String? - getter author : String + getter author : PostResponse::Creator getter created_at : Time def initialize( @title : String, @subtitle : String?, - @author : 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 14c62dd..d3e5dc8 100644 --- a/src/pages/articles/show_page.cr +++ b/src/pages/articles/show_page.cr @@ -11,7 +11,7 @@ class Articles::ShowPage < MainLayout para subtitle, class: "subtitle" end para class: "meta" do - text "#{page.author} on #{page.created_at.to_s("%F")}" + text "#{author_link(page.author)} on #{page.created_at.to_s("%F")}" end article do section do @@ -19,4 +19,9 @@ class Articles::ShowPage < MainLayout end end end + + def author_link(creator : PostResponse::Creator) + href = Nodes::UserAnchor::USER_BASE_URL + creator.id + a(href: href) { text creator.name } + end end