debian-mirror-gitlab/lib/gitlab/markdown.rb

163 lines
5.7 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
require 'html/pipeline'
2014-09-02 18:07:02 +05:30
module Gitlab
# Custom parser for GitLab-flavored Markdown
#
2015-09-11 14:41:01 +05:30
# See the files in `lib/gitlab/markdown/` for specific processing information.
2014-09-02 18:07:02 +05:30
module Markdown
2015-09-25 12:07:36 +05:30
# 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)
renderer.render(markdown)
end
2015-09-11 14:41:01 +05:30
# Provide autoload paths for filters to prevent a circular dependency error
autoload :AutolinkFilter, 'gitlab/markdown/autolink_filter'
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 :ExternalLinkFilter, 'gitlab/markdown/external_link_filter'
autoload :IssueReferenceFilter, 'gitlab/markdown/issue_reference_filter'
autoload :LabelReferenceFilter, 'gitlab/markdown/label_reference_filter'
autoload :MergeRequestReferenceFilter, 'gitlab/markdown/merge_request_reference_filter'
autoload :RelativeLinkFilter, 'gitlab/markdown/relative_link_filter'
autoload :SanitizationFilter, 'gitlab/markdown/sanitization_filter'
autoload :SnippetReferenceFilter, 'gitlab/markdown/snippet_reference_filter'
2015-09-25 12:07:36 +05:30
autoload :SyntaxHighlightFilter, 'gitlab/markdown/syntax_highlight_filter'
2015-09-11 14:41:01 +05:30
autoload :TableOfContentsFilter, 'gitlab/markdown/table_of_contents_filter'
autoload :TaskListFilter, 'gitlab/markdown/task_list_filter'
autoload :UserReferenceFilter, 'gitlab/markdown/user_reference_filter'
2015-04-26 12:48:37 +05:30
# Public: Parse the provided text with GitLab-Flavored Markdown
#
# text - the source text
2015-09-11 14:41:01 +05:30
# 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-09-25 12:07:36 +05:30
def self.gfm(text, options = {})
2014-09-02 18:07:02 +05:30
return text if text.nil?
# 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
2015-04-26 12:48:37 +05:30
options.reverse_merge!(
xhtml: false,
2015-09-11 14:41:01 +05:30
reference_only_path: true,
2015-09-25 12:07:36 +05:30
project: options[:project],
current_user: options[:current_user]
2015-04-26 12:48:37 +05:30
)
2015-09-11 14:41:01 +05:30
@pipeline ||= HTML::Pipeline.new(filters)
2014-09-02 18:07:02 +05:30
2015-09-11 14:41:01 +05:30
context = {
# SanitizationFilter
pipeline: options[:pipeline],
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
# EmojiFilter
2015-09-25 12:07:36 +05:30
asset_root: Gitlab.config.gitlab.base_url,
2015-09-11 14:41:01 +05:30
asset_host: Gitlab::Application.config.asset_host,
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
# TableOfContentsFilter
no_header_anchors: options[:no_header_anchors],
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
# ReferenceFilter
current_user: options[:current_user],
only_path: options[:reference_only_path],
project: options[:project],
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
# RelativeLinkFilter
2015-09-25 12:07:36 +05:30
ref: options[:ref],
requested_path: options[:path],
project_wiki: options[:project_wiki]
2015-04-26 12:48:37 +05:30
}
2015-09-11 14:41:01 +05:30
result = @pipeline.call(text, context)
2015-04-26 12:48:37 +05:30
save_options = 0
if options[:xhtml]
save_options |= Nokogiri::XML::Node::SaveOptions::AS_XHTML
end
text = result[:output].to_html(save_with: save_options)
text.html_safe
2014-09-02 18:07:02 +05:30
end
private
2015-09-25 12:07:36 +05:30
def self.renderer
@markdown ||= begin
renderer = Redcarpet::Render::HTML.new
Redcarpet::Markdown.new(renderer, redcarpet_options)
end
end
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-11 14:41:01 +05:30
# Filters used in our pipeline
2014-09-02 18:07:02 +05:30
#
2015-09-11 14:41:01 +05:30
# SanitizationFilter should come first so that all generated reference HTML
# goes through untouched.
2014-09-02 18:07:02 +05:30
#
2015-09-11 14:41:01 +05:30
# See https://github.com/jch/html-pipeline#filters for more filters.
2015-09-25 12:07:36 +05:30
def self.filters
2015-09-11 14:41:01 +05:30
[
2015-09-25 12:07:36 +05:30
Gitlab::Markdown::SyntaxHighlightFilter,
2015-09-11 14:41:01 +05:30
Gitlab::Markdown::SanitizationFilter,
Gitlab::Markdown::RelativeLinkFilter,
Gitlab::Markdown::EmojiFilter,
Gitlab::Markdown::TableOfContentsFilter,
Gitlab::Markdown::AutolinkFilter,
Gitlab::Markdown::ExternalLinkFilter,
Gitlab::Markdown::UserReferenceFilter,
Gitlab::Markdown::IssueReferenceFilter,
Gitlab::Markdown::ExternalIssueReferenceFilter,
Gitlab::Markdown::MergeRequestReferenceFilter,
Gitlab::Markdown::SnippetReferenceFilter,
Gitlab::Markdown::CommitRangeReferenceFilter,
Gitlab::Markdown::CommitReferenceFilter,
Gitlab::Markdown::LabelReferenceFilter,
Gitlab::Markdown::TaskListFilter
]
2014-09-02 18:07:02 +05:30
end
end
end