Add link_to_gfm helper and specs

This commit is contained in:
Riyad Preukschas 2012-08-02 02:28:50 +02:00
parent 8ce390333a
commit 6873d07f62
2 changed files with 24 additions and 0 deletions

View file

@ -110,6 +110,17 @@ module ApplicationHelper
text.html_safe text.html_safe
end end
# circumvents nesting links, which will behave bad in browsers
def link_to_gfm(body, url, html_options = {})
gfm_body = gfm(body, html_options)
gfm_body.gsub!(%r{<a.*?>.*?</a>}m) do |match|
"</a>#{match}#{link_to("", url, html_options)[0..-5]}" # "</a>".length +1
end
link_to(gfm_body.html_safe, url, html_options)
end
def markdown(text) def markdown(text)
@__renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::GitlabHTML.new(self, filter_html: true), { @__renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::GitlabHTML.new(self, filter_html: true), {
no_intra_emphasis: true, no_intra_emphasis: true,

View file

@ -163,4 +163,17 @@ describe ApplicationHelper do
gfm("fixed in #{@commit.id}", :class => "foo").should have_selector("a.foo") gfm("fixed in #{@commit.id}", :class => "foo").should have_selector("a.foo")
end end
end end
describe "#link_to_gfm" do
let(:issue1) { Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project }
let(:issue2) { Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project }
it "should handle references nested in links with all the text" do
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)}"
end
it "should forward HTML options" do
link_to_gfm("This should finally fix ##{issue1.id} for real", project_commit_path(@project, :id => @commit.id), :class => "foo").should have_selector(".foo")
end
end
end end