2014-09-01 11:47:28 -04:00
|
|
|
require 'html/pipeline'
|
|
|
|
|
2012-08-14 04:32:19 -04:00
|
|
|
module Gitlab
|
2012-09-06 03:50:47 -04:00
|
|
|
# Custom parser for GitLab-flavored Markdown
|
2012-08-23 14:10:06 -04:00
|
|
|
#
|
2015-04-27 22:01:46 -04:00
|
|
|
# See the files in `lib/gitlab/markdown/` for specific processing information.
|
2012-09-05 16:07:39 -04:00
|
|
|
module Markdown
|
2015-08-27 16:09:01 -04:00
|
|
|
# Convert a Markdown String into an HTML-safe String of HTML
|
|
|
|
#
|
|
|
|
# markdown - Markdown String
|
|
|
|
# context - Hash of context options passed to our HTML Pipeline
|
|
|
|
#
|
|
|
|
# Returns an HTML-safe String
|
|
|
|
def self.render(markdown, context = {})
|
|
|
|
html = renderer.render(markdown)
|
|
|
|
html = gfm(html, context)
|
|
|
|
|
|
|
|
html.html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert a Markdown String into HTML without going through the HTML
|
|
|
|
# Pipeline.
|
|
|
|
#
|
|
|
|
# Note that because the pipeline is skipped, SanitizationFilter is as well.
|
|
|
|
# Do not output the result of this method to the user.
|
|
|
|
#
|
|
|
|
# markdown - Markdown String
|
|
|
|
#
|
|
|
|
# Returns a String
|
|
|
|
def self.render_without_gfm(markdown)
|
2015-08-31 16:22:34 -04:00
|
|
|
renderer.render(markdown)
|
2015-08-27 16:09:01 -04:00
|
|
|
end
|
|
|
|
|
2015-04-23 12:58:55 -04:00
|
|
|
# Provide autoload paths for filters to prevent a circular dependency error
|
2015-04-27 18:54:13 -04:00
|
|
|
autoload :AutolinkFilter, 'gitlab/markdown/autolink_filter'
|
2015-04-23 12:58:55 -04:00
|
|
|
autoload :CommitRangeReferenceFilter, 'gitlab/markdown/commit_range_reference_filter'
|
|
|
|
autoload :CommitReferenceFilter, 'gitlab/markdown/commit_reference_filter'
|
|
|
|
autoload :EmojiFilter, 'gitlab/markdown/emoji_filter'
|
|
|
|
autoload :ExternalIssueReferenceFilter, 'gitlab/markdown/external_issue_reference_filter'
|
2015-05-27 15:39:08 -04:00
|
|
|
autoload :ExternalLinkFilter, 'gitlab/markdown/external_link_filter'
|
2015-04-23 12:58:55 -04:00
|
|
|
autoload :IssueReferenceFilter, 'gitlab/markdown/issue_reference_filter'
|
|
|
|
autoload :LabelReferenceFilter, 'gitlab/markdown/label_reference_filter'
|
|
|
|
autoload :MergeRequestReferenceFilter, 'gitlab/markdown/merge_request_reference_filter'
|
2015-05-10 19:34:32 -04:00
|
|
|
autoload :RelativeLinkFilter, 'gitlab/markdown/relative_link_filter'
|
2015-04-27 18:56:37 -04:00
|
|
|
autoload :SanitizationFilter, 'gitlab/markdown/sanitization_filter'
|
2015-04-23 12:58:55 -04:00
|
|
|
autoload :SnippetReferenceFilter, 'gitlab/markdown/snippet_reference_filter'
|
2015-08-27 16:09:01 -04:00
|
|
|
autoload :SyntaxHighlightFilter, 'gitlab/markdown/syntax_highlight_filter'
|
2015-04-22 16:40:25 -04:00
|
|
|
autoload :TableOfContentsFilter, 'gitlab/markdown/table_of_contents_filter'
|
2015-05-18 15:44:45 -04:00
|
|
|
autoload :TaskListFilter, 'gitlab/markdown/task_list_filter'
|
2015-04-23 12:58:55 -04:00
|
|
|
autoload :UserReferenceFilter, 'gitlab/markdown/user_reference_filter'
|
|
|
|
|
2012-09-05 16:07:39 -04:00
|
|
|
# Public: Parse the provided text with GitLab-Flavored Markdown
|
|
|
|
#
|
|
|
|
# text - the source text
|
2015-04-29 17:53:32 -04:00
|
|
|
# options - A Hash of options used to customize output (default: {}):
|
|
|
|
# :xhtml - output XHTML instead of HTML
|
|
|
|
# :reference_only_path - Use relative path for reference links
|
2015-08-27 16:09:01 -04:00
|
|
|
def self.gfm(text, options = {})
|
2012-09-05 16:07:39 -04:00
|
|
|
return text if text.nil?
|
|
|
|
|
2012-09-19 20:21:35 -04:00
|
|
|
# Duplicate the string so we don't alter the original, then call to_str
|
|
|
|
# to cast it back to a String instead of a SafeBuffer. This is required
|
|
|
|
# for gsub calls to work as we need them to.
|
|
|
|
text = text.dup.to_str
|
2012-09-07 19:58:12 -04:00
|
|
|
|
2015-03-25 05:03:55 -04:00
|
|
|
options.reverse_merge!(
|
|
|
|
xhtml: false,
|
2015-05-14 07:05:33 -04:00
|
|
|
reference_only_path: true,
|
2015-08-27 16:09:01 -04:00
|
|
|
project: options[:project],
|
|
|
|
current_user: options[:current_user]
|
2015-03-25 05:03:55 -04:00
|
|
|
)
|
|
|
|
|
2015-06-29 05:38:48 -04:00
|
|
|
@pipeline ||= HTML::Pipeline.new(filters)
|
2015-04-16 17:39:09 -04:00
|
|
|
|
|
|
|
context = {
|
2015-05-29 19:01:12 -04:00
|
|
|
# SanitizationFilter
|
|
|
|
pipeline: options[:pipeline],
|
|
|
|
|
2015-04-16 17:39:09 -04:00
|
|
|
# EmojiFilter
|
2015-09-06 12:42:39 -04:00
|
|
|
asset_root: Gitlab.config.gitlab.base_url,
|
2015-04-23 12:58:55 -04:00
|
|
|
asset_host: Gitlab::Application.config.asset_host,
|
2015-04-16 17:39:09 -04:00
|
|
|
|
2015-04-22 16:40:25 -04:00
|
|
|
# TableOfContentsFilter
|
|
|
|
no_header_anchors: options[:no_header_anchors],
|
|
|
|
|
2015-04-16 17:39:09 -04:00
|
|
|
# ReferenceFilter
|
2015-05-14 07:05:33 -04:00
|
|
|
current_user: options[:current_user],
|
2015-04-16 17:39:09 -04:00
|
|
|
only_path: options[:reference_only_path],
|
2015-05-14 07:05:33 -04:00
|
|
|
project: options[:project],
|
2015-05-10 19:34:32 -04:00
|
|
|
|
|
|
|
# RelativeLinkFilter
|
2015-08-27 16:09:01 -04:00
|
|
|
ref: options[:ref],
|
|
|
|
requested_path: options[:path],
|
|
|
|
project_wiki: options[:project_wiki]
|
2014-09-01 11:47:28 -04:00
|
|
|
}
|
|
|
|
|
2015-06-29 05:38:48 -04:00
|
|
|
result = @pipeline.call(text, context)
|
2015-03-31 22:03:54 -04:00
|
|
|
|
2015-03-25 05:03:55 -04:00
|
|
|
save_options = 0
|
Fix invalid Atom feeds when using emoji, horizontal rules, or images.
Fixes issues #880, #723, #1113: Markdown must be rendered to XHTML, not HTML, when generating summary content for Atom feeds. Otherwise, content-less tags like <img> and <hr>, generated when issue descriptions, merge request descriptions, comments, or commit messages use emoji, horizontal rules, or images, are not terminated and make the Atom XML invalid.
2015-02-21 16:12:13 -05:00
|
|
|
if options[:xhtml]
|
2015-03-25 05:03:55 -04:00
|
|
|
save_options |= Nokogiri::XML::Node::SaveOptions::AS_XHTML
|
Fix invalid Atom feeds when using emoji, horizontal rules, or images.
Fixes issues #880, #723, #1113: Markdown must be rendered to XHTML, not HTML, when generating summary content for Atom feeds. Otherwise, content-less tags like <img> and <hr>, generated when issue descriptions, merge request descriptions, comments, or commit messages use emoji, horizontal rules, or images, are not terminated and make the Atom XML invalid.
2015-02-21 16:12:13 -05:00
|
|
|
end
|
2014-09-01 11:47:28 -04:00
|
|
|
|
2015-03-25 05:03:55 -04:00
|
|
|
text = result[:output].to_html(save_with: save_options)
|
2014-05-26 08:40:10 -04:00
|
|
|
|
2015-03-22 11:02:32 -04:00
|
|
|
text.html_safe
|
2012-08-14 04:32:19 -04:00
|
|
|
end
|
|
|
|
|
2012-09-05 16:07:39 -04:00
|
|
|
private
|
|
|
|
|
2015-08-27 16:09:01 -04:00
|
|
|
def self.renderer
|
|
|
|
@markdown ||= begin
|
|
|
|
renderer = Redcarpet::Render::HTML.new
|
2015-08-31 16:22:34 -04:00
|
|
|
Redcarpet::Markdown.new(renderer, redcarpet_options)
|
2015-08-27 16:09:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-31 16:22:34 -04:00
|
|
|
def self.redcarpet_options
|
|
|
|
# https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use
|
|
|
|
@redcarpet_options ||= {
|
|
|
|
fenced_code_blocks: true,
|
|
|
|
footnotes: true,
|
|
|
|
lax_spacing: true,
|
|
|
|
no_intra_emphasis: true,
|
|
|
|
space_after_headers: true,
|
|
|
|
strikethrough: true,
|
|
|
|
superscript: true,
|
|
|
|
tables: true
|
|
|
|
}.freeze
|
|
|
|
end
|
|
|
|
|
2015-04-16 17:39:09 -04:00
|
|
|
# Filters used in our pipeline
|
2012-09-05 16:07:39 -04:00
|
|
|
#
|
2015-04-07 16:53:15 -04:00
|
|
|
# SanitizationFilter should come first so that all generated reference HTML
|
|
|
|
# goes through untouched.
|
|
|
|
#
|
2015-04-27 18:56:37 -04:00
|
|
|
# See https://github.com/jch/html-pipeline#filters for more filters.
|
2015-08-27 16:09:01 -04:00
|
|
|
def self.filters
|
2015-04-02 20:46:43 -04:00
|
|
|
[
|
2015-08-27 16:09:01 -04:00
|
|
|
Gitlab::Markdown::SyntaxHighlightFilter,
|
2015-04-27 18:56:37 -04:00
|
|
|
Gitlab::Markdown::SanitizationFilter,
|
2015-04-07 16:53:15 -04:00
|
|
|
|
2015-05-10 19:34:32 -04:00
|
|
|
Gitlab::Markdown::RelativeLinkFilter,
|
2015-04-16 17:39:09 -04:00
|
|
|
Gitlab::Markdown::EmojiFilter,
|
2015-04-22 16:40:25 -04:00
|
|
|
Gitlab::Markdown::TableOfContentsFilter,
|
2015-04-27 18:54:13 -04:00
|
|
|
Gitlab::Markdown::AutolinkFilter,
|
2015-05-27 15:39:08 -04:00
|
|
|
Gitlab::Markdown::ExternalLinkFilter,
|
2015-04-16 17:39:09 -04:00
|
|
|
|
2015-04-02 20:46:43 -04:00
|
|
|
Gitlab::Markdown::UserReferenceFilter,
|
|
|
|
Gitlab::Markdown::IssueReferenceFilter,
|
|
|
|
Gitlab::Markdown::ExternalIssueReferenceFilter,
|
|
|
|
Gitlab::Markdown::MergeRequestReferenceFilter,
|
|
|
|
Gitlab::Markdown::SnippetReferenceFilter,
|
|
|
|
Gitlab::Markdown::CommitRangeReferenceFilter,
|
|
|
|
Gitlab::Markdown::CommitReferenceFilter,
|
2015-04-29 15:51:32 -04:00
|
|
|
Gitlab::Markdown::LabelReferenceFilter,
|
|
|
|
|
2015-05-18 15:44:45 -04:00
|
|
|
Gitlab::Markdown::TaskListFilter
|
2015-04-02 20:46:43 -04:00
|
|
|
]
|
2014-10-01 11:31:13 -04:00
|
|
|
end
|
2012-08-14 04:32:19 -04:00
|
|
|
end
|
|
|
|
end
|