diff --git a/spec/classes/markup_converter_spec.cr b/spec/classes/markup_converter_spec.cr
index a87a2b2..45c295f 100644
--- a/spec/classes/markup_converter_spec.cr
+++ b/spec/classes/markup_converter_spec.cr
@@ -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
diff --git a/spec/components/page_content_spec.cr b/spec/components/page_content_spec.cr
index 3bab1c1..06cd479 100644
--- a/spec/components/page_content_spec.cr
+++ b/spec/components/page_content_spec.cr
@@ -38,6 +38,16 @@ describe PageContent do
html.should eq %(
Hello, World!
)
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 %(link)
+ end
+
it "renders a blockquote" do
page = Page.new(nodes: [
BlockQuote.new(children: [
diff --git a/src/classes/markup_converter.cr b/src/classes/markup_converter.cr
index 0e40f53..e02783c 100644
--- a/src/classes/markup_converter.cr
+++ b/src/classes/markup_converter.cr
@@ -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
diff --git a/src/clients/medium_client.cr b/src/clients/medium_client.cr
index 6a2328d..82b9f4a 100644
--- a/src/clients/medium_client.cr
+++ b/src/clients/medium_client.cr
@@ -38,6 +38,7 @@ class MediumClient
markups {
name
type
+ href
start
end
}
diff --git a/src/components/page_content.cr b/src/components/page_content.cr
index 49fe832..df68634 100644
--- a/src/components/page_content.cr
+++ b/src/components/page_content.cr
@@ -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
diff --git a/src/models/nodes.cr b/src/models/nodes.cr
index 99d02a1..a8cab7a 100644
--- a/src/models/nodes.cr
+++ b/src/models/nodes.cr
@@ -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