Render a User Anchor

This commit is contained in:
Edward Loveall 2021-07-04 17:37:45 -04:00
parent bc356baa45
commit 743d9e5fa9
No known key found for this signature in database
GPG Key ID: 789A4AE983AC8901
7 changed files with 78 additions and 2 deletions

View File

@ -129,4 +129,39 @@ describe MarkupConverter do
Anchor.new(text: "Link", href: "https://example.com")
])
end
it "renders an A USER markup" do
json = <<-JSON
{
"text": "Hi Dr Nick!",
"type": "P",
"markups": [
{
"title": null,
"type": "A",
"href": null,
"userId": "abc123",
"start": 3,
"end": 10,
"rel": null,
"anchorType": "USER"
}
],
"href": null,
"iframe": null,
"layout": null,
"metadata": null
}
JSON
paragraph = PostResponse::Paragraph.from_json(json)
result = MarkupConverter.convert(text: paragraph.text, markups: paragraph.markups)
result.should eq([
Text.new("Hi "),
UserAnchor.new(text: "Dr Nick", userId: "abc123"),
Text.new("!"),
])
end
end

View File

@ -196,4 +196,14 @@ describe PageContent do
html.should eq %(<ul><li>Apple</li><li>Banana</li></ul>)
end
it "renders a user anchor" do
page = Page.new(nodes: [
UserAnchor.new(userId: "abc123", text: "Some User"),
] of Child)
html = PageContent.new(page: page).render_to_string
html.should eq %(<a href="https://medium.com/u/abc123">Some User</a>)
end
end

View File

@ -38,7 +38,13 @@ class MarkupConverter
else
case markup.type
when PostResponse::MarkupType::A
container = Anchor.new(href: markup.href || "#", text: to_be_marked)
if href = markup.href
container = Anchor.new(href: href, text: to_be_marked)
elsif userId = markup.userId
container = UserAnchor.new(userId: userId, text: to_be_marked)
else
container = Empty.new
end
when PostResponse::MarkupType::CODE
container = construct_markup(text: to_be_marked, container: Code)
when PostResponse::MarkupType::EM

View File

@ -39,6 +39,7 @@ class MediumClient
name
type
href
userId
start
end
}

View File

@ -85,4 +85,8 @@ class PageContent < BaseComponent
def render_child(node : UnorderedList)
ul { render_children(node.children) }
end
def render_child(node : UserAnchor)
a(href: node.href) { text node.text }
end
end

View File

@ -1,5 +1,5 @@
module Nodes
alias Leaf = Text | Image | IFrame | Anchor
alias Leaf = Text | Image | IFrame | Anchor | UserAnchor
alias Child = Container | Leaf | Empty
alias Children = Array(Child)
@ -120,4 +120,23 @@ module Nodes
false
end
end
class UserAnchor
USER_BASE_URL = "https://medium.com/u/"
getter href : String
getter text : String
def initialize(userId : String, @text : String)
@href = USER_BASE_URL + userId
end
def ==(other : UserAnchor)
other.href == href && other.text == text
end
def empty?
false
end
end
end

View File

@ -55,6 +55,7 @@ class PostResponse
property title : String?
property type : MarkupType
property href : String?
property userId : String?
property start : Int32
property end : Int32
property anchorType : AnchorType?