Commit ced4a37a authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

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
```
parents ff07a5ab 54bcb6cc
...@@ -69,10 +69,17 @@ module GitlabMarkdownHelper ...@@ -69,10 +69,17 @@ module GitlabMarkdownHelper
project_path_with_namespace = project.path_with_namespace project_path_with_namespace = project.path_with_namespace
paths = extract_paths(text) paths = extract_paths(text)
paths.each do |file_path| paths.each do |file_path|
new_path = rebuild_path(project_path_with_namespace, file_path, requested_path, ref) original_file_path = extract(file_path)
# Replacing old string with a new one with brackets ]() to prevent replacing occurence of a word new_path = rebuild_path(project_path_with_namespace, original_file_path, requested_path, ref)
# e.g. If we have a markdown like [test](test) this will replace ](test) and not the word test if reference_path?(file_path)
text.gsub!("](#{file_path})", "](/#{new_path})") # Replacing old string with a new one that contains updated path
# eg. [some document]: document.md will be replaced with [some document] /namespace/project/master/blob/document.md
text.gsub!(file_path, file_path.gsub(original_file_path, "/#{new_path}"))
else
# Replacing old string with a new one with brackets ]() to prevent replacing occurence of a word
# e.g. If we have a markdown like [test](test) this will replace ](test) and not the word test
text.gsub!("](#{file_path})", "](/#{new_path})")
end
end end
text text
end end
...@@ -83,9 +90,11 @@ module GitlabMarkdownHelper ...@@ -83,9 +90,11 @@ module GitlabMarkdownHelper
select_relative(paths) select_relative(paths)
end end
# Split the markdown text to each line and find all paths, this will match anything with - ]("some_text") # Split the markdown text to each line and find all paths, this will match anything with - ]("some_text") and [some text]: file.md
def pick_out_paths(markdown_text) def pick_out_paths(markdown_text)
markdown_text.split("\n").map { |text| text.scan(/\]\(([^(]+)\)/) } inline_paths = markdown_text.split("\n").map { |text| text.scan(/\]\(([^(]+)\)/) }
reference_paths = markdown_text.split("\n").map { |text| text.scan(/\[.*\]:.*/) }
inline_paths + reference_paths
end end
# Removes any empty result produced by not matching the regexp # Removes any empty result produced by not matching the regexp
...@@ -93,12 +102,22 @@ module GitlabMarkdownHelper ...@@ -93,12 +102,22 @@ module GitlabMarkdownHelper
paths.reject{|l| l.empty? }.flatten paths.reject{|l| l.empty? }.flatten
end end
# If a path is a reference style link we need to omit ]:
def extract(path)
path.split("]: ").last
end
# Reject any path that contains ignored protocol # Reject any path that contains ignored protocol
# eg. reject "https://gitlab.org} but accept "doc/api/README.md" # eg. reject "https://gitlab.org} but accept "doc/api/README.md"
def select_relative(paths) def select_relative(paths)
paths.reject{|path| ignored_protocols.map{|protocol| path.include?(protocol)}.any?} paths.reject{|path| ignored_protocols.map{|protocol| path.include?(protocol)}.any?}
end end
# Check whether a path is a reference-style link
def reference_path?(path)
path.include?("]: ")
end
def ignored_protocols def ignored_protocols
["http://","https://", "ftp://", "mailto:"] ["http://","https://", "ftp://", "mailto:"]
end end
......
...@@ -16,6 +16,18 @@ Feature: Project markdown render ...@@ -16,6 +16,18 @@ Feature: Project markdown render
And I click on Rake tasks in README And I click on Rake tasks in README
Then I should see correct directory rendered Then I should see correct directory rendered
Scenario: I view README in master branch to see reference links to directory
Then I should see files from repository in master
And I should see rendered README which contains correct links
And I click on GitLab API doc directory in README
Then I should see correct doc/api directory rendered
Scenario: I view README in master branch to see reference links to file
Then I should see files from repository in master
And I should see rendered README which contains correct links
And I click on Maintenance in README
Then I should see correct maintenance file rendered
Scenario: I navigate to doc directory to view documentation in master Scenario: I navigate to doc directory to view documentation in master
And I navigate to the doc/api/README And I navigate to the doc/api/README
And I see correct file rendered And I see correct file rendered
......
...@@ -21,6 +21,8 @@ class Spinach::Features::ProjectMarkdownRender < Spinach::FeatureSteps ...@@ -21,6 +21,8 @@ class Spinach::Features::ProjectMarkdownRender < Spinach::FeatureSteps
page.should have_link "GitLab API website" page.should have_link "GitLab API website"
page.should have_link "Rake tasks" page.should have_link "Rake tasks"
page.should have_link "backup and restore procedure" page.should have_link "backup and restore procedure"
page.should have_link "GitLab API doc directory"
page.should have_link "Maintenance"
end end
And 'I click on Gitlab API in README' do And 'I click on Gitlab API in README' do
...@@ -42,6 +44,26 @@ class Spinach::Features::ProjectMarkdownRender < Spinach::FeatureSteps ...@@ -42,6 +44,26 @@ class Spinach::Features::ProjectMarkdownRender < Spinach::FeatureSteps
page.should have_content "maintenance.md" page.should have_content "maintenance.md"
end end
And 'I click on GitLab API doc directory in README' do
click_link "GitLab API doc directory"
end
Then 'I should see correct doc/api directory rendered' do
current_path.should == project_tree_path(@project, "master/doc/api/")
page.should have_content "README.md"
page.should have_content "users.md"
end
And 'I click on Maintenance in README' do
click_link "Maintenance"
end
Then 'I should see correct maintenance file rendered' do
current_path.should == project_blob_path(@project, "master/doc/raketasks/maintenance.md")
page.should have_content "bundle exec rake gitlab:env:info RAILS_ENV=production"
end
And 'I navigate to the doc/api/README' do And 'I navigate to the doc/api/README' do
click_link "doc" click_link "doc"
click_link "api" click_link "api"
......
...@@ -431,6 +431,24 @@ describe GitlabMarkdownHelper do ...@@ -431,6 +431,24 @@ describe GitlabMarkdownHelper do
expected = "<p><a href=\"/#{project.path_with_namespace}/wikis/test/link\">Link</a></p>\n" expected = "<p><a href=\"/#{project.path_with_namespace}/wikis/test/link\">Link</a></p>\n"
markdown(actual).should match(expected) markdown(actual).should match(expected)
end end
it "should handle relative urls in reference links for a file in master" do
actual = "[GitLab API doc][GitLab readme]\n [GitLab readme]: doc/api/README.md\n"
expected = "<p><a href=\"/#{project.path_with_namespace}/blob/master/doc/api/README.md\">GitLab API doc</a></p>\n"
markdown(actual).should match(expected)
end
it "should handle relative urls in reference links for a directory in master" do
actual = "[GitLab API doc directory][GitLab readmes]\n [GitLab readmes]: doc/api/\n"
expected = "<p><a href=\"/#{project.path_with_namespace}/tree/master/doc/api/\">GitLab API doc directory</a></p>\n"
markdown(actual).should match(expected)
end
it "should not handle malformed relative urls in reference links for a file in master" do
actual = "[GitLab readme]: doc/api/README.md\n"
expected = ""
markdown(actual).should match(expected)
end
end end
describe "#render_wiki_content" do describe "#render_wiki_content" do
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
describe 'Gitlab::Satellite::MergeAction' do describe 'Gitlab::Satellite::MergeAction' do
before(:each) do before(:each) do
@master = ['master', 'b1e6a9dbf1c85e6616497a5e7bad9143a4bd0828'] @master = ['master', '69b34b7e9ad9f496f0ad10250be37d6265a03bba']
@one_after_stable = ['stable', '6ea87c47f0f8a24ae031c3fff17bc913889ecd00'] #this commit sha is one after stable @one_after_stable = ['stable', '6ea87c47f0f8a24ae031c3fff17bc913889ecd00'] #this commit sha is one after stable
@wiki_branch = ['wiki', '635d3e09b72232b6e92a38de6cc184147e5bcb41'] #this is the commit sha where the wiki branch goes off from master @wiki_branch = ['wiki', '635d3e09b72232b6e92a38de6cc184147e5bcb41'] #this is the commit sha where the wiki branch goes off from master
@conflicting_metior = ['metior', '313d96e42b313a0af5ab50fa233bf43e27118b3f'] #this branch conflicts with the wiki branch @conflicting_metior = ['metior', '313d96e42b313a0af5ab50fa233bf43e27118b3f'] #this branch conflicts with the wiki branch
......
...@@ -137,16 +137,16 @@ describe Project do ...@@ -137,16 +137,16 @@ describe Project do
it "should close merge request if last commit from source branch was pushed to target branch" do it "should close merge request if last commit from source branch was pushed to target branch" do
@merge_request.reload_code @merge_request.reload_code
@merge_request.last_commit.id.should == "b1e6a9dbf1c85e6616497a5e7bad9143a4bd0828" @merge_request.last_commit.id.should == "69b34b7e9ad9f496f0ad10250be37d6265a03bba"
project.update_merge_requests("8716fc78f3c65bbf7bcf7b574febd583bc5d2812", "b1e6a9dbf1c85e6616497a5e7bad9143a4bd0828", "refs/heads/stable", @key.user) project.update_merge_requests("8716fc78f3c65bbf7bcf7b574febd583bc5d2812", "69b34b7e9ad9f496f0ad10250be37d6265a03bba", "refs/heads/stable", @key.user)
@merge_request.reload @merge_request.reload
@merge_request.merged?.should be_true @merge_request.merged?.should be_true
end end
it "should update merge request commits with new one if pushed to source branch" do it "should update merge request commits with new one if pushed to source branch" do
project.update_merge_requests("8716fc78f3c65bbf7bcf7b574febd583bc5d2812", "b1e6a9dbf1c85e6616497a5e7bad9143a4bd0828", "refs/heads/master", @key.user) project.update_merge_requests("8716fc78f3c65bbf7bcf7b574febd583bc5d2812", "69b34b7e9ad9f496f0ad10250be37d6265a03bba", "refs/heads/master", @key.user)
@merge_request.reload @merge_request.reload
@merge_request.last_commit.id.should == "b1e6a9dbf1c85e6616497a5e7bad9143a4bd0828" @merge_request.last_commit.id.should == "69b34b7e9ad9f496f0ad10250be37d6265a03bba"
end end
end end
......
No preview for this file type
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