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.
This commit is contained in:
Edward Loveall 2021-09-15 15:44:28 -04:00
parent 1c20c81d06
commit 561483cf9f
No known key found for this signature in database
GPG Key ID: 789A4AE983AC8901
8 changed files with 47 additions and 32 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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?

View File

@ -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)

View File

@ -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
)

View File

@ -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