Sha256: e89fb249b5fe234db83035f0f1656a51d04a34f09ad2baf6e153456780720269
Contents?: true
Size: 1.5 KB
Versions: 3
Compression:
Stored size: 1.5 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 mention 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 UserParser < BaseParser # Class used as a container for metadata # # @!attribute users # @return [Array] an array of Decidim::User mentioned in content Metadata = Struct.new(:users) # Matches a nickname if they start with a letter or number # and only contains letters, numbers or underscores. MENTION_REGEX = /(^|\s)@([a-zA-Z0-9]\w*)/ # Replaces found mentions matching a nickname of an existing # user with a global id. Other mentions found that doesn't # match an existing user are returned as is. # # @return [String] the content with the valid mentions replaced by a global id def rewrite content.gsub(MENTION_REGEX) do |match| if (user = Decidim::User.find_by(nickname: Regexp.last_match[2])) Regexp.last_match[1] + user.to_global_id.to_s else match end end end # (see BaseParser#metadata) def metadata Metadata.new( Decidim::User.where(nickname: content.scan(MENTION_REGEX).flatten) ) end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
decidim-core-0.9.2 | lib/decidim/content_parsers/user_parser.rb |
decidim-core-0.9.1 | lib/decidim/content_parsers/user_parser.rb |
decidim-core-0.9.0 | lib/decidim/content_parsers/user_parser.rb |