Anchor and UserAnchor nodes can contain children
The impetus for this change was to help make the MarkupConverter code more robust. However, it's also possible that an Anchor can contain styled text. For example, in markdown someone might write a link that contains some <strong> text: ```markdown [this link is so **good**](https://example.com) ``` This setup will now allow that. Unknown if UserAnchor can ever contain any text that isn't just the user's name, but it's easy to deal with and makes the typing much easier.
This commit is contained in:
parent
130b235a6c
commit
31f7d6956c
6 changed files with 19 additions and 18 deletions
|
@ -126,7 +126,7 @@ describe MarkupConverter do
|
||||||
|
|
||||||
result.should eq([
|
result.should eq([
|
||||||
Text.new("I am a "),
|
Text.new("I am a "),
|
||||||
Anchor.new(text: "Link", href: "https://example.com"),
|
Anchor.new(children: [Text.new("Link")] of Child, href: "https://example.com"),
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -160,7 +160,7 @@ describe MarkupConverter do
|
||||||
|
|
||||||
result.should eq([
|
result.should eq([
|
||||||
Text.new("Hi "),
|
Text.new("Hi "),
|
||||||
UserAnchor.new(text: "Dr Nick", userId: "abc123"),
|
UserAnchor.new(children: [Text.new("Dr Nick")] of Child, userId: "abc123"),
|
||||||
Text.new("!"),
|
Text.new("!"),
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
|
|
|
@ -190,7 +190,10 @@ describe ParagraphConverter do
|
||||||
Image.new(src: "image.png", originalWidth: 1000, originalHeight: 600),
|
Image.new(src: "image.png", originalWidth: 1000, originalHeight: 600),
|
||||||
FigureCaption.new(children: [
|
FigureCaption.new(children: [
|
||||||
Text.new("Image by "),
|
Text.new("Image by "),
|
||||||
Anchor.new(href: "https://unsplash.com/@someuser", text: "someuser"),
|
Anchor.new(
|
||||||
|
children: [Text.new("someuser")] of Child,
|
||||||
|
href: "https://unsplash.com/@someuser"
|
||||||
|
),
|
||||||
] of Child),
|
] of Child),
|
||||||
] of Child),
|
] of Child),
|
||||||
]
|
]
|
||||||
|
|
|
@ -40,7 +40,7 @@ describe PageContent do
|
||||||
|
|
||||||
it "renders an anchor" do
|
it "renders an anchor" do
|
||||||
page = Page.new(nodes: [
|
page = Page.new(nodes: [
|
||||||
Anchor.new(href: "https://example.com", text: "link"),
|
Anchor.new(children: [Text.new("link")] of Child, href: "https://example.com"),
|
||||||
] of Child)
|
] of Child)
|
||||||
|
|
||||||
html = PageContent.new(page: page).render_to_string
|
html = PageContent.new(page: page).render_to_string
|
||||||
|
@ -229,7 +229,7 @@ describe PageContent do
|
||||||
|
|
||||||
it "renders a user anchor" do
|
it "renders a user anchor" do
|
||||||
page = Page.new(nodes: [
|
page = Page.new(nodes: [
|
||||||
UserAnchor.new(userId: "abc123", text: "Some User"),
|
UserAnchor.new(children: [Text.new("Some User")] of Child, userId: "abc123"),
|
||||||
] of Child)
|
] of Child)
|
||||||
|
|
||||||
html = PageContent.new(page: page).render_to_string
|
html = PageContent.new(page: page).render_to_string
|
||||||
|
|
|
@ -39,9 +39,9 @@ class MarkupConverter
|
||||||
case markup.type
|
case markup.type
|
||||||
when PostResponse::MarkupType::A
|
when PostResponse::MarkupType::A
|
||||||
if href = markup.href
|
if href = markup.href
|
||||||
container = Anchor.new(href: href, text: to_be_marked)
|
container = Anchor.new(children: [Text.new(to_be_marked)] of Child, href: href)
|
||||||
elsif userId = markup.userId
|
elsif userId = markup.userId
|
||||||
container = UserAnchor.new(userId: userId, text: to_be_marked)
|
container = UserAnchor.new(children: [Text.new(to_be_marked)] of Child, userId: userId)
|
||||||
else
|
else
|
||||||
container = Empty.new
|
container = Empty.new
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,7 +13,7 @@ class PageContent < BaseComponent
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_child(node : Anchor)
|
def render_child(node : Anchor)
|
||||||
a(href: node.href) { text node.text }
|
a(href: node.href) { render_children(node.children) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_child(node : BlockQuote)
|
def render_child(node : BlockQuote)
|
||||||
|
@ -95,6 +95,6 @@ class PageContent < BaseComponent
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_child(node : UserAnchor)
|
def render_child(node : UserAnchor)
|
||||||
a(href: node.href) { text node.text }
|
a(href: node.href) { render_children(node.children) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
module Nodes
|
module Nodes
|
||||||
alias Leaf = Text | Image | IFrame | Anchor | UserAnchor
|
alias Leaf = Text | Image | IFrame
|
||||||
alias Child = Container | Leaf | Empty
|
alias Child = Container | Leaf | Empty
|
||||||
alias Children = Array(Child)
|
alias Children = Array(Child)
|
||||||
|
|
||||||
|
@ -136,15 +136,14 @@ module Nodes
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Anchor
|
class Anchor < Container
|
||||||
getter href : String
|
getter href : String
|
||||||
getter text : String
|
|
||||||
|
|
||||||
def initialize(@href : String, @text : String)
|
def initialize(@children : Children, @href : String)
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other : Anchor)
|
def ==(other : Anchor)
|
||||||
other.href == href && other.text == text
|
other.children == children && other.href == href
|
||||||
end
|
end
|
||||||
|
|
||||||
def empty?
|
def empty?
|
||||||
|
@ -152,18 +151,17 @@ module Nodes
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class UserAnchor
|
class UserAnchor < Container
|
||||||
USER_BASE_URL = "https://medium.com/u/"
|
USER_BASE_URL = "https://medium.com/u/"
|
||||||
|
|
||||||
getter href : String
|
getter href : String
|
||||||
getter text : String
|
|
||||||
|
|
||||||
def initialize(userId : String, @text : String)
|
def initialize(@children : Children, userId : String)
|
||||||
@href = USER_BASE_URL + userId
|
@href = USER_BASE_URL + userId
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other : UserAnchor)
|
def ==(other : UserAnchor)
|
||||||
other.href == href && other.text == text
|
other.children == children && other.href == href
|
||||||
end
|
end
|
||||||
|
|
||||||
def empty?
|
def empty?
|
||||||
|
|
Loading…
Reference in a new issue