From 52c3179be94e857ecb0de2cfa8ecbd484a36d213 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Tue, 20 May 2014 15:29:14 +0200 Subject: [PATCH] Do not replace links inside code blocks, less code for the same amount of work. --- app/helpers/gitlab_markdown_helper.rb | 109 ++++++++++---------------- lib/redcarpet/render/gitlab_html.rb | 17 +--- 2 files changed, 43 insertions(+), 83 deletions(-) diff --git a/app/helpers/gitlab_markdown_helper.rb b/app/helpers/gitlab_markdown_helper.rb index 69425bc171d..2d226adb2a4 100644 --- a/app/helpers/gitlab_markdown_helper.rb +++ b/app/helpers/gitlab_markdown_helper.rb @@ -59,90 +59,59 @@ module GitlabMarkdownHelper end end - # text - whole text from a markdown file - # project_path_with_namespace - namespace/projectname, eg. gitlabhq/gitlabhq - # ref - name of the branch or reference, eg. stable - # requested_path - path of request, eg. doc/api/README.md, used in special case when path is pointing to the .md file were the original request is coming from - def create_relative_links(text, project, ref, requested_path) - @path_to_satellite = project.satellite.path - project_path_with_namespace = project.path_with_namespace + def create_relative_links(text) paths = extract_paths(text) - paths.each do |file_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 + + paths.uniq.each do |file_path| + new_path = rebuild_path(file_path) + # Finds quoted path so we don't replace other mentions of the string + # eg. "doc/api" will be replaced and "/home/doc/api/text" won't + text.gsub!("\"#{file_path}\"", "\"/#{new_path}\"") end + text end - def extract_paths(markdown_text) - all_markdown_paths = pick_out_paths(markdown_text) - paths = remove_empty(all_markdown_paths) - select_relative(paths) + def extract_paths(text) + links = substitute_links(text) + image_links = substitute_image_links(text) + links + image_links end - # 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) - inline_paths = markdown_text.split("\n").map { |text| text.scan(/\]\(([^(]+)\)/) } - reference_paths = markdown_text.split("\n").map { |text| text.scan(/\[.*\]:.*/) } - inline_paths + reference_paths + def substitute_links(text) + links = text.scan(//) + relative_links = links.flatten.reject{ |link| link_to_ignore? link } + relative_links end - # Removes any empty result produced by not matching the regexp - def remove_empty(paths) - paths.reject{|l| l.empty? }.flatten + def substitute_image_links(text) + links = text.scan(/