class Qiita::Markdown::Filters::Mention
-
Adds :mentioned_usernames into result Hash as Array of String.
-
Replaces @mention with link.
You can pass :allowed_usernames context to limit mentioned usernames.
Constants
- IGNORE_PARENTS
- MentionPattern
Public Instance Methods
call()
click to toggle source
@note Override to use another IGNORE_PARENTS
# File lib/qiita/markdown/filters/mention.rb, line 24 def call result[:mentioned_usernames] ||= [] doc.search(".//text()").each do |node| content = node.to_html next unless content.include?("@") next if has_ancestor?(node, IGNORE_PARENTS) html = mention_link_filter(content, base_url, info_url, username_pattern) next if html == content node.replace(html) end doc end
mention_link_filter(text, _, _, _)
click to toggle source
@note Override to use customized MentionPattern
and allowed_usernames
logic.
# File lib/qiita/markdown/filters/mention.rb, line 41 def mention_link_filter(text, _, _, _) text.gsub(MentionPattern) do |match| name = ::Regexp.last_match(1) case when allowed_usernames && name == "all" result[:mentioned_usernames] |= allowed_usernames match.sub( "@#{name}", %(<a href="/" class="user-mention" title="#{name}">@#{name}</a>), ) when (allowed_usernames && !allowed_usernames.include?(name)) || name == "all" match else result[:mentioned_usernames] |= [name] url = File.join(base_url, name) match.sub( "@#{name}", %(<a href="#{url}" class="user-mention js-hovercard" title="#{name}" data-hovercard-target-type="user" data-hovercard-target-name="#{name}">@#{name}</a>), ) end end end
Private Instance Methods
allowed_usernames()
click to toggle source
# File lib/qiita/markdown/filters/mention.rb, line 66 def allowed_usernames context[:allowed_usernames] end
has_ancestor?(node, tags)
click to toggle source
Calls superclass method
# File lib/qiita/markdown/filters/mention.rb, line 70 def has_ancestor?(node, tags) super || (node.parent.parent && node.parent.parent["class"] == "code-lang") end