Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
d4acda12
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d4acda12
编写于
9月 21, 2015
作者:
R
Robert Speicher
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'rs-relative-link-up-one' into 'master'
Allow relative links to go up one directory level See merge request !1352
上级
d4a96016
2b94f5fb
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
51 addition
and
15 deletion
+51
-15
lib/gitlab/markdown/relative_link_filter.rb
lib/gitlab/markdown/relative_link_filter.rb
+29
-11
spec/lib/gitlab/markdown/relative_link_filter_spec.rb
spec/lib/gitlab/markdown/relative_link_filter_spec.rb
+22
-4
未找到文件。
lib/gitlab/markdown/relative_link_filter.rb
浏览文件 @
d4acda12
...
...
@@ -59,25 +59,43 @@ 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'
while
parts
.
length
>
1
&&
path
.
start_with?
(
'../'
)
parts
.
pop
path
.
sub!
(
'../'
,
''
)
end
parts
.
push
(
path
).
join
(
'/'
)
end
...
...
spec/lib/gitlab/markdown/relative_link_filter_spec.rb
浏览文件 @
d4acda12
...
...
@@ -4,14 +4,16 @@ require 'spec_helper'
module
Gitlab::Markdown
describe
RelativeLinkFilter
do
def
filter
(
doc
)
described_class
.
call
(
doc
,
{
def
filter
(
doc
,
contexts
=
{}
)
contexts
.
reverse_merge!
(
{
commit:
project
.
commit
,
project:
project
,
project_wiki:
project_wiki
,
ref:
ref
,
requested_path:
requested_path
})
described_class
.
call
(
doc
,
contexts
)
end
def
image
(
path
)
...
...
@@ -75,6 +77,22 @@ module Gitlab::Markdown
to
eq
"/
#{
project_path
}
/blob/
#{
ref
}
/doc/api/README.md"
end
it
'rebuilds relative URL for a file in the repo up one directory'
do
relative_link
=
link
(
'../api/README.md'
)
doc
=
filter
(
relative_link
,
requested_path:
'doc/update/7.14-to-8.0.md'
)
expect
(
doc
.
at_css
(
'a'
)[
'href'
]).
to
eq
"/
#{
project_path
}
/blob/
#{
ref
}
/doc/api/README.md"
end
it
'rebuilds relative URL for a file in the repo up multiple directories'
do
relative_link
=
link
(
'../../../api/README.md'
)
doc
=
filter
(
relative_link
,
requested_path:
'doc/foo/bar/baz/README.md'
)
expect
(
doc
.
at_css
(
'a'
)[
'href'
]).
to
eq
"/
#{
project_path
}
/blob/
#{
ref
}
/doc/api/README.md"
end
it
'rebuilds relative URL for a file in the repo with an anchor'
do
doc
=
filter
(
link
(
'README.md#section'
))
expect
(
doc
.
at_css
(
'a'
)[
'href'
]).
...
...
@@ -108,8 +126,8 @@ module Gitlab::Markdown
escaped
=
Addressable
::
URI
.
escape
(
path
)
# Stub these methods so the file doesn't actually need to be in the repo
allow_any_instance_of
(
described_class
).
to
receive
(
:file_exists?
).
and_return
(
true
)
allow_any_instance_of
(
described_class
).
to
receive
(
:file_exists?
).
and_return
(
true
)
allow_any_instance_of
(
described_class
).
to
receive
(
:image?
).
with
(
path
).
and_return
(
true
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录