Fix markdown rendering for label references that begin with a digit

parent c88075f4
...@@ -52,11 +52,14 @@ class Label < ActiveRecord::Base ...@@ -52,11 +52,14 @@ class Label < ActiveRecord::Base
# This pattern supports cross-project references. # This pattern supports cross-project references.
# #
def self.reference_pattern def self.reference_pattern
# NOTE: The id pattern only matches when all characters on the expression
# are digits, so it will match ~2 but not ~2fa because that's probably a
# label name and we want it to be matched as such.
@reference_pattern ||= %r{ @reference_pattern ||= %r{
(#{Project.reference_pattern})? (#{Project.reference_pattern})?
#{Regexp.escape(reference_prefix)} #{Regexp.escape(reference_prefix)}
(?: (?:
(?<label_id>\d+) | # Integer-based label ID, or (?<label_id>\d+(?!\S\w)\b) | # Integer-based label ID, or
(?<label_name> (?<label_name>
[A-Za-z0-9_\-\?&]+ | # String-based single-word label title, or [A-Za-z0-9_\-\?&]+ | # String-based single-word label title, or
"([^"]*)" # String-based multi-word label surrounded in quotes "([^"]*)" # String-based multi-word label surrounded in quotes
......
...@@ -104,6 +104,30 @@ describe Banzai::Filter::LabelReferenceFilter, lib: true do ...@@ -104,6 +104,30 @@ describe Banzai::Filter::LabelReferenceFilter, lib: true do
end end
end end
context 'String-based single-word references that begin with a digit' do
let(:label) { create(:label, name: '2fa', project: project) }
let(:reference) { "#{Label.reference_prefix}#{label.name}" }
it 'links to a valid reference' do
doc = reference_filter("See #{reference}")
expect(doc.css('a').first.attr('href')).to eq urls.
namespace_project_issues_url(project.namespace, project, label_name: label.name)
expect(doc.text).to eq 'See 2fa'
end
it 'links with adjacent text' do
doc = reference_filter("Label (#{reference}.)")
expect(doc.to_html).to match(%r(\(<a.+><span.+>#{label.name}</span></a>\.\)))
end
it 'ignores invalid label names' do
exp = act = "Label #{Label.reference_prefix}#{label.id}#{label.name.reverse}"
expect(reference_filter(act).to_html).to eq exp
end
end
context 'String-based single-word references with special characters' do context 'String-based single-word references with special characters' do
let(:label) { create(:label, name: '?gfm&', project: project) } let(:label) { create(:label, name: '?gfm&', project: project) }
let(:reference) { "#{Label.reference_prefix}#{label.name}" } let(:reference) { "#{Label.reference_prefix}#{label.name}" }
...@@ -153,6 +177,30 @@ describe Banzai::Filter::LabelReferenceFilter, lib: true do ...@@ -153,6 +177,30 @@ describe Banzai::Filter::LabelReferenceFilter, lib: true do
end end
end end
context 'String-based multi-word references that begin with a digit' do
let(:label) { create(:label, name: '2 factor authentication', project: project) }
let(:reference) { label.to_reference(format: :name) }
it 'links to a valid reference' do
doc = reference_filter("See #{reference}")
expect(doc.css('a').first.attr('href')).to eq urls.
namespace_project_issues_url(project.namespace, project, label_name: label.name)
expect(doc.text).to eq 'See 2 factor authentication'
end
it 'links with adjacent text' do
doc = reference_filter("Label (#{reference}.)")
expect(doc.to_html).to match(%r(\(<a.+><span.+>#{label.name}</span></a>\.\)))
end
it 'ignores invalid label names' do
exp = act = "Label #{Label.reference_prefix}#{label.id}#{label.name.reverse}"
expect(reference_filter(act).to_html).to eq exp
end
end
context 'String-based multi-word references with special characters in quotes' do context 'String-based multi-word references with special characters in quotes' do
let(:label) { create(:label, name: 'gfm & references?', project: project) } let(:label) { create(:label, name: 'gfm & references?', project: project) }
let(:reference) { label.to_reference(format: :name) } let(:reference) { label.to_reference(format: :name) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment