Commit ced4a37a186601b37121c19523955b96767d9af5

Authored by Dmitriy Zaporozhets
2 parents ff07a5ab 54bcb6cc

Merge branch 'reference_relative_links' into 'master'

Reference style links with relative links in markdown

#996

```
Adds relative links support to reference style links, eg:

[GitLab API doc directory][GitLab API directory listing]

[Maintenance][Maintenance rake task]

\[GitLab API directory listing]: doc/api/
\[Maintenance rake task]: doc/raketasks/maintenance.md
```
app/helpers/gitlab_markdown_helper.rb
... ... @@ -69,10 +69,17 @@ module GitlabMarkdownHelper
69 69 project_path_with_namespace = project.path_with_namespace
70 70 paths = extract_paths(text)
71 71 paths.each do |file_path|
72   - new_path = rebuild_path(project_path_with_namespace, file_path, requested_path, ref)
73   - # Replacing old string with a new one with brackets ]() to prevent replacing occurence of a word
74   - # e.g. If we have a markdown like [test](test) this will replace ](test) and not the word test
75   - text.gsub!("](#{file_path})", "](/#{new_path})")
  72 + original_file_path = extract(file_path)
  73 + new_path = rebuild_path(project_path_with_namespace, original_file_path, requested_path, ref)
  74 + if reference_path?(file_path)
  75 + # Replacing old string with a new one that contains updated path
  76 + # eg. [some document]: document.md will be replaced with [some document] /namespace/project/master/blob/document.md
  77 + text.gsub!(file_path, file_path.gsub(original_file_path, "/#{new_path}"))
  78 + else
  79 + # Replacing old string with a new one with brackets ]() to prevent replacing occurence of a word
  80 + # e.g. If we have a markdown like [test](test) this will replace ](test) and not the word test
  81 + text.gsub!("](#{file_path})", "](/#{new_path})")
  82 + end
76 83 end
77 84 text
78 85 end
... ... @@ -83,9 +90,11 @@ module GitlabMarkdownHelper
83 90 select_relative(paths)
84 91 end
85 92  
86   - # Split the markdown text to each line and find all paths, this will match anything with - ]("some_text")
  93 + # Split the markdown text to each line and find all paths, this will match anything with - ]("some_text") and [some text]: file.md
87 94 def pick_out_paths(markdown_text)
88   - markdown_text.split("\n").map { |text| text.scan(/\]\(([^(]+)\)/) }
  95 + inline_paths = markdown_text.split("\n").map { |text| text.scan(/\]\(([^(]+)\)/) }
  96 + reference_paths = markdown_text.split("\n").map { |text| text.scan(/\[.*\]:.*/) }
  97 + inline_paths + reference_paths
89 98 end
90 99  
91 100 # Removes any empty result produced by not matching the regexp
... ... @@ -93,12 +102,22 @@ module GitlabMarkdownHelper
93 102 paths.reject{|l| l.empty? }.flatten
94 103 end
95 104  
  105 + # If a path is a reference style link we need to omit ]:
  106 + def extract(path)
  107 + path.split("]: ").last
  108 + end
  109 +
96 110 # Reject any path that contains ignored protocol
97 111 # eg. reject "https://gitlab.org} but accept "doc/api/README.md"
98 112 def select_relative(paths)
99 113 paths.reject{|path| ignored_protocols.map{|protocol| path.include?(protocol)}.any?}
100 114 end
101 115  
  116 + # Check whether a path is a reference-style link
  117 + def reference_path?(path)
  118 + path.include?("]: ")
  119 + end
  120 +
102 121 def ignored_protocols
103 122 ["http://","https://", "ftp://", "mailto:"]
104 123 end
... ...
features/project/source/markdown_render.feature
... ... @@ -16,6 +16,18 @@ Feature: Project markdown render
16 16 And I click on Rake tasks in README
17 17 Then I should see correct directory rendered
18 18  
  19 + Scenario: I view README in master branch to see reference links to directory
  20 + Then I should see files from repository in master
  21 + And I should see rendered README which contains correct links
  22 + And I click on GitLab API doc directory in README
  23 + Then I should see correct doc/api directory rendered
  24 +
  25 + Scenario: I view README in master branch to see reference links to file
  26 + Then I should see files from repository in master
  27 + And I should see rendered README which contains correct links
  28 + And I click on Maintenance in README
  29 + Then I should see correct maintenance file rendered
  30 +
19 31 Scenario: I navigate to doc directory to view documentation in master
20 32 And I navigate to the doc/api/README
21 33 And I see correct file rendered
... ...
features/steps/project/project_markdown_render.rb
... ... @@ -21,6 +21,8 @@ class Spinach::Features::ProjectMarkdownRender < Spinach::FeatureSteps
21 21 page.should have_link "GitLab API website"
22 22 page.should have_link "Rake tasks"
23 23 page.should have_link "backup and restore procedure"
  24 + page.should have_link "GitLab API doc directory"
  25 + page.should have_link "Maintenance"
24 26 end
25 27  
26 28 And 'I click on Gitlab API in README' do
... ... @@ -42,6 +44,26 @@ class Spinach::Features::ProjectMarkdownRender < Spinach::FeatureSteps
42 44 page.should have_content "maintenance.md"
43 45 end
44 46  
  47 +
  48 + And 'I click on GitLab API doc directory in README' do
  49 + click_link "GitLab API doc directory"
  50 + end
  51 +
  52 + Then 'I should see correct doc/api directory rendered' do
  53 + current_path.should == project_tree_path(@project, "master/doc/api/")
  54 + page.should have_content "README.md"
  55 + page.should have_content "users.md"
  56 + end
  57 +
  58 + And 'I click on Maintenance in README' do
  59 + click_link "Maintenance"
  60 + end
  61 +
  62 + Then 'I should see correct maintenance file rendered' do
  63 + current_path.should == project_blob_path(@project, "master/doc/raketasks/maintenance.md")
  64 + page.should have_content "bundle exec rake gitlab:env:info RAILS_ENV=production"
  65 + end
  66 +
45 67 And 'I navigate to the doc/api/README' do
46 68 click_link "doc"
47 69 click_link "api"
... ...
spec/helpers/gitlab_markdown_helper_spec.rb
... ... @@ -431,6 +431,24 @@ describe GitlabMarkdownHelper do
431 431 expected = "<p><a href=\"/#{project.path_with_namespace}/wikis/test/link\">Link</a></p>\n"
432 432 markdown(actual).should match(expected)
433 433 end
  434 +
  435 + it "should handle relative urls in reference links for a file in master" do
  436 + actual = "[GitLab API doc][GitLab readme]\n [GitLab readme]: doc/api/README.md\n"
  437 + expected = "<p><a href=\"/#{project.path_with_namespace}/blob/master/doc/api/README.md\">GitLab API doc</a></p>\n"
  438 + markdown(actual).should match(expected)
  439 + end
  440 +
  441 + it "should handle relative urls in reference links for a directory in master" do
  442 + actual = "[GitLab API doc directory][GitLab readmes]\n [GitLab readmes]: doc/api/\n"
  443 + expected = "<p><a href=\"/#{project.path_with_namespace}/tree/master/doc/api/\">GitLab API doc directory</a></p>\n"
  444 + markdown(actual).should match(expected)
  445 + end
  446 +
  447 + it "should not handle malformed relative urls in reference links for a file in master" do
  448 + actual = "[GitLab readme]: doc/api/README.md\n"
  449 + expected = ""
  450 + markdown(actual).should match(expected)
  451 + end
434 452 end
435 453  
436 454 describe "#render_wiki_content" do
... ...
spec/lib/gitlab/satellite/merge_action_spec.rb
... ... @@ -2,7 +2,7 @@ require &#39;spec_helper&#39;
2 2  
3 3 describe 'Gitlab::Satellite::MergeAction' do
4 4 before(:each) do
5   - @master = ['master', 'b1e6a9dbf1c85e6616497a5e7bad9143a4bd0828']
  5 + @master = ['master', '69b34b7e9ad9f496f0ad10250be37d6265a03bba']
6 6 @one_after_stable = ['stable', '6ea87c47f0f8a24ae031c3fff17bc913889ecd00'] #this commit sha is one after stable
7 7 @wiki_branch = ['wiki', '635d3e09b72232b6e92a38de6cc184147e5bcb41'] #this is the commit sha where the wiki branch goes off from master
8 8 @conflicting_metior = ['metior', '313d96e42b313a0af5ab50fa233bf43e27118b3f'] #this branch conflicts with the wiki branch
... ...
spec/models/project_spec.rb
... ... @@ -137,16 +137,16 @@ describe Project do
137 137  
138 138 it "should close merge request if last commit from source branch was pushed to target branch" do
139 139 @merge_request.reload_code
140   - @merge_request.last_commit.id.should == "b1e6a9dbf1c85e6616497a5e7bad9143a4bd0828"
141   - project.update_merge_requests("8716fc78f3c65bbf7bcf7b574febd583bc5d2812", "b1e6a9dbf1c85e6616497a5e7bad9143a4bd0828", "refs/heads/stable", @key.user)
  140 + @merge_request.last_commit.id.should == "69b34b7e9ad9f496f0ad10250be37d6265a03bba"
  141 + project.update_merge_requests("8716fc78f3c65bbf7bcf7b574febd583bc5d2812", "69b34b7e9ad9f496f0ad10250be37d6265a03bba", "refs/heads/stable", @key.user)
142 142 @merge_request.reload
143 143 @merge_request.merged?.should be_true
144 144 end
145 145  
146 146 it "should update merge request commits with new one if pushed to source branch" do
147   - project.update_merge_requests("8716fc78f3c65bbf7bcf7b574febd583bc5d2812", "b1e6a9dbf1c85e6616497a5e7bad9143a4bd0828", "refs/heads/master", @key.user)
  147 + project.update_merge_requests("8716fc78f3c65bbf7bcf7b574febd583bc5d2812", "69b34b7e9ad9f496f0ad10250be37d6265a03bba", "refs/heads/master", @key.user)
148 148 @merge_request.reload
149   - @merge_request.last_commit.id.should == "b1e6a9dbf1c85e6616497a5e7bad9143a4bd0828"
  149 + @merge_request.last_commit.id.should == "69b34b7e9ad9f496f0ad10250be37d6265a03bba"
150 150 end
151 151 end
152 152  
... ...
spec/seed_project.tar.gz
No preview for this file type