diff --git a/app/helpers/gitlab_markdown_helper.rb b/app/helpers/gitlab_markdown_helper.rb index db5b4eacd1f8b4be75c6891319c54778593f46cd..c7f769cee6459043f0e36a7a40aae2bf1e4a80b8 100644 --- a/app/helpers/gitlab_markdown_helper.rb +++ b/app/helpers/gitlab_markdown_helper.rb @@ -63,10 +63,14 @@ module GitlabMarkdownHelper paths = extract_paths(text) paths.uniq.each do |file_path| - new_path = rebuild_path(file_path) - # 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 - text.gsub!("\"#{file_path}\"", "\"/#{new_path}\"") + # If project does not have repository + # its nothing to rebuild + if @repository.exists? && !@repository.empty? + new_path = rebuild_path(file_path) + # 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 + text.gsub!("\"#{file_path}\"", "\"/#{new_path}\"") + end end text @@ -91,7 +95,12 @@ module GitlabMarkdownHelper end def link_to_ignore?(link) - ignored_protocols.map{ |protocol| link.include?(protocol) }.any? + if link =~ /\#\w+/ + # ignore anchors like + true + else + ignored_protocols.map{ |protocol| link.include?(protocol) }.any? + end end def ignored_protocols @@ -169,7 +178,7 @@ module GitlabMarkdownHelper def current_sha if @commit @commit.id - else + elsif @repository && !@repository.empty? @repository.head_commit.sha end end diff --git a/spec/helpers/gitlab_markdown_helper_spec.rb b/spec/helpers/gitlab_markdown_helper_spec.rb index fc9d1ac90c0bb1bbab94beb93aadc88e470a5dd6..f176a393415d05a638b2fe0922f10acdf403119a 100644 --- a/spec/helpers/gitlab_markdown_helper_spec.rb +++ b/spec/helpers/gitlab_markdown_helper_spec.rb @@ -5,6 +5,7 @@ describe GitlabMarkdownHelper do include IssuesHelper let!(:project) { create(:project) } + let(:empty_project) { create(:empty_project) } let(:user) { create(:user, username: 'gfm') } let(:commit) { project.repository.commit } @@ -506,6 +507,19 @@ describe GitlabMarkdownHelper do 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 = "

GitLab API doc

\n" + markdown(actual).should match(expected) + end + end + describe "#render_wiki_content" do before do @wiki = double('WikiPage')