diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index b711e404576..f3a4b6e4420 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -457,6 +457,9 @@ module PrettyText end end + mentions = + DiscoursePluginRegistry.apply_modifier(:pretty_text_extract_mentions, mentions, cooked) + mentions.compact! mentions.uniq! mentions diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rb index 8a117ccc991..41ad0944bc6 100644 --- a/spec/lib/pretty_text_spec.rb +++ b/spec/lib/pretty_text_spec.rb @@ -566,6 +566,45 @@ RSpec.describe PrettyText do ).to match_html '

Hello @狮子

' end end + + context "with pretty_text_extract_mentions modifier" do + it "allows changing the mentions extracted" do + cooked_html = <<~HTML +

+ @test, + @test-group, + @test-custom, + this is a test +

+ HTML + + extracted_mentions = PrettyText.extract_mentions(Nokogiri::HTML5.fragment(cooked_html)) + expect(extracted_mentions).to include("test", "test-group") + expect(extracted_mentions).not_to include("test-custom") + + Plugin::Instance + .new + .register_modifier(:pretty_text_extract_mentions) do |mentions, cooked_text| + custom_mentions = + cooked_text + .css(".custom-mention") + .map do |e| + if (name = e.inner_text) + name = name[1..-1] + name = User.normalize_username(name) + name + end + end + + mentions + custom_mentions + end + + extracted_mentions = PrettyText.extract_mentions(Nokogiri::HTML5.fragment(cooked_html)) + expect(extracted_mentions).to include("test", "test-group", "test-custom") + ensure + DiscoursePluginRegistry.clear_modifiers! + end + end end describe "code fences" do