Render a User Anchor
This commit is contained in:
parent
bc356baa45
commit
743d9e5fa9
7 changed files with 78 additions and 2 deletions
|
@ -129,4 +129,39 @@ describe MarkupConverter do
|
||||||
Anchor.new(text: "Link", href: "https://example.com")
|
Anchor.new(text: "Link", href: "https://example.com")
|
||||||
])
|
])
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -196,4 +196,14 @@ describe PageContent do
|
||||||
|
|
||||||
html.should eq %(<ul><li>Apple</li><li>Banana</li></ul>)
|
html.should eq %(<ul><li>Apple</li><li>Banana</li></ul>)
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -38,7 +38,13 @@ class MarkupConverter
|
||||||
else
|
else
|
||||||
case markup.type
|
case markup.type
|
||||||
when PostResponse::MarkupType::A
|
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
|
when PostResponse::MarkupType::CODE
|
||||||
container = construct_markup(text: to_be_marked, container: Code)
|
container = construct_markup(text: to_be_marked, container: Code)
|
||||||
when PostResponse::MarkupType::EM
|
when PostResponse::MarkupType::EM
|
||||||
|
|
|
@ -39,6 +39,7 @@ class MediumClient
|
||||||
name
|
name
|
||||||
type
|
type
|
||||||
href
|
href
|
||||||
|
userId
|
||||||
start
|
start
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,4 +85,8 @@ class PageContent < BaseComponent
|
||||||
def render_child(node : UnorderedList)
|
def render_child(node : UnorderedList)
|
||||||
ul { render_children(node.children) }
|
ul { render_children(node.children) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_child(node : UserAnchor)
|
||||||
|
a(href: node.href) { text node.text }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
module Nodes
|
module Nodes
|
||||||
alias Leaf = Text | Image | IFrame | Anchor
|
alias Leaf = Text | Image | IFrame | Anchor | UserAnchor
|
||||||
alias Child = Container | Leaf | Empty
|
alias Child = Container | Leaf | Empty
|
||||||
alias Children = Array(Child)
|
alias Children = Array(Child)
|
||||||
|
|
||||||
|
@ -120,4 +120,23 @@ module Nodes
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -55,6 +55,7 @@ class PostResponse
|
||||||
property title : String?
|
property title : String?
|
||||||
property type : MarkupType
|
property type : MarkupType
|
||||||
property href : String?
|
property href : String?
|
||||||
|
property userId : String?
|
||||||
property start : Int32
|
property start : Int32
|
||||||
property end : Int32
|
property end : Int32
|
||||||
property anchorType : AnchorType?
|
property anchorType : AnchorType?
|
||||||
|
|
Loading…
Reference in a new issue