Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
5b23a7bb
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,发现更多精彩内容 >>
提交
5b23a7bb
编写于
10月 10, 2013
作者:
M
Marin Jankovski
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Cover a special case.
上级
5277ac1b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
35 addition
and
11 deletion
+35
-11
app/helpers/gitlab_markdown_helper.rb
app/helpers/gitlab_markdown_helper.rb
+33
-10
lib/redcarpet/render/gitlab_html.rb
lib/redcarpet/render/gitlab_html.rb
+2
-1
未找到文件。
app/helpers/gitlab_markdown_helper.rb
浏览文件 @
5b23a7bb
...
...
@@ -60,16 +60,17 @@ module GitlabMarkdownHelper
end
# text - whole text from a markdown file
# project_path_with_namespace - namespace/projectname
# ref - name of the branch or reference
# 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
# wiki - whether the markdown is from wiki or not
def
create_relative_links
(
text
,
project_path_with_namespace
,
ref
,
wiki
=
false
)
def
create_relative_links
(
text
,
project_path_with_namespace
,
ref
,
requested_path
,
wiki
=
false
)
paths
=
extract_paths
(
text
)
paths
.
each
do
|
path
|
new_path
=
rebuild_path
(
project_path_with_namespace
,
path
,
ref
)
paths
.
each
do
|
file_
path
|
new_path
=
rebuild_path
(
project_path_with_namespace
,
file_path
,
requested_
path
,
ref
)
# 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!
(
"](
#{
path
}
)"
,
"](/
#{
new_path
}
)"
)
text
.
gsub!
(
"](
#{
file_
path
}
)"
,
"](/
#{
new_path
}
)"
)
end
text
end
...
...
@@ -100,24 +101,46 @@ module GitlabMarkdownHelper
[
"http://"
,
"https://"
,
"ftp://"
,
"mailto:"
]
end
def
rebuild_path
(
path_with_namespace
,
string
,
ref
)
def
rebuild_path
(
path_with_namespace
,
path
,
requested_path
,
ref
)
file_path
=
relative_file_path
(
path
,
requested_path
)
[
path_with_namespace
,
path_with_ref
(
string
,
ref
),
string
path_with_ref
(
file_path
,
ref
),
file_path
].
compact
.
join
(
"/"
)
end
# Checks if the path exists in the repo
# eg. checks if doc/README.md exists, if it doesn't then it is a wiki link
def
path_with_ref
(
path
,
ref
)
if
File
.
exists?
(
Rails
.
root
.
join
(
path
)
)
if
file_exists?
(
path
)
"
#{
local_path
(
path
)
}
/
#{
correct_ref
(
ref
)
}
"
else
"wikis"
end
end
def
relative_file_path
(
path
,
requested_path
)
nested_path
=
build_nested_path
(
path
,
requested_path
)
return
nested_path
if
file_exists?
(
nested_path
)
path
end
# Covering a special case, when the link is referencing file in the same directory eg:
# 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
def
build_nested_path
(
path
,
request_path
)
return
path
unless
request_path
base
=
request_path
.
split
(
"/"
)
base
.
pop
(
base
+
[
path
]).
join
(
"/"
)
end
def
file_exists?
(
path
)
return
false
if
path
.
nil?
||
path
.
empty?
File
.
exists?
(
Rails
.
root
.
join
(
path
))
end
# Check if the path is pointing to a directory(tree) or a file(blob)
# eg. doc/api is directory and doc/README.md is file
def
local_path
(
path
)
...
...
lib/redcarpet/render/gitlab_html.rb
浏览文件 @
5b23a7bb
...
...
@@ -7,6 +7,7 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
@template
=
template
@project
=
@template
.
instance_variable_get
(
"@project"
)
@ref
=
@template
.
instance_variable_get
(
"@ref"
)
@request_path
=
@template
.
instance_variable_get
(
"@path"
)
super
options
end
...
...
@@ -34,7 +35,7 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
end
def
preprocess
(
full_document
)
h
.
create_relative_links
(
full_document
,
@project
.
path_with_namespace
,
@ref
,
is_wiki?
)
h
.
create_relative_links
(
full_document
,
@project
.
path_with_namespace
,
@ref
,
@request_path
,
is_wiki?
)
end
def
postprocess
(
full_document
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录