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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
16ba8fa0
Commit
16ba8fa0
authored
Jan 22, 2014
by
Marin Jankovski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Display correct paths in markdown for reference style links.
parent
1018cbdc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
6 deletions
+25
-6
app/helpers/gitlab_markdown_helper.rb
app/helpers/gitlab_markdown_helper.rb
+25
-6
No files found.
app/helpers/gitlab_markdown_helper.rb
View file @
16ba8fa0
...
...
@@ -69,10 +69,17 @@ module GitlabMarkdownHelper
project_path_with_namespace
=
project
.
path_with_namespace
paths
=
extract_paths
(
text
)
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!
(
"](
#{
file_path
}
)"
,
"](/
#{
new_path
}
)"
)
original_file_path
=
extract
(
file_path
)
new_path
=
rebuild_path
(
project_path_with_namespace
,
original_file_path
,
requested_path
,
ref
)
if
reference_path?
(
file_path
)
# Replacing old string with a new one that contains updated path
# eg. [some document]: document.md will be replaced with [some document] /namespace/project/master/blob/document.md
text
.
gsub!
(
file_path
,
file_path
.
gsub
(
original_file_path
,
"/
#{
new_path
}
"
))
else
# 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!
(
"](
#{
file_path
}
)"
,
"](/
#{
new_path
}
)"
)
end
end
text
end
...
...
@@ -83,9 +90,11 @@ module GitlabMarkdownHelper
select_relative
(
paths
)
end
# Split the markdown text to each line and find all paths, this will match anything with - ]("some_text")
# Split the markdown text to each line and find all paths, this will match anything with - ]("some_text")
and [some text]: file.md
def
pick_out_paths
(
markdown_text
)
markdown_text
.
split
(
"
\n
"
).
map
{
|
text
|
text
.
scan
(
/\]\(([^(]+)\)/
)
}
inline_paths
=
markdown_text
.
split
(
"
\n
"
).
map
{
|
text
|
text
.
scan
(
/\]\(([^(]+)\)/
)
}
reference_paths
=
markdown_text
.
split
(
"
\n
"
).
map
{
|
text
|
text
.
scan
(
/\[.*\]:.*/
)
}
inline_paths
+
reference_paths
end
# Removes any empty result produced by not matching the regexp
...
...
@@ -93,12 +102,22 @@ module GitlabMarkdownHelper
paths
.
reject
{
|
l
|
l
.
empty?
}.
flatten
end
# If a path is a reference style link we need to omit ]:
def
extract
(
path
)
path
.
split
(
"]: "
).
last
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
# Check whether a path is a reference-style link
def
reference_path?
(
path
)
path
.
include?
(
"]: "
)
end
def
ignored_protocols
[
"http://"
,
"https://"
,
"ftp://"
,
"mailto:"
]
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