Commit 9f20580ed7338e72ffeadff86c0d605a2802c957

Authored by Dmitriy Zaporozhets
2 parents e1c00d53 90bdcac6

Merge pull request #6375 from cirosantilli/link-with-id

Blob and tree gfm links to anchors work.
CHANGELOG
... ... @@ -32,6 +32,7 @@ v 6.6.0
32 32 - Restyle Issue#show page and MR#show page
33 33 - Ability to filter by multiple labels for Issues page
34 34 - Rails version to 4.0.3
  35 + - Blob and tree gfm links to anchors work
35 36  
36 37 v 6.5.1
37 38 - Fix branch selectbox when create merge request from fork
... ...
app/helpers/gitlab_markdown_helper.rb
... ... @@ -124,12 +124,14 @@ module GitlabMarkdownHelper
124 124 end
125 125  
126 126 def rebuild_path(path_with_namespace, path, requested_path, ref)
  127 + path.gsub!(/(#.*)/, "")
  128 + id = $1 || ""
127 129 file_path = relative_file_path(path, requested_path)
128 130 [
129 131 path_with_namespace,
130 132 path_with_ref(file_path, ref),
131 133 file_path
132   - ].compact.join("/")
  134 + ].compact.join("/").gsub(/\/*$/, '') + id
133 135 end
134 136  
135 137 # Checks if the path exists in the repo
... ... @@ -154,6 +156,7 @@ module GitlabMarkdownHelper
154 156 # If we are at doc/api and the README.md shown in below the tree view
155 157 # this takes the rquest path(doc/api) and adds users.md so the path looks like doc/api/users.md
156 158 def build_nested_path(path, request_path)
  159 + return request_path if path == ""
157 160 return path unless request_path
158 161 if local_path(request_path) == "tree"
159 162 base = request_path.split("/").push(path)
... ... @@ -166,7 +169,7 @@ module GitlabMarkdownHelper
166 169 end
167 170  
168 171 def file_exists?(path)
169   - return false if path.nil? || path.empty?
  172 + return false if path.nil?
170 173 return @repository.blob_at(current_sha, path).present? || @repository.tree(current_sha, path).entries.any?
171 174 end
172 175  
... ...
features/project/source/markdown_render.feature
... ... @@ -4,9 +4,7 @@ Feature: Project markdown render
4 4 And I own project "Delta"
5 5 Given I visit project source page
6 6  
7   - # -------------------------------------------
8   - # README
9   - # -------------------------------------------
  7 + # Tree README
10 8  
11 9 Scenario: Tree view should have correct links in README
12 10 Given I go directory which contains README file
... ... @@ -41,9 +39,7 @@ Feature: Project markdown render
41 39 Then I should see rendered README which contains correct links
42 40 And Header "Application details" should have correct id and link
43 41  
44   - # -------------------------------------------
45   - # File content
46   - # -------------------------------------------
  42 + # Blob
47 43  
48 44 Scenario: I navigate to doc directory to view documentation in master
49 45 And I navigate to the doc/api/README
... ... @@ -61,9 +57,7 @@ Feature: Project markdown render
61 57 And I navigate to the doc/api/README
62 58 And Header "GitLab API" should have correct id and link
63 59  
64   - # -------------------------------------------
65   - # Markdown branch README
66   - # -------------------------------------------
  60 + # Markdown branch
67 61  
68 62 Scenario: I browse files from markdown branch
69 63 When I visit markdown branch
... ... @@ -93,9 +87,31 @@ Feature: Project markdown render
93 87 And I click on raketasks in doc/api/README
94 88 Then I should see correct directory rendered for markdown branch
95 89  
96   - # -------------------------------------------
  90 + Scenario: Tree markdown links view empty urls should have correct urls
  91 + When I visit markdown branch
  92 + Then The link with text "empty" should have url "tree/markdown"
  93 + When I visit markdown branch "README.md" blob
  94 + Then The link with text "empty" should have url "blob/markdown/README.md"
  95 + When I visit markdown branch "d" tree
  96 + Then The link with text "empty" should have url "tree/markdown/d"
  97 + When I visit markdown branch "d/README.md" blob
  98 + Then The link with text "empty" should have url "blob/markdown/d/README.md"
  99 +
  100 + # "ID" means "#id" on the tests below, because we are unable to escape the hash sign.
  101 + # which Spinach interprets as the start of a comment.
  102 + Scenario: All markdown links with ids should have correct urls
  103 + When I visit markdown branch
  104 + Then The link with text "ID" should have url "tree/markdownID"
  105 + Then The link with text "/ID" should have url "tree/markdownID"
  106 + Then The link with text "README.mdID" should have url "blob/markdown/README.mdID"
  107 + Then The link with text "d/README.mdID" should have url "blob/markdown/d/README.mdID"
  108 + When I visit markdown branch "README.md" blob
  109 + Then The link with text "ID" should have url "blob/markdown/README.mdID"
  110 + Then The link with text "/ID" should have url "blob/markdown/README.mdID"
  111 + Then The link with text "README.mdID" should have url "blob/markdown/README.mdID"
  112 + Then The link with text "d/README.mdID" should have url "blob/markdown/d/README.mdID"
  113 +
97 114 # Wiki
98   - # -------------------------------------------
99 115  
100 116 Scenario: I create a wiki page with different links
101 117 Given I go to wiki page
... ...
features/steps/project/project_markdown_render.rb
  1 +# If you need to modify the existing seed repository for your tests,
  2 +# it is recommended that you make the changes on the `markdown` branch of the seed project repository,
  3 +# which should only be used by tests in this file. See `/spec/factories.rb#project` for more info.
1 4 class Spinach::Features::ProjectMarkdownRender < Spinach::FeatureSteps
2 5 include SharedAuthentication
3 6 include SharedPaths
... ... @@ -50,7 +53,7 @@ class Spinach::Features::ProjectMarkdownRender &lt; Spinach::FeatureSteps
50 53 end
51 54  
52 55 Then 'I should see correct doc/api directory rendered' do
53   - current_path.should == project_tree_path(@project, "master/doc/api/")
  56 + current_path.should == project_tree_path(@project, "master/doc/api")
54 57 page.should have_content "README.md"
55 58 page.should have_content "users.md"
56 59 end
... ... @@ -64,6 +67,18 @@ class Spinach::Features::ProjectMarkdownRender &lt; Spinach::FeatureSteps
64 67 page.should have_content "bundle exec rake gitlab:env:info RAILS_ENV=production"
65 68 end
66 69  
  70 + And 'I click on link "empty" in the README' do
  71 + within('.readme-holder') do
  72 + click_link "empty"
  73 + end
  74 + end
  75 +
  76 + And 'I click on link "id" in the README' do
  77 + within('.readme-holder') do
  78 + click_link "#id"
  79 + end
  80 + end
  81 +
67 82 And 'I navigate to the doc/api/README' do
68 83 click_link "doc"
69 84 click_link "api"
... ... @@ -90,10 +105,24 @@ class Spinach::Features::ProjectMarkdownRender &lt; Spinach::FeatureSteps
90 105 click_link "Rake tasks"
91 106 end
92 107  
  108 + # Markdown branch
  109 +
93 110 When 'I visit markdown branch' do
94 111 visit project_tree_path(@project, "markdown")
95 112 end
96 113  
  114 + When 'I visit markdown branch "README.md" blob' do
  115 + visit project_blob_path(@project, "markdown/README.md")
  116 + end
  117 +
  118 + When 'I visit markdown branch "d" tree' do
  119 + visit project_tree_path(@project, "markdown/d")
  120 + end
  121 +
  122 + When 'I visit markdown branch "d/README.md" blob' do
  123 + visit project_blob_path(@project, "markdown/d/README.md")
  124 + end
  125 +
97 126 Then 'I should see files from repository in markdown branch' do
98 127 current_path.should == project_tree_path(@project, "markdown")
99 128 page.should have_content "Gemfile"
... ... @@ -124,6 +153,50 @@ class Spinach::Features::ProjectMarkdownRender &lt; Spinach::FeatureSteps
124 153 page.should have_content "Get a list of users."
125 154 end
126 155  
  156 + # Expected link contents
  157 +
  158 + Then 'The link with text "empty" should have url "tree/markdown"' do
  159 + find('a', text: /^empty$/)['href'] == current_host + project_tree_path(@project, "markdown")
  160 + end
  161 +
  162 + Then 'The link with text "empty" should have url "blob/markdown/README.md"' do
  163 + find('a', text: /^empty$/)['href'] == current_host + project_blob_path(@project, "markdown/README.md")
  164 + end
  165 +
  166 + Then 'The link with text "empty" should have url "tree/markdown/d"' do
  167 + find('a', text: /^empty$/)['href'] == current_host + project_tree_path(@project, "markdown/d")
  168 + end
  169 +
  170 + Then 'The link with text "empty" should have url "blob/markdown/d/README.md"' do
  171 + find('a', text: /^empty$/)['href'] == current_host + project_blob_path(@project, "markdown/d/README.md")
  172 + end
  173 +
  174 + Then 'The link with text "ID" should have url "tree/markdownID"' do
  175 + find('a', text: /^#id$/)['href'] == current_host + project_tree_path(@project, "markdown") + '#id'
  176 + end
  177 +
  178 + Then 'The link with text "/ID" should have url "tree/markdownID"' do
  179 + find('a', text: /^\/#id$/)['href'] == current_host + project_tree_path(@project, "markdown") + '#id'
  180 + end
  181 +
  182 + Then 'The link with text "README.mdID" should have url "blob/markdown/README.mdID"' do
  183 + find('a', text: /^README.md#id$/)['href'] == current_host + project_blob_path(@project, "markdown/README.md") + '#id'
  184 + end
  185 +
  186 + Then 'The link with text "d/README.mdID" should have url "blob/markdown/d/README.mdID"' do
  187 + find('a', text: /^d\/README.md#id$/)['href'] == current_host + project_blob_path(@project, "d/markdown/README.md") + '#id'
  188 + end
  189 +
  190 + Then 'The link with text "ID" should have url "blob/markdown/README.mdID"' do
  191 + find('a', text: /^#id$/)['href'] == current_host + project_blob_path(@project, "markdown/README.md") + '#id'
  192 + end
  193 +
  194 + Then 'The link with text "/ID" should have url "blob/markdown/README.mdID"' do
  195 + find('a', text: /^\/#id$/)['href'] == current_host + project_blob_path(@project, "markdown/README.md") + '#id'
  196 + end
  197 +
  198 + # Wiki
  199 +
127 200 Given 'I go to wiki page' do
128 201 click_link "Wiki"
129 202 current_path.should == project_wiki_path(@project, "home")
... ...
spec/factories.rb
... ... @@ -34,6 +34,16 @@ FactoryGirl.define do
34 34 creator
35 35 end
36 36  
  37 + # Generates a test repository from the repository stored under `spec/seed_project.tar.gz`.
  38 + # Once you run `rake gitlab:setup`, you can see what the repository looks like under `tmp/repositories/gitlabhq`.
  39 + # In order to modify files in the repository, you must untar the seed, modify and remake the tar.
  40 + # Before recompressing, do not forget to `git checkout master`.
  41 + # After recompressing, you need to run `RAILS_ENV=test bundle exec rake gitlab:setup` to regenerate the seeds under tmp.
  42 + #
  43 + # If you want to modify the repository only for an specific type of tests, e.g., markdown tests,
  44 + # consider using a feature branch to reduce the chances of collision with other tests.
  45 + # Create a new commit, and use the same commit message that you will use for the change in the main repo.
  46 + # Changing the commig message and SHA of branch `master` may break tests.
37 47 factory :project, parent: :empty_project do
38 48 path { 'gitlabhq' }
39 49  
... ...
spec/helpers/gitlab_markdown_helper_spec.rb
... ... @@ -454,7 +454,7 @@ describe GitlabMarkdownHelper do
454 454  
455 455 it "should handle relative urls in reference links for a directory in master" do
456 456 actual = "[GitLab API doc directory][GitLab readmes]\n [GitLab readmes]: doc/api/\n"
457   - expected = "<p><a href=\"/#{project.path_with_namespace}/tree/master/doc/api/\">GitLab API doc directory</a></p>\n"
  457 + expected = "<p><a href=\"/#{project.path_with_namespace}/tree/master/doc/api\">GitLab API doc directory</a></p>\n"
458 458 markdown(actual).should match(expected)
459 459 end
460 460  
... ...
spec/seed_project.tar.gz
No preview for this file type