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-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-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'
|
|
|
|
|
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
|
|
|
# Public: Parse the provided text with GitLab-Flavored Markdown
|
|
|
|
#
|
|
|
|
# text - the source text
|
2015-05-14 07:05:33 -04:00
|
|
|
# options - options
|
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
|
|
|
# html_options - extra options for the reference links as given to link_to
|
2015-05-14 07:05:33 -04:00
|
|
|
def gfm(text, options = {}, html_options = {})
|
|
|
|
gfm_with_options(text, options, html_options)
|
2014-10-05 01:53:44 -04:00
|
|
|
end
|
|
|
|
|
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-03-25 05:03:55 -04:00
|
|
|
# project - the project
|
2012-09-05 16:07:39 -04:00
|
|
|
# html_options - extra options for the reference links as given to link_to
|
2015-05-14 07:05:33 -04:00
|
|
|
def gfm_with_options(text, options = {}, html_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,
|
|
|
|
project: @project,
|
|
|
|
current_user: current_user
|
2015-03-25 05:03:55 -04:00
|
|
|
)
|
|
|
|
|
2015-04-16 17:39:09 -04:00
|
|
|
pipeline = HTML::Pipeline.new(filters)
|
|
|
|
|
|
|
|
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_root: Gitlab.config.gitlab.url,
|
|
|
|
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
|
|
|
reference_class: html_options[:class],
|
|
|
|
|
|
|
|
# RelativeLinkFilter
|
2015-05-13 14:08:37 -04:00
|
|
|
ref: @ref,
|
2015-05-10 19:34:32 -04:00
|
|
|
requested_path: @path,
|
2015-05-13 14:08:37 -04:00
|
|
|
project_wiki: @project_wiki
|
2014-09-01 11:47:28 -04:00
|
|
|
}
|
|
|
|
|
2015-04-16 17:39:09 -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-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-04-02 20:46:43 -04:00
|
|
|
def filters
|
|
|
|
[
|
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
|