Class: Gretel::Renderer::LinkCollection
- Inherits:
-
Array
- Object
- Array
- Gretel::Renderer::LinkCollection
- Defined in:
- lib/gretel/renderer.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#html_safe? ⇒ Boolean
Avoid unnecessary html escaping by template engines.
-
#initialize(context, links, options = {}) ⇒ LinkCollection
constructor
A new instance of LinkCollection.
-
#keys ⇒ Object
Helper for returning all link keys to allow for simple testing.
-
#render ⇒ Object
(also: #to_s)
Renders the links into breadcrumbs.
-
#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.
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, = {}) @context, @links, @options = context, links, concat links end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
159 160 161 |
# File 'lib/gretel/renderer.rb', line 159 def context @context end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
159 160 161 |
# File 'lib/gretel/renderer.rb', line 159 def links @links end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
159 160 161 |
# File 'lib/gretel/renderer.rb', line 159 def @options end |
Instance Method Details
#html_safe? ⇒ Boolean
Avoid unnecessary html escaping by template engines.
221 222 223 |
# File 'lib/gretel/renderer.rb', line 221 def html_safe? true end |
#keys ⇒ Object
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 |
#render ⇒ Object 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 = [:semantic] ? SemanticRenderer : NonSemanticRenderer renderer = renderer_class.new(context, ) # 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([: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 |