Commit 16ba8fa077145f99dff98501770ed80d0a58fd58

Authored by Marin Jankovski
1 parent 1018cbdc

Display correct paths in markdown for reference style links.

Showing 1 changed file with 25 additions and 6 deletions   Show diff stats
app/helpers/gitlab_markdown_helper.rb
... ... @@ -69,10 +69,17 @@ module GitlabMarkdownHelper
69 69 project_path_with_namespace = project.path_with_namespace
70 70 paths = extract_paths(text)
71 71 paths.each do |file_path|
72   - new_path = rebuild_path(project_path_with_namespace, file_path, requested_path, ref)
73   - # Replacing old string with a new one with brackets ]() to prevent replacing occurence of a word
74   - # e.g. If we have a markdown like [test](test) this will replace ](test) and not the word test
75   - text.gsub!("](#{file_path})", "](/#{new_path})")
  72 + original_file_path = extract(file_path)
  73 + new_path = rebuild_path(project_path_with_namespace, original_file_path, requested_path, ref)
  74 + if reference_path?(file_path)
  75 + # Replacing old string with a new one that contains updated path
  76 + # eg. [some document]: document.md will be replaced with [some document] /namespace/project/master/blob/document.md
  77 + text.gsub!(file_path, file_path.gsub(original_file_path, "/#{new_path}"))
  78 + else
  79 + # Replacing old string with a new one with brackets ]() to prevent replacing occurence of a word
  80 + # e.g. If we have a markdown like [test](test) this will replace ](test) and not the word test
  81 + text.gsub!("](#{file_path})", "](/#{new_path})")
  82 + end
76 83 end
77 84 text
78 85 end
... ... @@ -83,9 +90,11 @@ module GitlabMarkdownHelper
83 90 select_relative(paths)
84 91 end
85 92  
86   - # Split the markdown text to each line and find all paths, this will match anything with - ]("some_text")
  93 + # Split the markdown text to each line and find all paths, this will match anything with - ]("some_text") and [some text]: file.md
87 94 def pick_out_paths(markdown_text)
88   - markdown_text.split("\n").map { |text| text.scan(/\]\(([^(]+)\)/) }
  95 + inline_paths = markdown_text.split("\n").map { |text| text.scan(/\]\(([^(]+)\)/) }
  96 + reference_paths = markdown_text.split("\n").map { |text| text.scan(/\[.*\]:.*/) }
  97 + inline_paths + reference_paths
89 98 end
90 99  
91 100 # Removes any empty result produced by not matching the regexp
... ... @@ -93,12 +102,22 @@ module GitlabMarkdownHelper
93 102 paths.reject{|l| l.empty? }.flatten
94 103 end
95 104  
  105 + # If a path is a reference style link we need to omit ]:
  106 + def extract(path)
  107 + path.split("]: ").last
  108 + end
  109 +
96 110 # Reject any path that contains ignored protocol
97 111 # eg. reject "https://gitlab.org} but accept "doc/api/README.md"
98 112 def select_relative(paths)
99 113 paths.reject{|path| ignored_protocols.map{|protocol| path.include?(protocol)}.any?}
100 114 end
101 115  
  116 + # Check whether a path is a reference-style link
  117 + def reference_path?(path)
  118 + path.include?("]: ")
  119 + end
  120 +
102 121 def ignored_protocols
103 122 ["http://","https://", "ftp://", "mailto:"]
104 123 end
... ...