Render a Link Anchor

As opposed to a user anchor
This commit is contained in:
Edward Loveall 2021-07-04 17:06:17 -04:00
parent 5a5f68bcf8
commit bc356baa45
No known key found for this signature in database
GPG key ID: 789A4AE983AC8901
6 changed files with 71 additions and 5 deletions

View file

@ -96,4 +96,37 @@ describe MarkupConverter do
Code.new(children: [Text.new(content: "code")] of Child),
])
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

View file

@ -38,6 +38,16 @@ describe PageContent do
html.should eq %(<p>Hello, <em>World!</em></p><ul><li>List!</li><li>Again!</li></ul>)
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
page = Page.new(nodes: [
BlockQuote.new(children: [

View file

@ -37,14 +37,16 @@ class MarkupConverter
[Text.new(plain)] of Child
else
case markup.type
when PostResponse::MarkupType::A
container = Anchor.new(href: markup.href || "#", text: to_be_marked)
when PostResponse::MarkupType::CODE
container = construct_markup(to_be_marked, Code)
container = construct_markup(text: to_be_marked, container: Code)
when PostResponse::MarkupType::EM
container = construct_markup(to_be_marked, Emphasis)
container = construct_markup(text: to_be_marked, container: Emphasis)
when PostResponse::MarkupType::STRONG
container = construct_markup(to_be_marked, Strong)
container = construct_markup(text: to_be_marked, container: Strong)
else
container = construct_markup(to_be_marked, Code)
container = construct_markup(text: to_be_marked, container: Code)
end
[Text.new(plain), container] of Child
end

View file

@ -38,6 +38,7 @@ class MediumClient
markups {
name
type
href
start
end
}

View file

@ -12,6 +12,10 @@ class PageContent < BaseComponent
children.each { |child| render_child(child) }
end
def render_child(node : Anchor)
a(href: node.href) { text node.text }
end
def render_child(node : BlockQuote)
blockquote { render_children(node.children) }
end

View file

@ -1,5 +1,5 @@
module Nodes
alias Leaf = Text | Image | IFrame
alias Leaf = Text | Image | IFrame | Anchor
alias Child = Container | Leaf | Empty
alias Children = Array(Child)
@ -104,4 +104,20 @@ module Nodes
false
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