From 619f04c196731b2a274802dfb4b4d3c3664e5465 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Fri, 18 Sep 2015 14:03:42 -0400 Subject: Allow relative links to go up one directory level --- lib/gitlab/markdown/relative_link_filter.rb | 41 +++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/markdown/relative_link_filter.rb b/lib/gitlab/markdown/relative_link_filter.rb index 8c5cf51bfe1..d613c205509 100644 --- a/lib/gitlab/markdown/relative_link_filter.rb +++ b/lib/gitlab/markdown/relative_link_filter.rb @@ -59,25 +59,44 @@ module Gitlab end def relative_file_path(path) - nested_path = build_nested_path(path, context[:requested_path]) + nested_path = build_relative_path(path, context[:requested_path]) file_exists?(nested_path) ? nested_path : path end - # Covering a special case, when the link is referencing file in the same - # directory. - # If we are at doc/api/README.md and the README.md contains relative - # links like [Users](users.md), this takes the request - # path(doc/api/README.md) and replaces the README.md with users.md so the - # path looks like doc/api/users.md. - # If we are at doc/api and the README.md shown in below the tree view - # this takes the request path(doc/api) and adds users.md so the path - # looks like doc/api/users.md - def build_nested_path(path, request_path) + # Convert a relative path into its correct location based on the currently + # requested path + # + # path - Relative path String + # request_path - Currently-requested path String + # + # Examples: + # + # # File in the same directory as the current path + # build_relative_path("users.md", "doc/api/README.md") + # # => "doc/api/users.md" + # + # # File in the same directory, which is also the current path + # build_relative_path("users.md", "doc/api") + # # => "doc/api/users.md" + # + # # Going up one level to a different directory + # build_relative_path("../update/7.14-to-8.0.md", "doc/api/README.md") + # # => "doc/update/7.14-to-8.0.md" + # + # Returns a String + def build_relative_path(path, request_path) return request_path if path.empty? return path unless request_path parts = request_path.split('/') parts.pop if path_type(request_path) != 'tree' + + # Allow for going up one directory + if parts.length > 1 && path.start_with?('../') + parts.pop + path.sub!('../', '') + end + parts.push(path).join('/') end -- cgit v1.2.3 From 2b94f5fb06ef061fb49a7dadcbb95c0954bc9860 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Sun, 20 Sep 2015 19:21:33 -0400 Subject: Allow RelativeLinkFilter to go up multiple directories --- lib/gitlab/markdown/relative_link_filter.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/markdown/relative_link_filter.rb b/lib/gitlab/markdown/relative_link_filter.rb index d613c205509..6ee3d1ce039 100644 --- a/lib/gitlab/markdown/relative_link_filter.rb +++ b/lib/gitlab/markdown/relative_link_filter.rb @@ -91,8 +91,7 @@ module Gitlab parts = request_path.split('/') parts.pop if path_type(request_path) != 'tree' - # Allow for going up one directory - if parts.length > 1 && path.start_with?('../') + while parts.length > 1 && path.start_with?('../') parts.pop path.sub!('../', '') end -- cgit v1.2.3