2012-08-02 20:28:02 -04:00
|
|
|
module GitlabMarkdownHelper
|
2012-09-05 16:06:03 -04:00
|
|
|
include Gitlab::Markdown
|
2012-08-02 20:28:02 -04:00
|
|
|
|
2012-08-23 14:10:06 -04:00
|
|
|
# Use this in places where you would normally use link_to(gfm(...), ...).
|
|
|
|
#
|
|
|
|
# It solves a problem occurring with nested links (i.e.
|
|
|
|
# "<a>outer text <a>gfm ref</a> more outer text</a>"). This will not be
|
|
|
|
# interpreted as intended. Browsers will parse something like
|
|
|
|
# "<a>outer text </a><a>gfm ref</a> more outer text" (notice the last part is
|
|
|
|
# not linked any more). link_to_gfm corrects that. It wraps all parts to
|
|
|
|
# explicitly produce the correct linking behavior (i.e.
|
|
|
|
# "<a>outer text </a><a>gfm ref</a><a> more outer text</a>").
|
2012-08-02 20:28:02 -04:00
|
|
|
def link_to_gfm(body, url, html_options = {})
|
2012-09-10 11:32:31 -04:00
|
|
|
return "" if body.blank?
|
2012-09-19 19:42:26 -04:00
|
|
|
|
2013-01-29 04:00:56 -05:00
|
|
|
escaped_body = if body =~ /^\<img/
|
|
|
|
body
|
|
|
|
else
|
|
|
|
escape_once(body)
|
|
|
|
end
|
|
|
|
|
2014-05-26 08:40:10 -04:00
|
|
|
gfm_body = gfm(escaped_body, @project, html_options)
|
2012-08-02 20:28:02 -04:00
|
|
|
|
|
|
|
gfm_body.gsub!(%r{<a.*?>.*?</a>}m) do |match|
|
|
|
|
"</a>#{match}#{link_to("", url, html_options)[0..-5]}" # "</a>".length +1
|
|
|
|
end
|
|
|
|
|
|
|
|
link_to(gfm_body.html_safe, url, html_options)
|
|
|
|
end
|
2012-08-08 04:52:09 -04:00
|
|
|
|
2014-02-04 02:48:33 -05:00
|
|
|
def markdown(text, options={})
|
|
|
|
unless (@markdown and options == @options)
|
|
|
|
@options = options
|
|
|
|
gitlab_renderer = Redcarpet::Render::GitlabHTML.new(self, {
|
|
|
|
# see https://github.com/vmg/redcarpet#darling-i-packed-you-a-couple-renderers-for-lunch-
|
|
|
|
filter_html: true,
|
|
|
|
with_toc_data: true,
|
|
|
|
safe_links_only: true
|
|
|
|
}.merge(options))
|
2012-09-07 19:11:32 -04:00
|
|
|
@markdown = Redcarpet::Markdown.new(gitlab_renderer,
|
2012-08-27 15:20:13 -04:00
|
|
|
# see https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use
|
|
|
|
no_intra_emphasis: true,
|
|
|
|
tables: true,
|
|
|
|
fenced_code_blocks: true,
|
|
|
|
autolink: true,
|
|
|
|
strikethrough: true,
|
2014-03-14 07:33:03 -04:00
|
|
|
lax_spacing: true,
|
2012-08-27 15:20:13 -04:00
|
|
|
space_after_headers: true,
|
|
|
|
superscript: true)
|
|
|
|
end
|
|
|
|
@markdown.render(text).html_safe
|
2012-08-08 04:52:09 -04:00
|
|
|
end
|
2013-03-14 02:31:08 -04:00
|
|
|
|
2014-10-11 22:17:02 -04:00
|
|
|
# Return the first line of +text+, up to +max_chars+, after parsing the line
|
|
|
|
# as Markdown. HTML tags in the parsed output are not counted toward the
|
|
|
|
# +max_chars+ limit. If the length limit falls within a tag's contents, then
|
|
|
|
# the tag contents are truncated without removing the closing tag.
|
|
|
|
def first_line_in_markdown(text, max_chars = nil)
|
2014-10-13 00:07:18 -04:00
|
|
|
md = markdown(text).strip
|
2014-10-11 22:17:02 -04:00
|
|
|
|
2014-10-13 00:07:18 -04:00
|
|
|
truncate_visible(md, max_chars || md.length) if md.present?
|
2014-09-12 06:45:00 -04:00
|
|
|
end
|
|
|
|
|
2013-03-14 02:31:08 -04:00
|
|
|
def render_wiki_content(wiki_page)
|
|
|
|
if wiki_page.format == :markdown
|
|
|
|
markdown(wiki_page.content)
|
|
|
|
else
|
|
|
|
wiki_page.formatted_content.html_safe
|
|
|
|
end
|
|
|
|
end
|
2013-10-08 06:24:50 -04:00
|
|
|
|
2014-05-20 09:29:14 -04:00
|
|
|
def create_relative_links(text)
|
2013-10-10 05:16:15 -04:00
|
|
|
paths = extract_paths(text)
|
2013-10-09 07:28:25 -04:00
|
|
|
|
2014-05-20 09:29:14 -04:00
|
|
|
paths.uniq.each do |file_path|
|
2014-06-26 03:13:38 -04:00
|
|
|
# If project does not have repository
|
|
|
|
# its nothing to rebuild
|
2014-09-23 10:56:41 -04:00
|
|
|
#
|
|
|
|
# TODO: pass project variable to markdown helper instead of using
|
|
|
|
# instance variable. Right now it generates invalid path for pages out
|
|
|
|
# of project scope. Example: search results where can be rendered markdown
|
|
|
|
# from different projects
|
|
|
|
if @repository && @repository.exists? && !@repository.empty?
|
2014-06-26 03:13:38 -04:00
|
|
|
new_path = rebuild_path(file_path)
|
|
|
|
# Finds quoted path so we don't replace other mentions of the string
|
|
|
|
# eg. "doc/api" will be replaced and "/home/doc/api/text" won't
|
|
|
|
text.gsub!("\"#{file_path}\"", "\"/#{new_path}\"")
|
|
|
|
end
|
2014-05-20 09:29:14 -04:00
|
|
|
end
|
2013-10-09 07:28:25 -04:00
|
|
|
|
2014-05-20 09:29:14 -04:00
|
|
|
text
|
2013-10-10 05:16:15 -04:00
|
|
|
end
|
|
|
|
|
2014-05-20 09:29:14 -04:00
|
|
|
def extract_paths(text)
|
|
|
|
links = substitute_links(text)
|
|
|
|
image_links = substitute_image_links(text)
|
|
|
|
links + image_links
|
2013-10-10 05:16:15 -04:00
|
|
|
end
|
|
|
|
|
2014-05-20 09:29:14 -04:00
|
|
|
def substitute_links(text)
|
|
|
|
links = text.scan(/<a href=\"([^"]*)\">/)
|
|
|
|
relative_links = links.flatten.reject{ |link| link_to_ignore? link }
|
|
|
|
relative_links
|
2014-01-22 09:07:29 -05:00
|
|
|
end
|
|
|
|
|
2014-05-20 09:29:14 -04:00
|
|
|
def substitute_image_links(text)
|
|
|
|
links = text.scan(/<img src=\"([^"]*)\"/)
|
|
|
|
relative_links = links.flatten.reject{ |link| link_to_ignore? link }
|
|
|
|
relative_links
|
2013-10-10 05:16:15 -04:00
|
|
|
end
|
|
|
|
|
2014-05-20 09:29:14 -04:00
|
|
|
def link_to_ignore?(link)
|
2014-06-26 03:13:38 -04:00
|
|
|
if link =~ /\#\w+/
|
|
|
|
# ignore anchors like <a href="#my-header">
|
|
|
|
true
|
|
|
|
else
|
|
|
|
ignored_protocols.map{ |protocol| link.include?(protocol) }.any?
|
|
|
|
end
|
2014-01-22 09:07:29 -05:00
|
|
|
end
|
|
|
|
|
2013-10-10 05:16:15 -04:00
|
|
|
def ignored_protocols
|
|
|
|
["http://","https://", "ftp://", "mailto:"]
|
|
|
|
end
|
|
|
|
|
2014-05-20 09:29:14 -04:00
|
|
|
def rebuild_path(path)
|
2014-02-20 09:09:30 -05:00
|
|
|
path.gsub!(/(#.*)/, "")
|
|
|
|
id = $1 || ""
|
2014-05-20 09:29:14 -04:00
|
|
|
file_path = relative_file_path(path)
|
2014-05-20 09:58:30 -04:00
|
|
|
file_path = sanitize_slashes(file_path)
|
|
|
|
|
2013-10-09 07:28:25 -04:00
|
|
|
[
|
2014-05-20 09:29:14 -04:00
|
|
|
Gitlab.config.gitlab.relative_url_root,
|
|
|
|
@project.path_with_namespace,
|
|
|
|
path_with_ref(file_path),
|
2013-10-10 08:29:16 -04:00
|
|
|
file_path
|
2014-05-20 09:29:14 -04:00
|
|
|
].compact.join("/").gsub(/^\/*|\/*$/, '') + id
|
2013-10-09 07:28:25 -04:00
|
|
|
end
|
|
|
|
|
2014-05-20 09:58:30 -04:00
|
|
|
def sanitize_slashes(path)
|
|
|
|
path[0] = "" if path.start_with?("/")
|
|
|
|
path.chop if path.end_with?("/")
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
2014-05-20 09:29:14 -04:00
|
|
|
def relative_file_path(path)
|
|
|
|
requested_path = @path
|
2013-10-10 08:29:16 -04:00
|
|
|
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
|
2013-10-14 09:55:44 -04:00
|
|
|
# If we are at doc/api and the README.md shown in below the tree view
|
2014-07-10 08:59:01 -04:00
|
|
|
# this takes the request path(doc/api) and adds users.md so the path looks like doc/api/users.md
|
2013-10-10 08:29:16 -04:00
|
|
|
def build_nested_path(path, request_path)
|
2014-02-20 09:09:30 -05:00
|
|
|
return request_path if path == ""
|
2013-10-10 08:29:16 -04:00
|
|
|
return path unless request_path
|
2013-10-14 09:55:44 -04:00
|
|
|
if local_path(request_path) == "tree"
|
|
|
|
base = request_path.split("/").push(path)
|
|
|
|
base.join("/")
|
|
|
|
else
|
|
|
|
base = request_path.split("/")
|
|
|
|
base.pop
|
|
|
|
base.push(path).join("/")
|
|
|
|
end
|
2013-10-10 08:29:16 -04:00
|
|
|
end
|
|
|
|
|
2014-05-20 09:29:14 -04:00
|
|
|
# Checks if the path exists in the repo
|
|
|
|
# eg. checks if doc/README.md exists, if not then link to blob
|
|
|
|
def path_with_ref(path)
|
|
|
|
if file_exists?(path)
|
|
|
|
"#{local_path(path)}/#{correct_ref}"
|
|
|
|
else
|
|
|
|
"blob/#{correct_ref}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-08 11:20:13 -05:00
|
|
|
def file_exists?(path)
|
2014-02-20 09:09:30 -05:00
|
|
|
return false if path.nil?
|
2014-02-04 12:14:36 -05:00
|
|
|
return @repository.blob_at(current_sha, path).present? || @repository.tree(current_sha, path).entries.any?
|
2013-10-10 08:29:16 -04:00
|
|
|
end
|
|
|
|
|
2013-10-10 05:16:15 -04:00
|
|
|
# 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)
|
2014-02-04 12:14:36 -05:00
|
|
|
return "tree" if @repository.tree(current_sha, path).entries.any?
|
|
|
|
return "raw" if @repository.blob_at(current_sha, path).image?
|
2014-01-29 08:48:59 -05:00
|
|
|
return "blob"
|
2013-11-08 11:20:13 -05:00
|
|
|
end
|
|
|
|
|
2014-02-04 12:14:36 -05:00
|
|
|
def current_sha
|
|
|
|
if @commit
|
|
|
|
@commit.id
|
2014-06-26 03:13:38 -04:00
|
|
|
elsif @repository && !@repository.empty?
|
2014-08-06 05:14:53 -04:00
|
|
|
if @ref
|
|
|
|
@repository.commit(@ref).try(:sha)
|
|
|
|
else
|
|
|
|
@repository.head_commit.sha
|
|
|
|
end
|
2014-02-04 12:14:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-10 05:16:15 -04:00
|
|
|
# We will assume that if no ref exists we can point to master
|
2014-05-20 09:29:14 -04:00
|
|
|
def correct_ref
|
|
|
|
@ref ? @ref : "master"
|
2013-10-09 07:28:25 -04:00
|
|
|
end
|
2014-10-11 22:17:02 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Return +text+, truncated to +max_chars+ characters, excluding any HTML
|
|
|
|
# tags.
|
|
|
|
def truncate_visible(text, max_chars)
|
|
|
|
doc = Nokogiri::HTML.fragment(text)
|
|
|
|
content_length = 0
|
2014-10-13 00:07:18 -04:00
|
|
|
truncated = false
|
2014-10-11 22:17:02 -04:00
|
|
|
|
|
|
|
doc.traverse do |node|
|
|
|
|
if node.text? || node.content.empty?
|
2014-10-13 00:07:18 -04:00
|
|
|
if truncated
|
2014-10-11 22:17:02 -04:00
|
|
|
node.remove
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2014-10-13 00:07:18 -04:00
|
|
|
# Handle line breaks within a node
|
|
|
|
if node.content.strip.lines.length > 1
|
|
|
|
node.content = "#{node.content.lines.first.chomp}..."
|
|
|
|
truncated = true
|
|
|
|
end
|
|
|
|
|
2014-10-11 22:17:02 -04:00
|
|
|
num_remaining = max_chars - content_length
|
|
|
|
if node.content.length > num_remaining
|
|
|
|
node.content = node.content.truncate(num_remaining)
|
2014-10-13 00:07:18 -04:00
|
|
|
truncated = true
|
2014-10-11 22:17:02 -04:00
|
|
|
end
|
|
|
|
content_length += node.content.length
|
|
|
|
end
|
2014-10-13 00:07:18 -04:00
|
|
|
|
|
|
|
truncated = truncate_if_block(node, truncated)
|
2014-10-11 22:17:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
doc.to_html
|
|
|
|
end
|
2014-10-13 00:07:18 -04:00
|
|
|
|
|
|
|
# Used by #truncate_visible. If +node+ is the first block element, and the
|
|
|
|
# text hasn't already been truncated, then append "..." to the node contents
|
|
|
|
# and return true. Otherwise return false.
|
|
|
|
def truncate_if_block(node, truncated)
|
|
|
|
if node.element? && node.description.block? && !truncated
|
|
|
|
node.content = "#{node.content}..." if node.next_sibling
|
|
|
|
true
|
|
|
|
else
|
|
|
|
truncated
|
|
|
|
end
|
|
|
|
end
|
2014-10-20 05:56:01 -04:00
|
|
|
|
|
|
|
def cross_project_reference(project, entity)
|
|
|
|
path = project.path_with_namespace
|
|
|
|
|
|
|
|
if entity.kind_of?(Issue)
|
|
|
|
[path, entity.iid].join('#')
|
|
|
|
elsif entity.kind_of?(MergeRequest)
|
|
|
|
[path, entity.iid].join('!')
|
|
|
|
else
|
|
|
|
raise 'Not supported type'
|
|
|
|
end
|
|
|
|
end
|
2012-08-02 20:28:02 -04:00
|
|
|
end
|