Class: Decidim::ContentParsers::HashtagParser
- Inherits:
-
BaseParser
- Object
- BaseParser
- Decidim::ContentParsers::HashtagParser
- Defined in:
- decidim-core/lib/decidim/content_parsers/hashtag_parser.rb
Overview
A parser that searches hashtags in content.
A word starting with ‘#` will be considered as a hashtag if it only contains letters, numbers or underscores. If the `#` is followed with an underscore, then it is not considered.
Defined Under Namespace
Classes: Metadata
Constant Summary collapse
- HASHTAG_REGEX =
Matches a hashtag if it starts with a letter or number and only contains letters, numbers or underscores.
/(?:\A|\s\K)\B#([[:alnum:]](?:[[:alnum:]]|_)*)\b/i
Constants inherited from BaseParser
Instance Attribute Summary
Attributes inherited from BaseParser
Instance Method Summary collapse
- #metadata ⇒ Object
-
#rewrite ⇒ String
Replaces hashtags name with new or existing hashtags models global ids.
Methods inherited from BaseParser
Constructor Details
This class inherits a constructor from Decidim::ContentParsers::BaseParser
Instance Method Details
#metadata ⇒ Object
32 33 34 |
# File 'decidim-core/lib/decidim/content_parsers/hashtag_parser.rb', line 32 def Metadata.new(.map { |content_hashtag| hashtag(content_hashtag) }.uniq) end |
#rewrite ⇒ String
Replaces hashtags name with new or existing hashtags models global ids.
26 27 28 29 30 |
# File 'decidim-core/lib/decidim/content_parsers/hashtag_parser.rb', line 26 def rewrite content.gsub(HASHTAG_REGEX) do |match| "#{hashtag(match[1..-1]).to_global_id}/#{ ? "_" : ""}#{match[1..-1]}" end end |