Sha256: c1519cfefa2705234de42096802afc30786f64e2f012caf8d0edc3b4c0c17cbd

Contents?: true

Size: 885 Bytes

Versions: 15

Compression:

Stored size: 885 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, parser: Parsers::Header
        @parser = parser.new text
      end

      def label
        parser.content
      end

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

      def call url_suffix: ""
        "#{indented_bullet}[#{label}](##{computed_url url_suffix})"
      end

      private

      attr_reader :parser

      def computed_url suffix = ""
        [url, suffix.to_s].compress.join "-"
      end

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

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
tocer-11.1.0 lib/tocer/transformers/text.rb
tocer-11.0.0 lib/tocer/transformers/text.rb
tocer-10.5.0 lib/tocer/transformers/text.rb
tocer-10.4.1 lib/tocer/transformers/text.rb
tocer-10.4.0 lib/tocer/transformers/text.rb
tocer-10.3.0 lib/tocer/transformers/text.rb
tocer-10.2.0 lib/tocer/transformers/text.rb
tocer-10.1.1 lib/tocer/transformers/text.rb
tocer-10.1.0 lib/tocer/transformers/text.rb
tocer-10.0.1 lib/tocer/transformers/text.rb
tocer-10.0.0 lib/tocer/transformers/text.rb
tocer-9.1.2 lib/tocer/transformers/text.rb
tocer-9.1.1 lib/tocer/transformers/text.rb
tocer-9.1.0 lib/tocer/transformers/text.rb
tocer-9.0.0 lib/tocer/transformers/text.rb