Commit 2bc78739 authored by Riyad Preukschas's avatar Riyad Preukschas

Fix parsing of ref-like Urls in links and images in GFM

Fixes #2166
parent 16b54178
...@@ -45,12 +45,11 @@ module Gitlab ...@@ -45,12 +45,11 @@ module Gitlab
# Extract pre blocks so they are not altered # Extract pre blocks so they are not altered
# from http://github.github.com/github-flavored-markdown/ # from http://github.github.com/github-flavored-markdown/
extractions = {} text.gsub!(%r{<pre>.*?</pre>|<code>.*?</code>}m) { |match| extract_piece(match) }
text.gsub!(%r{<pre>.*?</pre>|<code>.*?</code>}m) do |match| # Extract links with probably parsable hrefs
md5 = Digest::MD5.hexdigest(match) text.gsub!(%r{<a.*?>.*?</a>}m) { |match| extract_piece(match) }
extractions[md5] = match # Extract images with probably parsable src
"{gfm-extraction-#{md5}}" text.gsub!(%r{<img.*?>}m) { |match| extract_piece(match) }
end
# TODO: add popups with additional information # TODO: add popups with additional information
...@@ -58,7 +57,7 @@ module Gitlab ...@@ -58,7 +57,7 @@ module Gitlab
# Insert pre block extractions # Insert pre block extractions
text.gsub!(/\{gfm-extraction-(\h{32})\}/) do text.gsub!(/\{gfm-extraction-(\h{32})\}/) do
extractions[$1] insert_piece($1)
end end
sanitize text.html_safe, attributes: ActionView::Base.sanitized_allowed_attributes + %w(id class) sanitize text.html_safe, attributes: ActionView::Base.sanitized_allowed_attributes + %w(id class)
...@@ -66,6 +65,18 @@ module Gitlab ...@@ -66,6 +65,18 @@ module Gitlab
private 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 # Private: Parses text for references and emoji
# #
# text - Text to parse # text - Text to parse
......
...@@ -27,6 +27,10 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML ...@@ -27,6 +27,10 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
HTML HTML
end end
def link(link, title, content)
h.link_to_gfm(content, link, title: title)
end
def postprocess(full_document) def postprocess(full_document)
h.gfm(full_document) h.gfm(full_document)
end end
......
require "spec_helper" require "spec_helper"
describe GitlabMarkdownHelper do describe GitlabMarkdownHelper do
include ApplicationHelper
let!(:project) { create(:project) } let!(:project) { create(:project) }
let(:user) { create(:user, username: 'gfm') } let(:user) { create(:user, username: 'gfm') }
...@@ -340,6 +342,18 @@ describe GitlabMarkdownHelper do ...@@ -340,6 +342,18 @@ describe GitlabMarkdownHelper do
markdown("\nDon't use `$#{snippet.id}` here.\n").should == "<p>Don&#39;t use <code>$#{snippet.id}</code> here.</p>\n" markdown("\nDon't use `$#{snippet.id}` here.\n").should == "<p>Don&#39;t use <code>$#{snippet.id}</code> here.</p>\n"
end end
it "should leave ref-like autolinks untouched" do
markdown("look at http://example.tld/#!#{merge_request.id}").should == "<p>look at <a href=\"http://example.tld/#!#{merge_request.id}\">http://example.tld/#!#{merge_request.id}</a></p>\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 == "<p>why not <a href=\"http://example.tld/#!#{merge_request.id}\">inspect </a><a href=\"http://test.host/project60/merge_requests/#{merge_request.id}\" class=\"gfm gfm-merge_request \" title=\"Merge Request: #{merge_request.title}\">!#{merge_request.id}</a><a href=\"http://example.tld/#!#{merge_request.id}\"></a></p>\n"
end
it "should leave ref-like src of images untouched" do
markdown("screen shot: ![some image](http://example.tld/#!#{merge_request.id})").should == "<p>screen shot: <img src=\"http://example.tld/#!#{merge_request.id}\" alt=\"some image\"></p>\n"
end
it "should generate absolute urls for refs" do it "should generate absolute urls for refs" do
markdown("##{issue.id}").should include(project_issue_url(project, issue)) markdown("##{issue.id}").should include(project_issue_url(project, issue))
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment