Sha256: c374d23c65a7609f5fdd32f890d9e04fecd0c62c22b34e7c041ca3e0bc0ce009

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module Decidim
  module ContentParsers
    # A parser that searches user mentions in content.
    #
    # A word starting with `#` will be considered as a possible hashtagging if
    # they only contains letters, numbers or underscores. If the `#` is
    # followed with an underscore, then it is not considered.
    #
    # @see BaseParser Examples of how to use a content parser
    class HashtagParser < BaseParser
      Metadata = Struct.new(:hashtags)

      # Replaces found hashtags matching a name of an existing
      # hashtag with a global id.
      #
      # @return [String] the content with the hashtags replaced by a global id
      def rewrite
        content.gsub(Decidim::Hashtag::HASHTAG_REGEX) do |match|
          if (hashtag = Decidim::Hashtag.find_or_create_by(organization: context[:current_organization], name: Regexp.last_match[2].downcase))
            Regexp.last_match[1] + hashtag.to_global_id.to_s
          else
            match
          end
        end
      end

      def metadata
        Metadata.new(
          Decidim::Hashtag.where(organization: context[:current_organization], name: content.scan(Decidim::Hashtag::HASHTAG_REGEX).flatten)
        )
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-core-0.15.2 lib/decidim/content_parsers/hashtag_parser.rb
decidim-core-0.15.1 lib/decidim/content_parsers/hashtag_parser.rb
decidim-core-0.15.0 lib/decidim/content_parsers/hashtag_parser.rb
decidim-core-0.14.4 lib/decidim/content_parsers/hashtag_parser.rb
decidim-core-0.14.3 lib/decidim/content_parsers/hashtag_parser.rb
decidim-core-0.14.2 lib/decidim/content_parsers/hashtag_parser.rb
decidim-core-0.14.1 lib/decidim/content_parsers/hashtag_parser.rb