diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb
index 4694c7fc252..e7d6e3e6bd9 100644
--- a/lib/gitlab/markdown.rb
+++ b/lib/gitlab/markdown.rb
@@ -45,12 +45,11 @@ module Gitlab
# Extract pre blocks so they are not altered
# from http://github.github.com/github-flavored-markdown/
- extractions = {}
- text.gsub!(%r{
.*?
|.*?
}m) do |match|
- md5 = Digest::MD5.hexdigest(match)
- extractions[md5] = match
- "{gfm-extraction-#{md5}}"
- end
+ text.gsub!(%r{.*?
|.*?
}m) { |match| extract_piece(match) }
+ # Extract links with probably parsable hrefs
+ text.gsub!(%r{.*?}m) { |match| extract_piece(match) }
+ # Extract images with probably parsable src
+ text.gsub!(%r{}m) { |match| extract_piece(match) }
# TODO: add popups with additional information
@@ -58,7 +57,7 @@ module Gitlab
# Insert pre block extractions
text.gsub!(/\{gfm-extraction-(\h{32})\}/) do
- extractions[$1]
+ insert_piece($1)
end
sanitize text.html_safe, attributes: ActionView::Base.sanitized_allowed_attributes + %w(id class)
@@ -66,6 +65,18 @@ module Gitlab
private
+ def extract_piece(text)
+ @extractions ||= {}
+
+ md5 = Digest::MD5.hexdigest(text)
+ @extractions[md5] = text
+ "{gfm-extraction-#{md5}}"
+ end
+
+ def insert_piece(id)
+ @extractions[id]
+ end
+
# Private: Parses text for references and emoji
#
# text - Text to parse
diff --git a/lib/redcarpet/render/gitlab_html.rb b/lib/redcarpet/render/gitlab_html.rb
index 3a430e0bf9e..4f2c86e2d41 100644
--- a/lib/redcarpet/render/gitlab_html.rb
+++ b/lib/redcarpet/render/gitlab_html.rb
@@ -27,6 +27,10 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
HTML
end
+ def link(link, title, content)
+ h.link_to_gfm(content, link, title: title)
+ end
+
def postprocess(full_document)
h.gfm(full_document)
end
diff --git a/spec/helpers/gitlab_markdown_helper_spec.rb b/spec/helpers/gitlab_markdown_helper_spec.rb
index b7e16f1ee26..6d17c4eaf16 100644
--- a/spec/helpers/gitlab_markdown_helper_spec.rb
+++ b/spec/helpers/gitlab_markdown_helper_spec.rb
@@ -1,6 +1,8 @@
require "spec_helper"
describe GitlabMarkdownHelper do
+ include ApplicationHelper
+
let!(:project) { create(:project) }
let(:user) { create(:user, username: 'gfm') }
@@ -340,6 +342,18 @@ describe GitlabMarkdownHelper do
markdown("\nDon't use `$#{snippet.id}` here.\n").should == "Don't use $#{snippet.id}
here.
\n"
end
+ it "should leave ref-like autolinks untouched" do
+ markdown("look at http://example.tld/#!#{merge_request.id}").should == "look at http://example.tld/#!#{merge_request.id}
\n"
+ end
+
+ it "should leave ref-like href of 'manual' links untouched" do
+ markdown("why not [inspect !#{merge_request.id}](http://example.tld/#!#{merge_request.id})").should == "why not inspect !#{merge_request.id}
\n"
+ end
+
+ it "should leave ref-like src of images untouched" do
+ markdown("screen shot: ![some image](http://example.tld/#!#{merge_request.id})").should == "screen shot:
\n"
+ end
+
it "should generate absolute urls for refs" do
markdown("##{issue.id}").should include(project_issue_url(project, issue))
end