Skip to content

Commit

Permalink
Fix hashtag parsing on URLs with fragments (#9221) (#9326)
Browse files Browse the repository at this point in the history
* Fix hashtag parsing on URLs with fragments

* Fix spec
  • Loading branch information
andreslucena committed May 19, 2022
1 parent 598e926 commit 3b7cab8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion decidim-core/lib/decidim/content_parsers/hashtag_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class HashtagParser < BaseParser

# Matches a hashtag if it starts with a letter or number
# and only contains letters, numbers or underscores.
HASHTAG_REGEX = /\B#([[:alnum:]](?:[[:alnum:]]|_)*)\b/i.freeze
HASHTAG_REGEX = /\s\K\B#([[:alnum:]](?:[[:alnum:]]|_)*)\b/i.freeze

# Replaces hashtags name with new or existing hashtags models global ids.
#
Expand Down
20 changes: 20 additions & 0 deletions decidim-core/spec/content_parsers/decidim/hashtag_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,25 @@ module Decidim

it_behaves_like "find and stores the hashtags references"
end

context "when content contains an URL with a fragment (aka anchor link)" do
let(:content) { "You can add an URL and this shouldn't be parsed http://www.example.org/path##{hashtag.name}" }
let(:parsed_content) { "You can add an URL and this shouldn't be parsed http://www.example.org/path#fragment" }

it "doesn't find the hashtag" do
expect(parser.metadata).to be_a(Decidim::ContentParsers::HashtagParser::Metadata)
expect(parser.metadata.hashtags).to eq([])
end
end

context "when written with an slash before the fragment" do
let(:content) { "You can add an URL and this shouldn't be parsed http://www.example.org/path/#fragment" }
let(:parsed_content) { "You can add an URL and this shouldn't be parsed http://www.example.org/path/#fragment" }

it "doesn't find the hashtag" do
expect(parser.metadata).to be_a(Decidim::ContentParsers::HashtagParser::Metadata)
expect(parser.metadata.hashtags).to eq([])
end
end
end
end

0 comments on commit 3b7cab8

Please sign in to comment.