gitlab-org--gitlab-foss/app/helpers/gitlab_markdown_helper.rb

76 lines
2.6 KiB
Ruby
Raw Normal View History

module GitlabMarkdownHelper
include Gitlab::Markdown
2012-08-23 18:10:06 +00: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>").
def link_to_gfm(body, url, html_options = {})
2012-09-10 15:32:31 +00:00
return "" if body.blank?
escaped_body = if body =~ /^\<img/
body
else
escape_once(body)
end
gfm_body = gfm(escaped_body, html_options)
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 08:52:09 +00:00
def markdown(text)
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,
safe_links_only: true)
@markdown = Redcarpet::Markdown.new(gitlab_renderer,
# 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 08:52:09 +00:00
@markdown.render(text).html_safe
2012-08-08 08:52:09 +00:00
end
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 12:21:01 +00:00
def create_relative_links(text, project_path_with_namespace, ref, wiki = false)
2013-10-08 11:02:30 +00:00
links = text.split("\n").map { |a| a.scan(/\]\(([^(]+)\)/) }.reject{|b| b.empty? }.flatten.reject{|c| c.include?("http" || "www")}
links.each do |string|
2013-10-08 12:21:01 +00:00
new_link = [
project_path_with_namespace,
wiki ? "wikis":"blob",
2013-10-08 12:21:01 +00:00
ref,
string
].compact.join("/")
text.gsub!("](#{string})", "](/#{new_link})")
end
text
end
end