Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hashtag parsing on URLs with fragments #9221

Merged
merged 3 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
HASHTAG_REGEX = /\s\K\B#([[:alnum:]](?:[[:alnum:]]|_)*)\b/i

# 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