Commit 5277ac1b15edbb6dd7aa7a5cadee0efc07cdb3d4

Authored by Marin Jankovski
1 parent 7dbbb6de

Added comments and split the methods further.

Showing 1 changed file with 46 additions and 13 deletions   Show diff stats
app/helpers/gitlab_markdown_helper.rb
@@ -59,20 +59,48 @@ module GitlabMarkdownHelper @@ -59,20 +59,48 @@ module GitlabMarkdownHelper
59 end 59 end
60 end 60 end
61 61
  62 + # text - whole text from a markdown file
  63 + # project_path_with_namespace - namespace/projectname
  64 + # ref - name of the branch or reference
  65 + # wiki - whether the markdown is from wiki or not
62 def create_relative_links(text, project_path_with_namespace, ref, wiki = false) 66 def create_relative_links(text, project_path_with_namespace, ref, wiki = false)
63 - links = extract_paths(text)  
64 - links.each do |string|  
65 - new_link = new_link(project_path_with_namespace, string, ref)  
66 - text.gsub!("](#{string})", "](/#{new_link})") 67 + paths = extract_paths(text)
  68 + paths.each do |path|
  69 + new_path = rebuild_path(project_path_with_namespace, path, ref)
  70 + # Replacing old string with a new one with brackets ]() to prevent replacing occurence of a word
  71 + # e.g. If we have a markdown like [test](test) this will replace ](test) and not the word test
  72 + text.gsub!("](#{path})", "](/#{new_path})")
67 end 73 end
68 text 74 text
69 end 75 end
70 76
71 - def extract_paths(text)  
72 - text.split("\n").map { |a| a.scan(/\]\(([^(]+)\)/) }.reject{|b| b.empty? }.flatten.reject{|c| c.include?("http" || "www")} 77 + def extract_paths(markdown_text)
  78 + all_markdown_paths = pick_out_paths(markdown_text)
  79 + paths = remove_empty(all_markdown_paths)
  80 + select_relative(paths)
73 end 81 end
74 82
75 - def new_link(path_with_namespace, string, ref) 83 + # Split the markdown text to each line and find all paths, this will match anything with - ]("some_text")
  84 + def pick_out_paths(markdown_text)
  85 + markdown_text.split("\n").map { |text| text.scan(/\]\(([^(]+)\)/) }
  86 + end
  87 +
  88 + # Removes any empty result produced by not matching the regexp
  89 + def remove_empty(paths)
  90 + paths.reject{|l| l.empty? }.flatten
  91 + end
  92 +
  93 + # Reject any path that contains ignored protocol
  94 + # eg. reject "https://gitlab.org} but accept "doc/api/README.md"
  95 + def select_relative(paths)
  96 + paths.reject{|path| ignored_protocols.map{|protocol| path.include?(protocol)}.any?}
  97 + end
  98 +
  99 + def ignored_protocols
  100 + ["http://","https://", "ftp://", "mailto:"]
  101 + end
  102 +
  103 + def rebuild_path(path_with_namespace, string, ref)
76 [ 104 [
77 path_with_namespace, 105 path_with_namespace,
78 path_with_ref(string, ref), 106 path_with_ref(string, ref),
@@ -80,19 +108,24 @@ module GitlabMarkdownHelper @@ -80,19 +108,24 @@ module GitlabMarkdownHelper
80 ].compact.join("/") 108 ].compact.join("/")
81 end 109 end
82 110
83 - def path_with_ref(string, ref)  
84 - if File.exists?(Rails.root.join(string))  
85 - "#{local_path(string)}/#{correct_ref(ref)}" 111 + # Checks if the path exists in the repo
  112 + # eg. checks if doc/README.md exists, if it doesn't then it is a wiki link
  113 + def path_with_ref(path, ref)
  114 + if File.exists?(Rails.root.join(path))
  115 + "#{local_path(path)}/#{correct_ref(ref)}"
86 else 116 else
87 "wikis" 117 "wikis"
88 end 118 end
89 end 119 end
90 120
91 - def local_path(string)  
92 - File.directory?(Rails.root.join(string)) ? "tree":"blob" 121 + # Check if the path is pointing to a directory(tree) or a file(blob)
  122 + # eg. doc/api is directory and doc/README.md is file
  123 + def local_path(path)
  124 + File.directory?(Rails.root.join(path)) ? "tree" : "blob"
93 end 125 end
94 126
  127 + # We will assume that if no ref exists we can point to master
95 def correct_ref(ref) 128 def correct_ref(ref)
96 - ref ? ref:'master' 129 + ref ? ref : "master"
97 end 130 end
98 end 131 end