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
|
|
|
|
#
|
2015-09-03 17:35:50 -04:00
|
|
|
# Note that while the returned HTML will have been sanitized of dangerous
|
|
|
|
# HTML, it may post a risk of information leakage if it's not also passed
|
|
|
|
# through `post_process`.
|
|
|
|
#
|
|
|
|
# Also note that the returned String is always HTML, not XHTML. Views
|
|
|
|
# requiring XHTML, such as Atom feeds, need to call `post_process` on the
|
|
|
|
# result, providing the appropriate `pipeline` option.
|
|
|
|
#
|
2015-08-27 16:09:01 -04:00
|
|
|
# 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-09-01 18:16:56 -04:00
|
|
|
# Perform post-processing on an HTML String
|
|
|
|
#
|
|
|
|
# This method is used to perform state-dependent changes to a String of
|
|
|
|
# HTML, such as removing references that the current user doesn't have
|
|
|
|
# permission to make (`RedactorFilter`).
|
|
|
|
#
|
|
|
|
# html - String to process
|
2015-09-03 17:35:50 -04:00
|
|
|
# options - Hash of options to customize output
|
2015-10-13 07:36:47 -04:00
|
|
|
# :pipeline - Symbol pipeline type
|
|
|
|
# :project - Project
|
|
|
|
# :user - User object
|
2015-09-01 18:16:56 -04:00
|
|
|
#
|
|
|
|
# Returns an HTML-safe String
|
2015-09-03 17:35:50 -04:00
|
|
|
def self.post_process(html, options)
|
2015-10-13 07:36:47 -04:00
|
|
|
context = {
|
|
|
|
project: options[:project],
|
|
|
|
current_user: options[:user]
|
|
|
|
}
|
|
|
|
doc = post_processor.to_document(html, context)
|
2015-09-03 17:35:50 -04:00
|
|
|
|
|
|
|
if options[:pipeline] == :atom
|
|
|
|
doc.to_html(save_with: Nokogiri::XML::Node::SaveOptions::AS_XHTML)
|
|
|
|
else
|
|
|
|
doc.to_html
|
|
|
|
end.html_safe
|
2015-09-01 18:16:56 -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-08-04 22:25:08 -04:00
|
|
|
autoload :RedactorFilter, 'gitlab/markdown/redactor_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'
|
2015-10-06 10:09:03 -04:00
|
|
|
autoload :UploadLinkFilter, 'gitlab/markdown/upload_link_filter'
|
2015-04-23 12:58:55 -04:00
|
|
|
|
2015-09-03 17:35:50 -04:00
|
|
|
# Public: Parse the provided HTML with GitLab-Flavored Markdown
|
2012-09-05 16:07:39 -04:00
|
|
|
#
|
2015-09-03 17:35:50 -04:00
|
|
|
# html - HTML String
|
|
|
|
# options - A Hash of options used to customize output (default: {})
|
|
|
|
# :no_header_anchors - Disable header anchors in TableOfContentsFilter
|
|
|
|
# :path - Current path String
|
|
|
|
# :pipeline - Symbol pipeline type
|
|
|
|
# :project - Current Project object
|
|
|
|
# :project_wiki - Current ProjectWiki object
|
|
|
|
# :ref - Current ref String
|
|
|
|
#
|
|
|
|
# Returns an HTML-safe String
|
|
|
|
def self.gfm(html, options = {})
|
|
|
|
return '' unless html.present?
|
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-04-23 12:58:55 -04:00
|
|
|
asset_host: Gitlab::Application.config.asset_host,
|
2015-10-07 09:15:35 -04:00
|
|
|
asset_root: Gitlab.config.gitlab.base_url,
|
2015-04-22 16:40:25 -04:00
|
|
|
|
2015-04-16 17:39:09 -04:00
|
|
|
# ReferenceFilter
|
2015-09-03 17:35:50 -04:00
|
|
|
only_path: only_path_pipeline?(options[:pipeline]),
|
|
|
|
project: options[:project],
|
2015-05-10 19:34:32 -04:00
|
|
|
|
|
|
|
# RelativeLinkFilter
|
2015-09-03 17:35:50 -04:00
|
|
|
project_wiki: options[:project_wiki],
|
2015-08-27 16:09:01 -04:00
|
|
|
ref: options[:ref],
|
|
|
|
requested_path: options[:path],
|
2015-03-31 22:03:54 -04:00
|
|
|
|
2015-09-03 17:35:50 -04:00
|
|
|
# TableOfContentsFilter
|
|
|
|
no_header_anchors: options[:no_header_anchors]
|
|
|
|
}
|
2014-05-26 08:40:10 -04:00
|
|
|
|
2015-09-03 17:35:50 -04:00
|
|
|
@pipeline.to_html(html, context).html_safe
|
2012-08-14 04:32:19 -04:00
|
|
|
end
|
|
|
|
|
2012-09-05 16:07:39 -04:00
|
|
|
private
|
|
|
|
|
2015-09-03 17:35:50 -04:00
|
|
|
# Check if a pipeline enables the `only_path` context option
|
|
|
|
#
|
|
|
|
# Returns Boolean
|
|
|
|
def self.only_path_pipeline?(pipeline)
|
|
|
|
case pipeline
|
|
|
|
when :atom, :email
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
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-09-03 17:35:50 -04:00
|
|
|
def self.renderer
|
|
|
|
@markdown ||= begin
|
|
|
|
renderer = Redcarpet::Render::HTML.new
|
|
|
|
Redcarpet::Markdown.new(renderer, redcarpet_options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.post_processor
|
|
|
|
@post_processor ||= HTML::Pipeline.new([Gitlab::Markdown::RedactorFilter])
|
|
|
|
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-10-06 10:09:03 -04:00
|
|
|
Gitlab::Markdown::UploadLinkFilter,
|
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
|