summaryrefslogtreecommitdiff
path: root/app/helpers/gitlab_markdown_helper.rb
diff options
context:
space:
mode:
authorMarin Jankovski <marin@gitlab.com>2014-01-22 15:07:29 +0100
committerMarin Jankovski <marin@gitlab.com>2014-01-22 15:07:29 +0100
commit16ba8fa077145f99dff98501770ed80d0a58fd58 (patch)
treec0397c12de443307008e8b46eb37f5baa1929b23 /app/helpers/gitlab_markdown_helper.rb
parent1018cbdc803d56e6cae385aee97aa04752d3e3fb (diff)
downloadgitlab-ce-16ba8fa077145f99dff98501770ed80d0a58fd58.tar.gz
Display correct paths in markdown for reference style links.
Diffstat (limited to 'app/helpers/gitlab_markdown_helper.rb')
-rw-r--r--app/helpers/gitlab_markdown_helper.rb31
1 files changed, 25 insertions, 6 deletions
diff --git a/app/helpers/gitlab_markdown_helper.rb b/app/helpers/gitlab_markdown_helper.rb
index 8894a01eaea..60f9d4e764a 100644
--- a/app/helpers/gitlab_markdown_helper.rb
+++ b/app/helpers/gitlab_markdown_helper.rb
@@ -69,10 +69,17 @@ module GitlabMarkdownHelper
project_path_with_namespace = project.path_with_namespace
paths = extract_paths(text)
paths.each do |file_path|
- new_path = rebuild_path(project_path_with_namespace, file_path, requested_path, ref)
- # 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})")
+ original_file_path = extract(file_path)
+ new_path = rebuild_path(project_path_with_namespace, original_file_path, requested_path, ref)
+ if reference_path?(file_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
text
end
@@ -83,9 +90,11 @@ module GitlabMarkdownHelper
select_relative(paths)
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)
- 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
# Removes any empty result produced by not matching the regexp
@@ -93,12 +102,22 @@ module GitlabMarkdownHelper
paths.reject{|l| l.empty? }.flatten
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
# eg. reject "https://gitlab.org} but accept "doc/api/README.md"
def select_relative(paths)
paths.reject{|path| ignored_protocols.map{|protocol| path.include?(protocol)}.any?}
end
+ # Check whether a path is a reference-style link
+ def reference_path?(path)
+ path.include?("]: ")
+ end
+
def ignored_protocols
["http://","https://", "ftp://", "mailto:"]
end