Sha256: 56fcb6e55530b3d1918f2b42cdecd52cd8c65176cbc85a714f6b8b40036bebbd

Contents?: true

Size: 768 Bytes

Versions: 5

Compression:

Stored size: 768 Bytes

Contents

# typed: true
# frozen_string_literal: false

require 'htmlentities'
require 'singleton'
require 'sorbet-runtime'

module LinkHelper
  extend T::Sig

  sig { params(str: String, salt: String).returns(String) }
  def generate_id(str, salt = '')
    id = slugify(str)

    @ids ||= {}
    @ids[salt.to_s + id] ||= 0
    @ids[salt.to_s + id] += 1
    @ids[salt.to_s + id] == 1 ? id : "#{id}-#{@ids[salt.to_s + id]}"
  end

  sig { void }
  def reset_ids_generation
    @ids = {}
  end

  sig { params(string: String).returns(String) }
  def slugify(string)
    HTMLEntities.new
                .decode(string)
                .gsub(%r{</?[^>]*>}, '')
                .gsub(/\s/, '-')
                .gsub(%r{[\[\]()/",`'&<>\.*]}, '')
                .downcase
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
frontman-ssg-0.1.1 lib/frontman/helpers/link_helper.rb
frontman-ssg-0.1.0 lib/frontman/helpers/link_helper.rb
frontman-ssg-0.0.4 lib/frontman/helpers/link_helper.rb
frontman-ssg-0.0.3 lib/frontman/helpers/link_helper.rb
frontman-ssg-0.0.2 lib/frontman/helpers/link_helper.rb