2014-09-01 15:47:28 +00:00
|
|
|
require 'html/pipeline'
|
2015-04-29 19:51:32 +00:00
|
|
|
require 'task_list/filter'
|
2014-09-01 15:47:28 +00:00
|
|
|
|
2012-08-14 08:32:19 +00:00
|
|
|
module Gitlab
|
2012-09-06 07:50:47 +00:00
|
|
|
# Custom parser for GitLab-flavored Markdown
|
2012-08-23 18:10:06 +00:00
|
|
|
#
|
2015-04-28 02:01:46 +00:00
|
|
|
# See the files in `lib/gitlab/markdown/` for specific processing information.
|
2012-09-05 20:07:39 +00:00
|
|
|
module Markdown
|
2015-04-23 16:58:55 +00:00
|
|
|
# Provide autoload paths for filters to prevent a circular dependency error
|
2015-04-27 22:54:13 +00:00
|
|
|
autoload :AutolinkFilter, 'gitlab/markdown/autolink_filter'
|
2015-04-23 16:58:55 +00: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'
|
|
|
|
autoload :IssueReferenceFilter, 'gitlab/markdown/issue_reference_filter'
|
|
|
|
autoload :LabelReferenceFilter, 'gitlab/markdown/label_reference_filter'
|
|
|
|
autoload :MergeRequestReferenceFilter, 'gitlab/markdown/merge_request_reference_filter'
|
2015-04-27 22:56:37 +00:00
|
|
|
autoload :SanitizationFilter, 'gitlab/markdown/sanitization_filter'
|
2015-04-23 16:58:55 +00:00
|
|
|
autoload :SnippetReferenceFilter, 'gitlab/markdown/snippet_reference_filter'
|
2015-04-22 20:40:25 +00:00
|
|
|
autoload :TableOfContentsFilter, 'gitlab/markdown/table_of_contents_filter'
|
2015-04-23 16:58:55 +00:00
|
|
|
autoload :UserReferenceFilter, 'gitlab/markdown/user_reference_filter'
|
|
|
|
|
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 21:12:13 +00:00
|
|
|
# Public: Parse the provided text with GitLab-Flavored Markdown
|
|
|
|
#
|
|
|
|
# text - the source text
|
2015-03-25 09:03:55 +00:00
|
|
|
# project - the project
|
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 21:12:13 +00:00
|
|
|
# html_options - extra options for the reference links as given to link_to
|
|
|
|
def gfm(text, project = @project, html_options = {})
|
|
|
|
gfm_with_options(text, {}, project, html_options)
|
2014-10-05 05:53:44 +00:00
|
|
|
end
|
|
|
|
|
2012-09-05 20:07:39 +00:00
|
|
|
# Public: Parse the provided text with GitLab-Flavored Markdown
|
|
|
|
#
|
|
|
|
# text - the source text
|
2015-04-29 21:53:32 +00: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-03-25 09:03:55 +00:00
|
|
|
# project - the project
|
2012-09-05 20:07:39 +00:00
|
|
|
# html_options - extra options for the reference links as given to link_to
|
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 21:12:13 +00:00
|
|
|
def gfm_with_options(text, options = {}, project = @project, html_options = {})
|
2012-09-05 20:07:39 +00:00
|
|
|
return text if text.nil?
|
|
|
|
|
2012-09-20 00:21:35 +00: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 23:58:12 +00:00
|
|
|
|
2015-03-25 09:03:55 +00:00
|
|
|
options.reverse_merge!(
|
|
|
|
xhtml: false,
|
|
|
|
reference_only_path: true
|
|
|
|
)
|
|
|
|
|
2015-04-16 21:39:09 +00:00
|
|
|
pipeline = HTML::Pipeline.new(filters)
|
|
|
|
|
|
|
|
context = {
|
|
|
|
# EmojiFilter
|
2015-04-23 16:58:55 +00:00
|
|
|
asset_root: Gitlab.config.gitlab.url,
|
|
|
|
asset_host: Gitlab::Application.config.asset_host,
|
2015-04-16 21:39:09 +00:00
|
|
|
|
2015-04-22 20:40:25 +00:00
|
|
|
# TableOfContentsFilter
|
|
|
|
no_header_anchors: options[:no_header_anchors],
|
|
|
|
|
2015-04-16 21:39:09 +00:00
|
|
|
# ReferenceFilter
|
2015-04-13 20:23:00 +00:00
|
|
|
current_user: current_user,
|
2015-04-16 21:39:09 +00:00
|
|
|
only_path: options[:reference_only_path],
|
|
|
|
project: project,
|
|
|
|
reference_class: html_options[:class]
|
2014-09-01 15:47:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 21:39:09 +00:00
|
|
|
result = pipeline.call(text, context)
|
2015-04-01 02:03:54 +00:00
|
|
|
|
2015-03-25 09:03:55 +00: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 21:12:13 +00:00
|
|
|
if options[:xhtml]
|
2015-03-25 09:03:55 +00: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 21:12:13 +00:00
|
|
|
end
|
2014-09-01 15:47:28 +00:00
|
|
|
|
2015-03-25 09:03:55 +00:00
|
|
|
text = result[:output].to_html(save_with: save_options)
|
2014-05-26 12:40:10 +00:00
|
|
|
|
2015-03-22 15:02:32 +00:00
|
|
|
text.html_safe
|
2012-08-14 08:32:19 +00:00
|
|
|
end
|
|
|
|
|
2012-09-05 20:07:39 +00:00
|
|
|
private
|
|
|
|
|
2015-04-16 21:39:09 +00:00
|
|
|
# Filters used in our pipeline
|
2012-09-05 20:07:39 +00:00
|
|
|
#
|
2015-04-07 20:53:15 +00:00
|
|
|
# SanitizationFilter should come first so that all generated reference HTML
|
|
|
|
# goes through untouched.
|
|
|
|
#
|
2015-04-27 22:56:37 +00:00
|
|
|
# See https://github.com/jch/html-pipeline#filters for more filters.
|
2015-04-03 00:46:43 +00:00
|
|
|
def filters
|
|
|
|
[
|
2015-04-27 22:56:37 +00:00
|
|
|
Gitlab::Markdown::SanitizationFilter,
|
2015-04-07 20:53:15 +00:00
|
|
|
|
2015-04-16 21:39:09 +00:00
|
|
|
Gitlab::Markdown::EmojiFilter,
|
2015-04-22 20:40:25 +00:00
|
|
|
Gitlab::Markdown::TableOfContentsFilter,
|
2015-04-27 22:54:13 +00:00
|
|
|
Gitlab::Markdown::AutolinkFilter,
|
2015-04-16 21:39:09 +00:00
|
|
|
|
2015-04-03 00:46:43 +00: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 19:51:32 +00:00
|
|
|
Gitlab::Markdown::LabelReferenceFilter,
|
|
|
|
|
|
|
|
TaskList::Filter
|
2015-04-03 00:46:43 +00:00
|
|
|
]
|
2014-10-01 15:31:13 +00:00
|
|
|
end
|
2012-08-14 08:32:19 +00:00
|
|
|
end
|
|
|
|
end
|