Sha256: 5fb196f1f2aa75f7850beff8469a7cf2c04d8cae8d77df978f6cd827a2e46d52

Contents?: true

Size: 852 Bytes

Versions: 21

Compression:

Stored size: 852 Bytes

Contents

# frozen_string_literal: true

require "refinements/arrays"

module Tocer
  module Transformers
    # Transforms a Markdown header (plain text) into a table of contents link.
    class Text
      using Refinements::Arrays

      def initialize text, header: Parsers::Header
        @header = header.new text
      end

      def label
        header.content
      end

      def url
        label.downcase.gsub(/\s/, "-").gsub(/[^\w\-]+/, "")
      end

      def transform url_suffix: ""
        modified_url = [url, url_suffix.to_s].compress.join "-"
        "#{indented_bullet}[#{label}](##{modified_url})"
      end

      private

      attr_reader :header

      def prefix_to_spaces
        Array.new(header.prefix.length, "  ").join
      end

      def indented_bullet
        prefix_to_spaces.gsub(/\s{2}$/, "- ")
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
tocer-8.2.1 lib/tocer/transformers/text.rb
tocer-8.2.0 lib/tocer/transformers/text.rb
tocer-8.1.0 lib/tocer/transformers/text.rb
tocer-8.0.0 lib/tocer/transformers/text.rb
tocer-7.2.0 lib/tocer/transformers/text.rb
tocer-7.1.0 lib/tocer/transformers/text.rb
tocer-7.0.2 lib/tocer/transformers/text.rb
tocer-7.0.1 lib/tocer/transformers/text.rb
tocer-7.0.0 lib/tocer/transformers/text.rb
tocer-6.3.1 lib/tocer/transformers/text.rb
tocer-6.3.0 lib/tocer/transformers/text.rb
tocer-6.2.0 lib/tocer/transformers/text.rb
tocer-6.1.0 lib/tocer/transformers/text.rb
tocer-6.0.0 lib/tocer/transformers/text.rb
tocer-5.0.0 lib/tocer/transformers/text.rb
tocer-4.0.0 lib/tocer/transformers/text.rb
tocer-3.3.0 lib/tocer/transformers/text.rb
tocer-3.2.0 lib/tocer/transformers/text.rb
tocer-3.1.1 lib/tocer/transformers/text.rb
tocer-3.1.0 lib/tocer/transformers/text.rb