debian-mirror-gitlab/app/helpers/gitlab_markdown_helper.rb

189 lines
5.6 KiB
Ruby
Raw Normal View History

2015-09-11 14:41:01 +05:30
require 'nokogiri'
2014-09-02 18:07:02 +05:30
module GitlabMarkdownHelper
# 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>").
def link_to_gfm(body, url, html_options = {})
return "" if body.blank?
escaped_body = if body.start_with?('<img')
2014-09-02 18:07:02 +05:30
body
else
escape_once(body)
end
2015-10-24 18:46:33 +05:30
user = current_user if defined?(current_user)
2015-12-23 02:04:40 +05:30
gfm_body = Banzai.render(escaped_body, project: @project, current_user: user, pipeline: :single_line)
2014-09-02 18:07:02 +05:30
2015-10-24 18:46:33 +05:30
fragment = Nokogiri::HTML::DocumentFragment.parse(gfm_body)
2015-09-11 14:41:01 +05:30
if fragment.children.size == 1 && fragment.children[0].name == 'a'
# Fragment has only one node, and it's a link generated by `gfm`.
# Replace it with our requested link.
text = fragment.children[0].text
fragment.children[0].replace(link_to(text, url, html_options))
else
# Traverse the fragment's first generation of children looking for pure
# text, wrapping anything found in the requested link
fragment.children.each do |node|
next unless node.text?
node.replace(link_to(node.text, url, html_options))
end
2014-09-02 18:07:02 +05:30
end
2015-09-25 12:07:36 +05:30
# Add any custom CSS classes to the GFM-generated reference links
if html_options[:class]
fragment.css('a.gfm').add_class(html_options[:class])
end
2015-09-11 14:41:01 +05:30
fragment.to_html.html_safe
2014-09-02 18:07:02 +05:30
end
2015-09-25 12:07:36 +05:30
def markdown(text, context = {})
2015-10-24 18:46:33 +05:30
return "" unless text.present?
2015-12-23 02:04:40 +05:30
context[:project] ||= @project
2015-04-26 12:48:37 +05:30
2016-06-02 11:05:42 +05:30
text = Banzai.pre_process(text, context)
2015-12-23 02:04:40 +05:30
html = Banzai.render(text, context)
2015-10-24 18:46:33 +05:30
2015-12-23 02:04:40 +05:30
context.merge!(
current_user: (current_user if defined?(current_user)),
2015-04-26 12:48:37 +05:30
2015-12-23 02:04:40 +05:30
# RelativeLinkFilter
requested_path: @path,
project_wiki: @project_wiki,
ref: @ref
2015-09-25 12:07:36 +05:30
)
2015-04-26 12:48:37 +05:30
2015-12-23 02:04:40 +05:30
Banzai.post_process(html, context)
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
def asciidoc(text)
2015-12-23 02:04:40 +05:30
Gitlab::Asciidoc.render(
text,
project: @project,
2016-04-02 18:10:28 +05:30
current_user: (current_user if defined?(current_user)),
# RelativeLinkFilter
project_wiki: @project_wiki,
requested_path: @path,
ref: @ref,
commit: @commit
)
end
def other_markup(file_name, text)
Gitlab::OtherMarkup.render(
file_name,
text,
project: @project,
2015-12-23 02:04:40 +05:30
current_user: (current_user if defined?(current_user)),
# RelativeLinkFilter
project_wiki: @project_wiki,
2015-09-11 14:41:01 +05:30
requested_path: @path,
2015-12-23 02:04:40 +05:30
ref: @ref,
commit: @commit
)
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
# 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.
2015-09-11 14:41:01 +05:30
def first_line_in_markdown(text, max_chars = nil, options = {})
md = markdown(text, options).strip
2015-04-26 12:48:37 +05:30
truncate_visible(md, max_chars || md.length) if md.present?
end
2014-09-02 18:07:02 +05:30
def render_wiki_content(wiki_page)
2015-09-11 14:41:01 +05:30
case wiki_page.format
when :markdown
markdown(wiki_page.content, pipeline: :wiki, project_wiki: @project_wiki, page_slug: wiki_page.slug)
2015-09-11 14:41:01 +05:30
when :asciidoc
asciidoc(wiki_page.content)
2014-09-02 18:07:02 +05:30
else
wiki_page.formatted_content.html_safe
end
end
2015-04-26 12:48:37 +05:30
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
truncated = false
doc.traverse do |node|
if node.text? || node.content.empty?
if truncated
node.remove
next
end
# Handle line breaks within a node
if node.content.strip.lines.length > 1
node.content = "#{node.content.lines.first.chomp}..."
truncated = true
end
num_remaining = max_chars - content_length
if node.content.length > num_remaining
node.content = node.content.truncate(num_remaining)
truncated = true
end
content_length += node.content.length
end
truncated = truncate_if_block(node, truncated)
end
doc.to_html
end
# 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
2015-10-24 18:46:33 +05:30
node.inner_html = "#{node.inner_html}..." if node.next_sibling
2015-04-26 12:48:37 +05:30
true
else
truncated
end
end
2015-09-11 14:41:01 +05:30
# Returns the text necessary to reference `entity` across projects
#
# project - Project to reference
# entity - Object that responds to `to_reference`
#
# Examples:
#
# cross_project_reference(project, project.issues.first)
# # => 'namespace1/project1#123'
#
# cross_project_reference(project, project.merge_requests.first)
# # => 'namespace1/project1!345'
#
# Returns a String
2015-04-26 12:48:37 +05:30
def cross_project_reference(project, entity)
2015-09-11 14:41:01 +05:30
if entity.respond_to?(:to_reference)
"#{project.to_reference}#{entity.to_reference}"
2015-04-26 12:48:37 +05:30
else
2015-09-11 14:41:01 +05:30
''
2015-04-26 12:48:37 +05:30
end
end
2014-09-02 18:07:02 +05:30
end