Class: RichTextRenderer::AssetHyperlinkRenderer

Inherits:
BaseBlockRenderer show all
Defined in:
lib/rich_text_renderer/block_renderers/asset_hyperlink_renderer.rb

Overview

Asset hyperlink renderer

Direct Known Subclasses

AssetBlockRenderer

Constant Summary collapse

ANCHOR_HTML =

Anchor HTML Tag

->(url, text) { "<a href=\"#{url}\">#{text}</a>" }

Instance Attribute Summary

Attributes inherited from BaseNodeRenderer

#mappings

Instance Method Summary collapse

Methods inherited from BaseNodeRenderer

#initialize

Constructor Details

This class inherits a constructor from RichTextRenderer::BaseNodeRenderer

Instance Method Details

#render(node) ⇒ Object

Renders asset nodes



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rich_text_renderer/block_renderers/asset_hyperlink_renderer.rb', line 10

def render(node)
  asset = nil
  begin
    asset = node['data']['target']
  rescue
    fail "Node target is not an asset - Node: #{node}"
  end

  # Check by class name instead of instance type to
  # avoid dependending on the Contentful SDK.
  return render_asset(asset, node) if asset.class.ancestors.map(&:to_s).any? { |name| name.include?('Asset') }

  if asset.is_a?(::Hash)
    unless asset.key?('fields') && asset['fields'].key?('file')
      fail "Node target is not an asset - Node: #{node}"
    end

    return render_hash(asset, node)
  end

  fail "Node target is not an asset - Node: #{node}"
end