class Gretel::Renderer::LinkCollection

Attributes

context[R]
options[R]

Public Class Methods

new(context, links, options = {}) click to toggle source
# File lib/gretel/renderer.rb, line 161
def initialize(context, links, options = {})
  @context, @links, @options = context, links, options
  concat links
end

Public Instance Methods

html_safe?() click to toggle source

Avoid unnecessary html escaping by template engines.

# File lib/gretel/renderer.rb, line 221
def html_safe?
  true
end
keys() click to toggle source

Helper for returning all link keys to allow for simple testing.

# File lib/gretel/renderer.rb, line 188
def keys
  map(&:key)
end
render() click to toggle source

Renders the links into breadcrumbs.

# File lib/gretel/renderer.rb, line 193
def render
  return "" if links.empty?

  renderer_class = options[:semantic] ? SemanticRenderer : NonSemanticRenderer
  renderer = renderer_class.new(context, options)
  # Loop through all but the last (current) link and build HTML of the fragments
  fragments = links[0..-2].map.with_index do |link, index|
    renderer.render_fragment(link, index + 1)
  end

  # The current link is handled a little differently, and is only linked if specified in the options
  current_link = links.last
  position = links.size
  fragments << renderer.render_current_fragment(current_link, position)

  # Build the final HTML
  html_fragments = [
    renderer.render_pretext,
    fragments.join(options[:separator]),
    renderer.render_posttext
  ]
  html = html_fragments.compact.join(" ").html_safe
  renderer.render_container(html)
end
Also aliased as: to_s
structured_data(url_base:) click to toggle source

Returns a hash matching the JSON-LD Structured Data schema developers.google.com/search/docs/data-types/breadcrumb#json-ld

# File lib/gretel/renderer.rb, line 168
def structured_data(url_base:)
  url_base = url_base.chomp("/") # Remove trailing `/`, if present

  items = @links.each_with_index.map do |link, i|
    {
      "@type": "ListItem",
      "position": i + 1,
      "name": link.text,
      "item": "#{url_base}#{link.url}"
    }
  end

  {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    "itemListElement": items
  }
end
to_s()
Alias for: render