Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
5b23a7bb
Commit
5b23a7bb
authored
Oct 10, 2013
by
Marin Jankovski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cover a special case.
parent
5277ac1b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
11 deletions
+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
No files found.
app/helpers/gitlab_markdown_helper.rb
View file @
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
View file @
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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment