Commit 1e673c72 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Improve markdown parsing

* dont try to build links for anchors like `#title-anchor`
* dont build relative url for project with empty repo
Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 4d482b3b
...@@ -63,11 +63,15 @@ module GitlabMarkdownHelper ...@@ -63,11 +63,15 @@ module GitlabMarkdownHelper
paths = extract_paths(text) paths = extract_paths(text)
paths.uniq.each do |file_path| paths.uniq.each do |file_path|
# If project does not have repository
# its nothing to rebuild
if @repository.exists? && !@repository.empty?
new_path = rebuild_path(file_path) new_path = rebuild_path(file_path)
# Finds quoted path so we don't replace other mentions of the string # Finds quoted path so we don't replace other mentions of the string
# eg. "doc/api" will be replaced and "/home/doc/api/text" won't # eg. "doc/api" will be replaced and "/home/doc/api/text" won't
text.gsub!("\"#{file_path}\"", "\"/#{new_path}\"") text.gsub!("\"#{file_path}\"", "\"/#{new_path}\"")
end end
end
text text
end end
...@@ -91,8 +95,13 @@ module GitlabMarkdownHelper ...@@ -91,8 +95,13 @@ module GitlabMarkdownHelper
end end
def link_to_ignore?(link) def link_to_ignore?(link)
if link =~ /\#\w+/
# ignore anchors like <a href="#my-header">
true
else
ignored_protocols.map{ |protocol| link.include?(protocol) }.any? ignored_protocols.map{ |protocol| link.include?(protocol) }.any?
end end
end
def ignored_protocols def ignored_protocols
["http://","https://", "ftp://", "mailto:"] ["http://","https://", "ftp://", "mailto:"]
...@@ -169,7 +178,7 @@ module GitlabMarkdownHelper ...@@ -169,7 +178,7 @@ module GitlabMarkdownHelper
def current_sha def current_sha
if @commit if @commit
@commit.id @commit.id
else elsif @repository && !@repository.empty?
@repository.head_commit.sha @repository.head_commit.sha
end end
end end
......
...@@ -5,6 +5,7 @@ describe GitlabMarkdownHelper do ...@@ -5,6 +5,7 @@ describe GitlabMarkdownHelper do
include IssuesHelper include IssuesHelper
let!(:project) { create(:project) } let!(:project) { create(:project) }
let(:empty_project) { create(:empty_project) }
let(:user) { create(:user, username: 'gfm') } let(:user) { create(:user, username: 'gfm') }
let(:commit) { project.repository.commit } let(:commit) { project.repository.commit }
...@@ -506,6 +507,19 @@ describe GitlabMarkdownHelper do ...@@ -506,6 +507,19 @@ describe GitlabMarkdownHelper do
end end
end end
describe "markdwon for empty repository" do
before do
@project = empty_project
@repository = empty_project.repository
end
it "should not touch relative urls" do
actual = "[GitLab API doc][GitLab readme]\n [GitLab readme]: doc/api/README.md\n"
expected = "<p><a href=\"doc/api/README.md\">GitLab API doc</a></p>\n"
markdown(actual).should match(expected)
end
end
describe "#render_wiki_content" do describe "#render_wiki_content" do
before do before do
@wiki = double('WikiPage') @wiki = double('WikiPage')
......
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