From dd2f01bcac6de88c476416ecf8b4dec603299964 Mon Sep 17 00:00:00 2001 From: Sagarpreet Chadha Date: Thu, 7 Dec 2017 18:03:28 +0530 Subject: [PATCH] Get numerical hashes like #123 to link to notes, not generate tag links #1825 (#1826) * Updated constant.rb * Updated answer.rb * Updated comment.rb * Updated revision.rb * Added Test for HASHTAGNUMBER regex --- app/models/answer.rb | 3 ++- app/models/comment.rb | 3 ++- app/models/revision.rb | 1 + config/initializers/constants.rb | 3 +++ test/unit/constants_test.rb | 7 +++++++ 5 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/models/answer.rb b/app/models/answer.rb index 8da4976642..b1240ea6be 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index 08af659240..4cef0b18fa 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/models/revision.rb b/app/models/revision.rb index 7688c1b93b..3392afb346 100644 --- a/app/models/revision.rb +++ b/app/models/revision.rb @@ -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 diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index 165b9ff0bc..2af0b3de41 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -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@\2' HASHLINKHTML = '\1#\2' + NODELINKHTML = '\1#\2' end diff --git a/test/unit/constants_test.rb b/test/unit/constants_test.rb index a694508124..184e111888 100644 --- a/test/unit/constants_test.rb +++ b/test/unit/constants_test.rb @@ -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