2012-08-03 02:28:02 +02:00
|
|
|
module GitlabMarkdownHelper
|
2012-09-05 22:06:03 +02:00
|
|
|
include Gitlab::Markdown
|
2012-08-03 02:28:02 +02:00
|
|
|
|
2012-08-23 20:10:06 +02: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-03 02:28:02 +02:00
|
|
|
def link_to_gfm(body, url, html_options = {})
|
2012-09-10 17:32:31 +02:00
|
|
|
return "" if body.blank?
|
2012-09-20 01:42:26 +02:00
|
|
|
|
|
|
|
gfm_body = gfm(escape_once(body), html_options)
|
2012-08-03 02:28:02 +02: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 10:52:09 +02:00
|
|
|
|
|
|
|
def markdown(text)
|
2012-08-27 21:20:13 +02:00
|
|
|
unless @markdown
|
|
|
|
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,
|
|
|
|
hard_wrap: true)
|
2012-09-08 01:11:32 +02:00
|
|
|
@markdown = Redcarpet::Markdown.new(gitlab_renderer,
|
2012-08-27 21:20:13 +02: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,
|
|
|
|
lax_html_blocks: true,
|
|
|
|
space_after_headers: true,
|
|
|
|
superscript: true)
|
|
|
|
end
|
2012-08-08 10:52:09 +02:00
|
|
|
|
2012-08-27 21:20:13 +02:00
|
|
|
@markdown.render(text).html_safe
|
2012-08-08 10:52:09 +02:00
|
|
|
end
|
2012-08-03 02:28:02 +02:00
|
|
|
end
|