Sha256: 6a70dd4739bc1af40af13665b7944e8fbfdd454baebec54a4d4a253403f8766e

Contents?: true

Size: 792 Bytes

Versions: 21

Compression:

Stored size: 792 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

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

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

      private

      attr_reader :parser

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

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

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

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
tocer-14.5.0 lib/tocer/transformers/text.rb
tocer-14.4.0 lib/tocer/transformers/text.rb
tocer-14.3.0 lib/tocer/transformers/text.rb
tocer-14.2.0 lib/tocer/transformers/text.rb
tocer-14.1.0 lib/tocer/transformers/text.rb
tocer-14.0.1 lib/tocer/transformers/text.rb
tocer-14.0.0 lib/tocer/transformers/text.rb
tocer-13.3.1 lib/tocer/transformers/text.rb
tocer-13.3.0 lib/tocer/transformers/text.rb
tocer-13.2.0 lib/tocer/transformers/text.rb
tocer-13.1.0 lib/tocer/transformers/text.rb
tocer-13.0.2 lib/tocer/transformers/text.rb
tocer-13.0.1 lib/tocer/transformers/text.rb
tocer-13.0.0 lib/tocer/transformers/text.rb
tocer-12.2.0 lib/tocer/transformers/text.rb
tocer-12.1.0 lib/tocer/transformers/text.rb
tocer-12.0.4 lib/tocer/transformers/text.rb
tocer-12.0.3 lib/tocer/transformers/text.rb
tocer-12.0.2 lib/tocer/transformers/text.rb
tocer-12.0.1 lib/tocer/transformers/text.rb