Skip to content

Commit

Permalink
Get numerical hashes like #123 to link to notes, not generate tag links
Browse files Browse the repository at this point in the history
#1825 (#1826)

* Updated constant.rb

* Updated answer.rb

* Updated comment.rb

* Updated revision.rb

* Added Test for HASHTAGNUMBER regex
  • Loading branch information
sagarpreet-chadha authored and jywarren committed Dec 7, 2017
1 parent 3dfcaa9 commit dd2f01b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class Answer < ActiveRecord::Base

def body
finder = content.gsub(Callouts.const_get(:FINDER), Callouts.const_get(:PRETTYLINKMD))
finder.gsub(Callouts.const_get(:HASHTAG), Callouts.const_get(:HASHLINKMD))
finder = finder.gsub(Callouts.const_get(:HASHTAGNUMBER), Callouts.const_get(:NODELINKMD))
finder = finder.gsub(Callouts.const_get(:HASHTAG), Callouts.const_get(:HASHLINKMD))
end

# users who like this answer
Expand Down
3 changes: 2 additions & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def created_at

def body
finder = comment.gsub(Callouts.const_get(:FINDER), Callouts.const_get(:PRETTYLINKMD))
finder.gsub(Callouts.const_get(:HASHTAG), Callouts.const_get(:HASHLINKMD))
finder = finder.gsub(Callouts.const_get(:HASHTAGNUMBER), Callouts.const_get(:NODELINKMD))
finder = finder.gsub(Callouts.const_get(:HASHTAG), Callouts.const_get(:HASHLINKMD))
end

def icon
Expand Down
1 change: 1 addition & 0 deletions app/models/revision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def render_body
body = RDiscount.new(body)
body = body.to_html
body = body.gsub(Callouts.const_get(:FINDER), Callouts.const_get(:PRETTYLINKHTML))
body = body.gsub(Callouts.const_get(:HASHTAGNUMBER), Callouts.const_get(:NODELINKHTML))
body = body.gsub(Callouts.const_get(:HASHTAG), Callouts.const_get(:HASHLINKHTML))
body_extras(body)
end
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module Callouts
HASHTAG = /(\s)\#([:a-zA-Z0-9_-]+)/
PRETTYLINKMD = '\1[@\2](/profile/\2)'
HASHLINKMD = '\1[#\2](/tag/\2)'
HASHTAGNUMBER = /(\s)\#([:0-9]+)/
NODELINKMD = '\1[#\2](/n/\2)'
PRETTYLINKHTML = '\1<a href="/profile/\2">@\2</a>'
HASHLINKHTML = '\1<a href="/tag/\2">#\2</a>'
NODELINKHTML = '\1<a href="/n/\2">#\2</a>'
end
7 changes: 7 additions & 0 deletions test/unit/constants_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ class ConstantsTest < ActiveSupport::TestCase
string = 'this is a #tag #anotheRTag, #question:spectrometer'
assert string.scan(Callouts.const_get(:HASHTAG)).length == 3
end

test 'hashtagnumber regex should match non-initial hash' do
string = "hello #123 #hello " ;
assert string.scan(Callouts.const_get(:HASHTAGNUMBER)).length == 1
expect = "hello REPLACE #hello " ;
assert_equal expect, string.gsub(Callouts.const_get(:HASHTAGNUMBER), ' REPLACE') ;
end
end

0 comments on commit dd2f01b

Please sign in to comment.