Sha256: 8c4a424c97502ac2a14c6ace965dcbc98db1ab3a0a8925ad0643accf869c7b25

Contents?: true

Size: 1.04 KB

Versions: 13

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require "core"
require "refinements/array"

module Tocer
  module Transformers
    # Transforms a Markdown header (embedded link) into a table of contents link.
    class Link
      using Refinements::Array

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

      def label = parser.content.gsub(embedded_link, embedded_link_label)

      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 = Core::EMPTY_STRING) = [url, suffix.to_s].compress.join("-")

      def embedded_link = "[#{embedded_link_label}](#{embedded_link_url})"

      def embedded_link_label = parser.content[/\[(.*)\]/, 1]

      def embedded_link_url = parser.content[/\((.*)\)/, 1]

      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

13 entries across 13 versions & 1 rubygems

Version Path
tocer-18.7.0 lib/tocer/transformers/link.rb
tocer-18.6.0 lib/tocer/transformers/link.rb
tocer-18.5.0 lib/tocer/transformers/link.rb
tocer-18.4.0 lib/tocer/transformers/link.rb
tocer-18.3.0 lib/tocer/transformers/link.rb
tocer-18.2.0 lib/tocer/transformers/link.rb
tocer-18.1.0 lib/tocer/transformers/link.rb
tocer-18.0.0 lib/tocer/transformers/link.rb
tocer-17.5.0 lib/tocer/transformers/link.rb
tocer-17.3.0 lib/tocer/transformers/link.rb
tocer-17.2.0 lib/tocer/transformers/link.rb
tocer-17.1.0 lib/tocer/transformers/link.rb
tocer-17.0.0 lib/tocer/transformers/link.rb