Commit 6873d07f62f51686f71ac7d31a06ae2124062235

Authored by Riyad Preukschas
1 parent 8ce39033

Add link_to_gfm helper and specs

app/helpers/application_helper.rb
@@ -110,6 +110,17 @@ module ApplicationHelper @@ -110,6 +110,17 @@ module ApplicationHelper
110 text.html_safe 110 text.html_safe
111 end 111 end
112 112
  113 + # circumvents nesting links, which will behave bad in browsers
  114 + def link_to_gfm(body, url, html_options = {})
  115 + gfm_body = gfm(body, html_options)
  116 +
  117 + gfm_body.gsub!(%r{<a.*?>.*?</a>}m) do |match|
  118 + "</a>#{match}#{link_to("", url, html_options)[0..-5]}" # "</a>".length +1
  119 + end
  120 +
  121 + link_to(gfm_body.html_safe, url, html_options)
  122 + end
  123 +
113 def markdown(text) 124 def markdown(text)
114 @__renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::GitlabHTML.new(self, filter_html: true), { 125 @__renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::GitlabHTML.new(self, filter_html: true), {
115 no_intra_emphasis: true, 126 no_intra_emphasis: true,
spec/helpers/gitlab_flavored_markdown_spec.rb
@@ -163,4 +163,17 @@ describe ApplicationHelper do @@ -163,4 +163,17 @@ describe ApplicationHelper do
163 gfm("fixed in #{@commit.id}", :class => "foo").should have_selector("a.foo") 163 gfm("fixed in #{@commit.id}", :class => "foo").should have_selector("a.foo")
164 end 164 end
165 end 165 end
  166 +
  167 + describe "#link_to_gfm" do
  168 + let(:issue1) { Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project }
  169 + let(:issue2) { Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project }
  170 +
  171 + it "should handle references nested in links with all the text" do
  172 + link_to_gfm("This should finally fix ##{issue1.id} and ##{issue2.id} for real", project_commit_path(@project, :id => @commit.id)).should == "#{link_to "This should finally fix ", project_commit_path(@project, :id => @commit.id)}#{link_to "##{issue1.id}", project_issue_path(@project, issue1), :title => "Issue: #{issue1.title}", :class => "gfm gfm-issue "}#{link_to " and ", project_commit_path(@project, :id => @commit.id)}#{link_to "##{issue2.id}", project_issue_path(@project, issue2), :title => "Issue: #{issue2.title}", :class => "gfm gfm-issue "}#{link_to " for real", project_commit_path(@project, :id => @commit.id)}"
  173 + end
  174 +
  175 + it "should forward HTML options" do
  176 + link_to_gfm("This should finally fix ##{issue1.id} for real", project_commit_path(@project, :id => @commit.id), :class => "foo").should have_selector(".foo")
  177 + end
  178 + end
166 end 179 end