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
Léo-Paul Géneau
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
Show 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,11 +69,18 @@ module GitlabMarkdownHelper
...
@@ -69,11 +69,18 @@ module GitlabMarkdownHelper
project_path_with_namespace
=
project
.
path_with_namespace
project_path_with_namespace
=
project
.
path_with_namespace
paths
=
extract_paths
(
text
)
paths
=
extract_paths
(
text
)
paths
.
each
do
|
file_path
|
paths
.
each
do
|
file_path
|
new_path
=
rebuild_path
(
project_path_with_namespace
,
file_path
,
requested_path
,
ref
)
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
# 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
# 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
}
)"
)
text
.
gsub!
(
"](
#{
file_path
}
)"
,
"](/
#{
new_path
}
)"
)
end
end
end
text
text
end
end
...
@@ -83,9 +90,11 @@ module GitlabMarkdownHelper
...
@@ -83,9 +90,11 @@ module GitlabMarkdownHelper
select_relative
(
paths
)
select_relative
(
paths
)
end
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
)
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
end
# Removes any empty result produced by not matching the regexp
# Removes any empty result produced by not matching the regexp
...
@@ -93,12 +102,22 @@ module GitlabMarkdownHelper
...
@@ -93,12 +102,22 @@ module GitlabMarkdownHelper
paths
.
reject
{
|
l
|
l
.
empty?
}.
flatten
paths
.
reject
{
|
l
|
l
.
empty?
}.
flatten
end
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
# Reject any path that contains ignored protocol
# eg. reject "https://gitlab.org} but accept "doc/api/README.md"
# eg. reject "https://gitlab.org} but accept "doc/api/README.md"
def
select_relative
(
paths
)
def
select_relative
(
paths
)
paths
.
reject
{
|
path
|
ignored_protocols
.
map
{
|
protocol
|
path
.
include?
(
protocol
)}.
any?
}
paths
.
reject
{
|
path
|
ignored_protocols
.
map
{
|
protocol
|
path
.
include?
(
protocol
)}.
any?
}
end
end
# Check whether a path is a reference-style link
def
reference_path?
(
path
)
path
.
include?
(
"]: "
)
end
def
ignored_protocols
def
ignored_protocols
[
"http://"
,
"https://"
,
"ftp://"
,
"mailto:"
]
[
"http://"
,
"https://"
,
"ftp://"
,
"mailto:"
]
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