Class: Gretel::Renderer::LinkCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/gretel/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, links, options = {}) ⇒ LinkCollection

Returns a new instance of LinkCollection.



161
162
163
164
# File 'lib/gretel/renderer.rb', line 161

def initialize(context, links, options = {})
  @context, @links, @options = context, links, options
  concat links
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



159
160
161
# File 'lib/gretel/renderer.rb', line 159

def context
  @context
end

Returns the value of attribute links.



159
160
161
# File 'lib/gretel/renderer.rb', line 159

def links
  @links
end

#optionsObject (readonly)

Returns the value of attribute options.



159
160
161
# File 'lib/gretel/renderer.rb', line 159

def options
  @options
end

Instance Method Details

#html_safe?Boolean

Avoid unnecessary html escaping by template engines.

Returns:

  • (Boolean)


221
222
223
# File 'lib/gretel/renderer.rb', line 221

def html_safe?
  true
end

#keysObject

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



188
189
190
# File 'lib/gretel/renderer.rb', line 188

def keys
  map(&:key)
end

#renderObject Also known as: to_s

Renders the links into breadcrumbs.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# 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

#structured_data(url_base:) ⇒ Object

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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# 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