Render a Link Anchor
As opposed to a user anchor
This commit is contained in:
parent
5a5f68bcf8
commit
bc356baa45
6 changed files with 71 additions and 5 deletions
|
@ -96,4 +96,37 @@ describe MarkupConverter do
|
||||||
Code.new(children: [Text.new(content: "code")] of Child),
|
Code.new(children: [Text.new(content: "code")] of Child),
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "renders an A LINK markup" do
|
||||||
|
json = <<-JSON
|
||||||
|
{
|
||||||
|
"text": "I am a Link",
|
||||||
|
"type": "P",
|
||||||
|
"markups": [
|
||||||
|
{
|
||||||
|
"title": "",
|
||||||
|
"type": "A",
|
||||||
|
"href": "https://example.com",
|
||||||
|
"start": 7,
|
||||||
|
"end": 11,
|
||||||
|
"rel": "",
|
||||||
|
"anchorType": "LINK"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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("I am a "),
|
||||||
|
Anchor.new(text: "Link", href: "https://example.com")
|
||||||
|
])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,6 +38,16 @@ describe PageContent do
|
||||||
html.should eq %(<p>Hello, <em>World!</em></p><ul><li>List!</li><li>Again!</li></ul>)
|
html.should eq %(<p>Hello, <em>World!</em></p><ul><li>List!</li><li>Again!</li></ul>)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "renders an anchor" do
|
||||||
|
page = Page.new(nodes: [
|
||||||
|
Anchor.new(href: "https://example.com", text: "link"),
|
||||||
|
] of Child)
|
||||||
|
|
||||||
|
html = PageContent.new(page: page).render_to_string
|
||||||
|
|
||||||
|
html.should eq %(<a href="https://example.com">link</a>)
|
||||||
|
end
|
||||||
|
|
||||||
it "renders a blockquote" do
|
it "renders a blockquote" do
|
||||||
page = Page.new(nodes: [
|
page = Page.new(nodes: [
|
||||||
BlockQuote.new(children: [
|
BlockQuote.new(children: [
|
||||||
|
|
|
@ -37,14 +37,16 @@ class MarkupConverter
|
||||||
[Text.new(plain)] of Child
|
[Text.new(plain)] of Child
|
||||||
else
|
else
|
||||||
case markup.type
|
case markup.type
|
||||||
|
when PostResponse::MarkupType::A
|
||||||
|
container = Anchor.new(href: markup.href || "#", text: to_be_marked)
|
||||||
when PostResponse::MarkupType::CODE
|
when PostResponse::MarkupType::CODE
|
||||||
container = construct_markup(to_be_marked, Code)
|
container = construct_markup(text: to_be_marked, container: Code)
|
||||||
when PostResponse::MarkupType::EM
|
when PostResponse::MarkupType::EM
|
||||||
container = construct_markup(to_be_marked, Emphasis)
|
container = construct_markup(text: to_be_marked, container: Emphasis)
|
||||||
when PostResponse::MarkupType::STRONG
|
when PostResponse::MarkupType::STRONG
|
||||||
container = construct_markup(to_be_marked, Strong)
|
container = construct_markup(text: to_be_marked, container: Strong)
|
||||||
else
|
else
|
||||||
container = construct_markup(to_be_marked, Code)
|
container = construct_markup(text: to_be_marked, container: Code)
|
||||||
end
|
end
|
||||||
[Text.new(plain), container] of Child
|
[Text.new(plain), container] of Child
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,6 +38,7 @@ class MediumClient
|
||||||
markups {
|
markups {
|
||||||
name
|
name
|
||||||
type
|
type
|
||||||
|
href
|
||||||
start
|
start
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,10 @@ class PageContent < BaseComponent
|
||||||
children.each { |child| render_child(child) }
|
children.each { |child| render_child(child) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_child(node : Anchor)
|
||||||
|
a(href: node.href) { text node.text }
|
||||||
|
end
|
||||||
|
|
||||||
def render_child(node : BlockQuote)
|
def render_child(node : BlockQuote)
|
||||||
blockquote { render_children(node.children) }
|
blockquote { render_children(node.children) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
module Nodes
|
module Nodes
|
||||||
alias Leaf = Text | Image | IFrame
|
alias Leaf = Text | Image | IFrame | Anchor
|
||||||
alias Child = Container | Leaf | Empty
|
alias Child = Container | Leaf | Empty
|
||||||
alias Children = Array(Child)
|
alias Children = Array(Child)
|
||||||
|
|
||||||
|
@ -104,4 +104,20 @@ module Nodes
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Anchor
|
||||||
|
getter href : String
|
||||||
|
getter text : String
|
||||||
|
|
||||||
|
def initialize(@href : String, @text : String)
|
||||||
|
end
|
||||||
|
|
||||||
|
def ==(other : Anchor)
|
||||||
|
other.href == href && other.text == text
|
||||||
|
end
|
||||||
|
|
||||||
|
def empty?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue