Class: BetterSeo::Generators::BreadcrumbsGenerator
- Inherits:
-
Object
- Object
- BetterSeo::Generators::BreadcrumbsGenerator
- Defined in:
- lib/better_seo/generators/breadcrumbs_generator.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
-
#add_item(name, url) ⇒ Object
Add single breadcrumb item.
-
#add_items(items_array) ⇒ Object
Add multiple items from array.
-
#clear ⇒ Object
Clear all items.
-
#initialize ⇒ BreadcrumbsGenerator
constructor
A new instance of BreadcrumbsGenerator.
-
#to_html(schema: false, nav_class: "breadcrumb", list_class: "breadcrumb") ⇒ Object
Generate HTML breadcrumbs.
-
#to_json_ld ⇒ Object
Generate JSON-LD structured data.
-
#to_script_tag ⇒ Object
Generate script tag with JSON-LD.
Constructor Details
#initialize ⇒ BreadcrumbsGenerator
Returns a new instance of BreadcrumbsGenerator.
10 11 12 |
# File 'lib/better_seo/generators/breadcrumbs_generator.rb', line 10 def initialize @items = [] end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
8 9 10 |
# File 'lib/better_seo/generators/breadcrumbs_generator.rb', line 8 def items @items end |
Instance Method Details
#add_item(name, url) ⇒ Object
Add single breadcrumb item
15 16 17 18 |
# File 'lib/better_seo/generators/breadcrumbs_generator.rb', line 15 def add_item(name, url) @items << { name: name, url: url } self end |
#add_items(items_array) ⇒ Object
Add multiple items from array
21 22 23 24 25 26 |
# File 'lib/better_seo/generators/breadcrumbs_generator.rb', line 21 def add_items(items_array) items_array.each do |item| add_item(item[:name], item[:url]) end self end |
#clear ⇒ Object
Clear all items
29 30 31 32 |
# File 'lib/better_seo/generators/breadcrumbs_generator.rb', line 29 def clear @items = [] self end |
#to_html(schema: false, nav_class: "breadcrumb", list_class: "breadcrumb") ⇒ Object
Generate HTML breadcrumbs
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/better_seo/generators/breadcrumbs_generator.rb', line 35 def to_html(schema: false, nav_class: "breadcrumb", list_class: "breadcrumb") return "" if @items.empty? html = [] if schema html << %(<nav class="#{escape_html(nav_class)}" aria-label="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">) else html << %(<nav class="#{escape_html(nav_class)}" aria-label="breadcrumb">) end html << %( <ol class="#{escape_html(list_class)}">) @items.each_with_index do |item, index| active_class = item[:url].nil? ? " active" : "" aria_current = item[:url].nil? ? ' aria-current="page"' : "" if schema html << %( <li class="breadcrumb-item#{active_class}" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"#{aria_current}>) else html << %( <li class="breadcrumb-item#{active_class}"#{aria_current}>) end if item[:url] if schema html << %( <a href="#{escape_html(item[:url])}" itemprop="item">) html << %( <span itemprop="name">#{escape_html(item[:name])}</span>) html << %( </a>) html << %( <meta itemprop="position" content="#{index + 1}" />) else html << %( <a href="#{escape_html(item[:url])}">#{escape_html(item[:name])}</a>) end elsif schema html << %( <span itemprop="name">#{escape_html(item[:name])}</span>) html << %( <meta itemprop="position" content="#{index + 1}" />) else html << %( #{escape_html(item[:name])}) end html << %( </li>) end html << %( </ol>) html << %(</nav>) html.join("\n") end |
#to_json_ld ⇒ Object
Generate JSON-LD structured data
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/better_seo/generators/breadcrumbs_generator.rb', line 84 def to_json_ld return "" if @items.empty? data = { "@context" => "https://schema.org", "@type" => "BreadcrumbList", "itemListElement" => [] } @items.each_with_index do |item, index| list_item = { "@type" => "ListItem", "position" => index + 1, "name" => item[:name] } list_item["item"] = item[:url] if item[:url] data["itemListElement"] << list_item end JSON.generate(data) end |
#to_script_tag ⇒ Object
Generate script tag with JSON-LD
107 108 109 110 111 |
# File 'lib/better_seo/generators/breadcrumbs_generator.rb', line 107 def to_script_tag return "" if @items.empty? %(<script type="application/ld+json">\n#{to_json_ld}\n</script>) end |