Commit 70f828cd4b06c0854e71b72cc03cf45c11987d7e
1 parent
1eabd9df
Exists in
master
and in
4 other branches
Split.
Showing
1 changed file
with
30 additions
and
7 deletions
Show diff stats
app/helpers/gitlab_markdown_helper.rb
... | ... | @@ -60,16 +60,39 @@ module GitlabMarkdownHelper |
60 | 60 | end |
61 | 61 | |
62 | 62 | def create_relative_links(text, project_path_with_namespace, ref, wiki = false) |
63 | - links = text.split("\n").map { |a| a.scan(/\]\(([^(]+)\)/) }.reject{|b| b.empty? }.flatten.reject{|c| c.include?("http" || "www")} | |
63 | + links = extract_paths(text) | |
64 | 64 | links.each do |string| |
65 | - new_link = [ | |
66 | - project_path_with_namespace, | |
67 | - wiki ? "wikis":"blob", | |
68 | - ref, | |
69 | - string | |
70 | - ].compact.join("/") | |
65 | + new_link = new_link(project_path_with_namespace, string, ref) | |
71 | 66 | text.gsub!("](#{string})", "](/#{new_link})") |
72 | 67 | end |
73 | 68 | text |
74 | 69 | end |
70 | + | |
71 | + def extract_paths(text) | |
72 | + text.split("\n").map { |a| a.scan(/\]\(([^(]+)\)/) }.reject{|b| b.empty? }.flatten.reject{|c| c.include?("http" || "www")} | |
73 | + end | |
74 | + | |
75 | + def new_link(path_with_namespace, string, ref) | |
76 | + [ | |
77 | + path_with_namespace, | |
78 | + path(string, ref), | |
79 | + string | |
80 | + ].compact.join("/") | |
81 | + end | |
82 | + | |
83 | + def path(string, ref) | |
84 | + if File.exists?(Rails.root.join(string)) | |
85 | + "#{local_path(string)}/#{correct_ref(ref)}" | |
86 | + else | |
87 | + "wikis" | |
88 | + end | |
89 | + end | |
90 | + | |
91 | + def local_path(string) | |
92 | + File.directory?(Rails.root.join(string)) ? "tree":"blob" | |
93 | + end | |
94 | + | |
95 | + def correct_ref(ref) | |
96 | + ref ? ref:'master' | |
97 | + end | |
75 | 98 | end | ... | ... |