Sha256: a986ed380f372bc0d653efa3a1715c4481e6f996f73eb2c84ad42d87b8efe61c

Contents?: true

Size: 840 Bytes

Versions: 2

Compression:

Stored size: 840 Bytes

Contents

require "refinements/array_extensions"

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

      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

2 entries across 2 versions & 1 rubygems

Version Path
tocer-2.0.0 lib/tocer/transformers/text.rb
tocer-1.0.0 lib/tocer/transformers/text.rb