From 743d9e5fa9bca08a196ceb21617cd3c80aaac3ef Mon Sep 17 00:00:00 2001 From: Edward Loveall Date: Sun, 4 Jul 2021 17:37:45 -0400 Subject: [PATCH] Render a User Anchor --- spec/classes/markup_converter_spec.cr | 35 +++++++++++++++++++++++++++ spec/components/page_content_spec.cr | 10 ++++++++ src/classes/markup_converter.cr | 8 +++++- src/clients/medium_client.cr | 1 + src/components/page_content.cr | 4 +++ src/models/nodes.cr | 21 +++++++++++++++- src/models/post_response.cr | 1 + 7 files changed, 78 insertions(+), 2 deletions(-) diff --git a/spec/classes/markup_converter_spec.cr b/spec/classes/markup_converter_spec.cr index 45c295f..8f5ed09 100644 --- a/spec/classes/markup_converter_spec.cr +++ b/spec/classes/markup_converter_spec.cr @@ -129,4 +129,39 @@ describe MarkupConverter do Anchor.new(text: "Link", href: "https://example.com") ]) 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 diff --git a/spec/components/page_content_spec.cr b/spec/components/page_content_spec.cr index 06cd479..ff25dac 100644 --- a/spec/components/page_content_spec.cr +++ b/spec/components/page_content_spec.cr @@ -196,4 +196,14 @@ describe PageContent do html.should eq %() 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 %(Some User) + end end diff --git a/src/classes/markup_converter.cr b/src/classes/markup_converter.cr index e02783c..5830d77 100644 --- a/src/classes/markup_converter.cr +++ b/src/classes/markup_converter.cr @@ -38,7 +38,13 @@ class MarkupConverter else case markup.type 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 container = construct_markup(text: to_be_marked, container: Code) when PostResponse::MarkupType::EM diff --git a/src/clients/medium_client.cr b/src/clients/medium_client.cr index 82b9f4a..10d567e 100644 --- a/src/clients/medium_client.cr +++ b/src/clients/medium_client.cr @@ -39,6 +39,7 @@ class MediumClient name type href + userId start end } diff --git a/src/components/page_content.cr b/src/components/page_content.cr index df68634..52c4eb3 100644 --- a/src/components/page_content.cr +++ b/src/components/page_content.cr @@ -85,4 +85,8 @@ class PageContent < BaseComponent def render_child(node : UnorderedList) ul { render_children(node.children) } end + + def render_child(node : UserAnchor) + a(href: node.href) { text node.text } + end end diff --git a/src/models/nodes.cr b/src/models/nodes.cr index a8cab7a..b533099 100644 --- a/src/models/nodes.cr +++ b/src/models/nodes.cr @@ -1,5 +1,5 @@ module Nodes - alias Leaf = Text | Image | IFrame | Anchor + alias Leaf = Text | Image | IFrame | Anchor | UserAnchor alias Child = Container | Leaf | Empty alias Children = Array(Child) @@ -120,4 +120,23 @@ module Nodes false 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 diff --git a/src/models/post_response.cr b/src/models/post_response.cr index c8447cc..31bf0ae 100644 --- a/src/models/post_response.cr +++ b/src/models/post_response.cr @@ -55,6 +55,7 @@ class PostResponse property title : String? property type : MarkupType property href : String? + property userId : String? property start : Int32 property end : Int32 property anchorType : AnchorType?