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
0
Merge Requests
0
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
iv
gitlab-ce
Commits
5277ac1b
Commit
5277ac1b
authored
Oct 10, 2013
by
Marin Jankovski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added comments and split the methods further.
parent
7dbbb6de
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
13 deletions
+46
-13
app/helpers/gitlab_markdown_helper.rb
app/helpers/gitlab_markdown_helper.rb
+46
-13
No files found.
app/helpers/gitlab_markdown_helper.rb
View file @
5277ac1b
...
@@ -59,20 +59,48 @@ module GitlabMarkdownHelper
...
@@ -59,20 +59,48 @@ module GitlabMarkdownHelper
end
end
end
end
# text - whole text from a markdown file
# project_path_with_namespace - namespace/projectname
# ref - name of the branch or reference
# 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
,
wiki
=
false
)
links
=
extract_paths
(
text
)
paths
=
extract_paths
(
text
)
links
.
each
do
|
string
|
paths
.
each
do
|
path
|
new_link
=
new_link
(
project_path_with_namespace
,
string
,
ref
)
new_path
=
rebuild_path
(
project_path_with_namespace
,
path
,
ref
)
text
.
gsub!
(
"](
#{
string
}
)"
,
"](/
#{
new_link
}
)"
)
# 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
}
)"
)
end
end
text
text
end
end
def
extract_paths
(
text
)
def
extract_paths
(
markdown_text
)
text
.
split
(
"
\n
"
).
map
{
|
a
|
a
.
scan
(
/\]\(([^(]+)\)/
)
}.
reject
{
|
b
|
b
.
empty?
}.
flatten
.
reject
{
|
c
|
c
.
include?
(
"http"
||
"www"
)}
all_markdown_paths
=
pick_out_paths
(
markdown_text
)
paths
=
remove_empty
(
all_markdown_paths
)
select_relative
(
paths
)
end
end
def
new_link
(
path_with_namespace
,
string
,
ref
)
# Split the markdown text to each line and find all paths, this will match anything with - ]("some_text")
def
pick_out_paths
(
markdown_text
)
markdown_text
.
split
(
"
\n
"
).
map
{
|
text
|
text
.
scan
(
/\]\(([^(]+)\)/
)
}
end
# Removes any empty result produced by not matching the regexp
def
remove_empty
(
paths
)
paths
.
reject
{
|
l
|
l
.
empty?
}.
flatten
end
# Reject any path that contains ignored protocol
# eg. reject "https://gitlab.org} but accept "doc/api/README.md"
def
select_relative
(
paths
)
paths
.
reject
{
|
path
|
ignored_protocols
.
map
{
|
protocol
|
path
.
include?
(
protocol
)}.
any?
}
end
def
ignored_protocols
[
"http://"
,
"https://"
,
"ftp://"
,
"mailto:"
]
end
def
rebuild_path
(
path_with_namespace
,
string
,
ref
)
[
[
path_with_namespace
,
path_with_namespace
,
path_with_ref
(
string
,
ref
),
path_with_ref
(
string
,
ref
),
...
@@ -80,19 +108,24 @@ module GitlabMarkdownHelper
...
@@ -80,19 +108,24 @@ module GitlabMarkdownHelper
].
compact
.
join
(
"/"
)
].
compact
.
join
(
"/"
)
end
end
def
path_with_ref
(
string
,
ref
)
# Checks if the path exists in the repo
if
File
.
exists?
(
Rails
.
root
.
join
(
string
))
# eg. checks if doc/README.md exists, if it doesn't then it is a wiki link
"
#{
local_path
(
string
)
}
/
#{
correct_ref
(
ref
)
}
"
def
path_with_ref
(
path
,
ref
)
if
File
.
exists?
(
Rails
.
root
.
join
(
path
))
"
#{
local_path
(
path
)
}
/
#{
correct_ref
(
ref
)
}
"
else
else
"wikis"
"wikis"
end
end
end
end
def
local_path
(
string
)
# Check if the path is pointing to a directory(tree) or a file(blob)
File
.
directory?
(
Rails
.
root
.
join
(
string
))
?
"tree"
:
"blob"
# eg. doc/api is directory and doc/README.md is file
def
local_path
(
path
)
File
.
directory?
(
Rails
.
root
.
join
(
path
))
?
"tree"
:
"blob"
end
end
# We will assume that if no ref exists we can point to master
def
correct_ref
(
ref
)
def
correct_ref
(
ref
)
ref
?
ref
:
'master'
ref
?
ref
:
"master"
end
end
end
end
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