2015-05-29 01:56:30 -04:00
|
|
|
require 'nokogiri'
|
|
|
|
|
2012-08-02 20:28:02 -04:00
|
|
|
module GitlabMarkdownHelper
|
2012-09-05 16:06:03 -04:00
|
|
|
include Gitlab::Markdown
|
2015-06-05 15:53:57 -04:00
|
|
|
include PreferencesHelper
|
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
|
|
|
|
2015-04-10 12:30:02 -04:00
|
|
|
escaped_body = if body =~ /\A\<img/
|
2013-01-29 04:00:56 -05:00
|
|
|
body
|
|
|
|
else
|
|
|
|
escape_once(body)
|
|
|
|
end
|
|
|
|
|
2015-05-14 07:05:33 -04:00
|
|
|
gfm_body = gfm(escaped_body, {}, html_options)
|
2012-08-02 20:28:02 -04:00
|
|
|
|
2015-05-29 01:56:30 -04:00
|
|
|
fragment = Nokogiri::XML::DocumentFragment.parse(gfm_body)
|
|
|
|
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
|
2012-08-02 20:28:02 -04:00
|
|
|
end
|
|
|
|
|
2015-05-29 01:56:30 -04:00
|
|
|
fragment.to_html.html_safe
|
2012-08-02 20:28:02 -04:00
|
|
|
end
|
2012-08-08 04:52:09 -04:00
|
|
|
|
2015-06-02 07:17:11 -04:00
|
|
|
MARKDOWN_OPTIONS = {
|
|
|
|
no_intra_emphasis: true,
|
|
|
|
tables: true,
|
|
|
|
fenced_code_blocks: true,
|
|
|
|
strikethrough: true,
|
|
|
|
lax_spacing: true,
|
|
|
|
space_after_headers: true,
|
|
|
|
superscript: true,
|
|
|
|
footnotes: true
|
|
|
|
}.freeze
|
|
|
|
|
2014-02-04 02:48:33 -05:00
|
|
|
def markdown(text, options={})
|
2015-03-24 21:28:10 -04:00
|
|
|
unless @markdown && options == @options
|
2014-02-04 02:48:33 -05:00
|
|
|
@options = options
|
2015-04-08 12:35:57 -04:00
|
|
|
|
2015-05-14 07:05:33 -04:00
|
|
|
# see https://github.com/vmg/redcarpet#darling-i-packed-you-a-couple-renderers-for-lunch
|
|
|
|
rend = Redcarpet::Render::GitlabHTML.new(self, user_color_scheme_class, options)
|
2015-04-08 12:35:57 -04:00
|
|
|
|
|
|
|
# see https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use
|
2015-06-02 07:17:11 -04:00
|
|
|
@markdown = Redcarpet::Markdown.new(rend, MARKDOWN_OPTIONS)
|
2012-08-27 15:20:13 -04:00
|
|
|
end
|
2015-04-08 12:35:57 -04:00
|
|
|
|
2012-08-27 15:20:13 -04:00
|
|
|
@markdown.render(text).html_safe
|
2012-08-08 04:52:09 -04:00
|
|
|
end
|
2013-03-14 02:31:08 -04:00
|
|
|
|
2015-05-12 19:07:48 -04:00
|
|
|
def asciidoc(text)
|
|
|
|
Gitlab::Asciidoc.render(text, {
|
|
|
|
commit: @commit,
|
|
|
|
project: @project,
|
|
|
|
project_wiki: @project_wiki,
|
|
|
|
requested_path: @path,
|
|
|
|
ref: @ref
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
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.
|
2015-05-14 07:05:33 -04:00
|
|
|
def first_line_in_markdown(text, max_chars = nil, options = {})
|
|
|
|
md = markdown(text, options).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)
|
2015-05-12 19:07:48 -04:00
|
|
|
case wiki_page.format
|
|
|
|
when :markdown
|
2013-03-14 02:31:08 -04:00
|
|
|
markdown(wiki_page.content)
|
2015-05-12 19:07:48 -04:00
|
|
|
when :asciidoc
|
|
|
|
asciidoc(wiki_page.content)
|
2013-03-14 02:31:08 -04:00
|
|
|
else
|
|
|
|
wiki_page.formatted_content.html_safe
|
|
|
|
end
|
|
|
|
end
|
2013-10-08 06:24:50 -04:00
|
|
|
|
2015-06-23 21:00:10 -04:00
|
|
|
MARKDOWN_TIPS = [
|
|
|
|
"End a line with two or more spaces for a line-break, or soft-return",
|
|
|
|
"Inline code can be denoted by `surrounding it with backticks`",
|
|
|
|
"Blocks of code can be denoted by three backticks ``` or four leading spaces",
|
|
|
|
"Emoji can be added by :emoji_name:, for example :thumbsup:",
|
|
|
|
"Notify other participants using @user_name",
|
|
|
|
"Notify a specific group using @group_name",
|
|
|
|
"Notify the entire team using @all",
|
|
|
|
"Reference an issue using a hash, for example issue #123",
|
|
|
|
"Reference a merge request using an exclamation point, for example MR !123",
|
|
|
|
"Italicize words or phrases using *asterisks* or _underscores_",
|
|
|
|
"Bold words or phrases using **double asterisks** or __double underscores__",
|
|
|
|
"Strikethrough words or phrases using ~~two tildes~~",
|
|
|
|
"Make a bulleted list using + pluses, - minuses, or * asterisks",
|
|
|
|
"Denote blockquotes using > at the beginning of a line",
|
|
|
|
"Make a horizontal line using three or more hyphens ---, asterisks ***, or underscores ___"
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
# Returns a random markdown tip for use as a textarea placeholder
|
|
|
|
def random_markdown_tip
|
2015-07-09 16:38:33 -04:00
|
|
|
MARKDOWN_TIPS.sample
|
2015-06-23 21:00:10 -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
|
|
|
|
2015-04-20 18:47:22 -04:00
|
|
|
# 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
|
2014-10-20 05:56:01 -04:00
|
|
|
def cross_project_reference(project, entity)
|
2015-04-20 18:47:22 -04:00
|
|
|
if entity.respond_to?(:to_reference)
|
|
|
|
"#{project.to_reference}#{entity.to_reference}"
|
2014-10-20 05:56:01 -04:00
|
|
|
else
|
2015-04-20 18:47:22 -04:00
|
|
|
''
|
2014-10-20 05:56:01 -04:00
|
|
|
end
|
|
|
|
end
|
2012-08-02 20:28:02 -04:00
|
|
|
end
|