Sha256: 31a4e9c7520bf92ca6995e8d1fc2b8e21079a249e85947bb9921aafd0750ad55

Contents?: true

Size: 1.76 KB

Versions: 21

Compression:

Stored size: 1.76 KB

Contents

module Nexmo
  module OAS
    module Renderer
      module Filters
        class Heading < Banzai::Filter
          def call(input)
            @input = input
            @headings = []

            heading_tag_list = %w[h1 h2 h3 h4 h5 h6]
            headings = document.children.select do |child|
              heading_tag_list.include? child.name
            end

            headings.each do |heading|
              parameterized_heading = parameterized_heading_without_collision(heading)
              heading['id'] = parameterized_heading
              heading['data-id'] = SecureRandom.hex
              heading['class'] = heading[:class]
              heading['class'] += ' Vlt-title--icon'

              heading.prepend_child <<~HEREDOC
                <a href="##{parameterized_heading}" class="heading-permalink">
                  <svg class="Vlt-grey"><use xlink:href=\"/assets/symbol/volta-icons.svg#Vlt-icon-link\" /></svg>
                </a>
              HEREDOC
            end

            @document.to_html
          end

          private

          def document
            @document ||= Nokogiri::HTML::DocumentFragment.parse(@input)
          end

          def parameterized_heading_without_collision(heading)
            parameterized_heading = nil
            index = nil

            loop do
              if index
                parameterized_heading = "#{heading.text.parameterize}-#{index}"
                index += 1
              else
                parameterized_heading = heading.text.parameterize
                index = 2
              end

              break if @headings.exclude? parameterized_heading
            end

            @headings << parameterized_heading
            parameterized_heading
          end
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
nexmo-oas-renderer-0.7.1 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.7.0 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.6.6 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.6.5 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.6.4 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.6.3 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.6.2 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.6.1 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.5.6 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.5.5 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.5.4 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.5.2 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.5.1 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.5.0 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.4.1 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.4.0 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.2.1 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.2.0 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.1.2 lib/nexmo/oas/renderer/filters/heading.rb
nexmo-oas-renderer-0.1.1 lib/nexmo/oas/renderer/filters/heading.rb