2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
module Banzai
|
|
|
|
module Renderer
|
|
|
|
# Convert a Markdown String into an HTML-safe String of HTML
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2016-08-24 12:49:21 +05:30
|
|
|
# text - Markdown String
|
2015-12-23 02:04:40 +05:30
|
|
|
# context - Hash of context options passed to our HTML Pipeline
|
|
|
|
#
|
|
|
|
# Returns an HTML-safe String
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.render(text, context = {})
|
2015-12-23 02:04:40 +05:30
|
|
|
cache_key = context.delete(:cache_key)
|
|
|
|
cache_key = full_cache_key(cache_key, context[:pipeline])
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
if cache_key
|
2016-06-02 11:05:42 +05:30
|
|
|
Gitlab::Metrics.measure(:banzai_cached_render) do
|
|
|
|
Rails.cache.fetch(cache_key) do
|
|
|
|
cacheless_render(text, context)
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
else
|
|
|
|
cacheless_render(text, context)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
# Convert a Markdown-containing field on an object into an HTML-safe String
|
|
|
|
# of HTML. This method is analogous to calling render(object.field), but it
|
|
|
|
# can cache the rendered HTML in the object, rather than Redis.
|
2018-03-17 18:26:18 +05:30
|
|
|
def self.render_field(object, field, context = {})
|
|
|
|
unless object.respond_to?(:cached_markdown_fields)
|
|
|
|
return cacheless_render_field(object, field, context)
|
|
|
|
end
|
|
|
|
|
|
|
|
object.refresh_markdown_cache! unless object.cached_html_up_to_date?(field)
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
object.cached_html_for(field)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Same as +render_field+, but without consulting or updating the cache field
|
2018-03-17 18:26:18 +05:30
|
|
|
def self.cacheless_render_field(object, field, context = {})
|
|
|
|
text = object.__send__(field) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
context = context.reverse_merge(object.banzai_render_context(field)) if object.respond_to?(:banzai_render_context)
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
cacheless_render(text, context)
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
# Perform multiple render from an Array of Markdown String into an
|
|
|
|
# Array of HTML-safe String of HTML.
|
|
|
|
#
|
2019-09-30 21:07:59 +05:30
|
|
|
# The redis cache is completely obviated if we receive a `:rendered` key in the
|
|
|
|
# context, as it is assumed the item has been pre-rendered somewhere else and there
|
|
|
|
# is no need to cache it.
|
|
|
|
#
|
|
|
|
# If no `:rendered` key is present in the context, as the rendered Markdown String
|
|
|
|
# can be already cached, read all the data from the cache using
|
|
|
|
# Rails.cache.read_multi operation. If the Markdown String is not in the cache
|
|
|
|
# or it's not cacheable (no cache_key entry is provided in the context) the
|
|
|
|
# Markdown String is rendered and stored in the cache so the next render call
|
|
|
|
# gets the rendered HTML-safe String from the cache.
|
2016-08-24 12:49:21 +05:30
|
|
|
#
|
|
|
|
# For further explanation see #render method comments.
|
|
|
|
#
|
|
|
|
# texts_and_contexts - An Array of Hashes that contains the Markdown String (:text)
|
|
|
|
# an options passed to our HTML Pipeline (:context)
|
|
|
|
#
|
|
|
|
# If on the :context you specify a :cache_key entry will be used to retrieve it
|
|
|
|
# and cache the result of rendering the Markdown String.
|
|
|
|
#
|
|
|
|
# Returns an Array containing HTML-safe String instances.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# texts_and_contexts
|
|
|
|
# => [{ text: '### Hello',
|
|
|
|
# context: { cache_key: [note, :note] } }]
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.cache_collection_render(texts_and_contexts)
|
2019-09-30 21:07:59 +05:30
|
|
|
items_collection = texts_and_contexts.each do |item|
|
2016-08-24 12:49:21 +05:30
|
|
|
context = item[:context]
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
if context.key?(:rendered)
|
|
|
|
item[:rendered] = context.delete(:rendered)
|
|
|
|
else
|
|
|
|
# If the attribute didn't come in pre-rendered, let's prepare it for caching it in redis
|
|
|
|
cache_key = full_cache_multi_key(context.delete(:cache_key), context[:pipeline])
|
|
|
|
item[:cache_key] = cache_key if cache_key
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
cacheable_items, non_cacheable_items = items_collection.group_by do |item|
|
|
|
|
if item.key?(:rendered)
|
|
|
|
# We're not really doing anything here as these don't need any processing, but leaving it just in case
|
|
|
|
# as they could have a cache_key and we don't want them to be re-rendered
|
|
|
|
:rendered
|
|
|
|
elsif item.key?(:cache_key)
|
|
|
|
:cacheable
|
|
|
|
else
|
|
|
|
:non_cacheable
|
|
|
|
end
|
|
|
|
end.values_at(:cacheable, :non_cacheable)
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
items_in_cache = []
|
|
|
|
items_not_in_cache = []
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
if cacheable_items.present?
|
2016-08-24 12:49:21 +05:30
|
|
|
items_in_cache = Rails.cache.read_multi(*cacheable_items.map { |item| item[:cache_key] })
|
|
|
|
items_not_in_cache = cacheable_items.reject do |item|
|
|
|
|
item[:rendered] = items_in_cache[item[:cache_key]]
|
|
|
|
items_in_cache.key?(item[:cache_key])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
(items_not_in_cache + Array.wrap(non_cacheable_items)).each do |item|
|
2016-08-24 12:49:21 +05:30
|
|
|
item[:rendered] = render(item[:text], item[:context])
|
|
|
|
Rails.cache.write(item[:cache_key], item[:rendered]) if item[:cache_key]
|
|
|
|
end
|
|
|
|
|
|
|
|
items_collection.map { |item| item[:rendered] }
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.render_result(text, context = {})
|
2016-06-22 15:30:34 +05:30
|
|
|
text = Pipeline[:pre_process].to_html(text, context) if text
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
Pipeline[context[:pipeline]].call(text, context)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
# 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
|
2019-09-30 21:07:59 +05:30
|
|
|
# permission to make (`ReferenceRedactorFilter`).
|
2015-12-23 02:04:40 +05:30
|
|
|
#
|
|
|
|
# html - String to process
|
|
|
|
# context - Hash of options to customize output
|
2020-05-24 23:13:21 +05:30
|
|
|
# :pipeline - Symbol pipeline type - for context transform only, defaults to :full
|
2015-12-23 02:04:40 +05:30
|
|
|
# :project - Project
|
|
|
|
# :user - User object
|
2020-05-24 23:13:21 +05:30
|
|
|
# :post_process_pipeline - pipeline to use for post_processing - defaults to PostProcessPipeline
|
2015-12-23 02:04:40 +05:30
|
|
|
#
|
|
|
|
# Returns an HTML-safe String
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.post_process(html, context)
|
2015-12-23 02:04:40 +05:30
|
|
|
context = Pipeline[context[:pipeline]].transform_context(context)
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
# Use a passed class for the pipeline or default to PostProcessPipeline
|
|
|
|
pipeline = context.delete(:post_process_pipeline) || ::Banzai::Pipeline::PostProcessPipeline
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
if context[:xhtml]
|
|
|
|
pipeline.to_document(html, context).to_html(save_with: Nokogiri::XML::Node::SaveOptions::AS_XHTML)
|
|
|
|
else
|
|
|
|
pipeline.to_html(html, context)
|
|
|
|
end.html_safe
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.cacheless_render(text, context = {})
|
2017-09-10 17:25:29 +05:30
|
|
|
return text.to_s unless text.present?
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
real_start = Gitlab::Metrics::System.monotonic_time
|
|
|
|
cpu_start = Gitlab::Metrics::System.cpu_time
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
result = render_result(text, context)
|
|
|
|
|
|
|
|
output = result[:output]
|
|
|
|
rendered = if output.respond_to?(:to_html)
|
|
|
|
output.to_html
|
|
|
|
else
|
|
|
|
output.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
cpu_duration_histogram.observe({}, Gitlab::Metrics::System.cpu_time - cpu_start)
|
|
|
|
real_duration_histogram.observe({}, Gitlab::Metrics::System.monotonic_time - real_start)
|
|
|
|
|
|
|
|
rendered
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.real_duration_histogram
|
|
|
|
Gitlab::Metrics.histogram(
|
|
|
|
:gitlab_banzai_cacheless_render_real_duration_seconds,
|
|
|
|
'Duration of Banzai pipeline rendering in real time',
|
|
|
|
{},
|
|
|
|
[0.01, 0.01, 0.05, 0.1, 0.5, 1, 2, 5, 10.0, 50, 100]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cpu_duration_histogram
|
|
|
|
Gitlab::Metrics.histogram(
|
|
|
|
:gitlab_banzai_cacheless_render_cpu_duration_seconds,
|
|
|
|
'Duration of Banzai pipeline rendering in cpu time',
|
|
|
|
{},
|
|
|
|
Gitlab::Metrics::EXECUTION_MEASUREMENT_BUCKETS
|
|
|
|
)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.full_cache_key(cache_key, pipeline_name)
|
2015-12-23 02:04:40 +05:30
|
|
|
return unless cache_key
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
["banzai", *cache_key, pipeline_name || :full]
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
# To map Rails.cache.read_multi results we need to know the Rails.cache.expanded_key.
|
|
|
|
# Other option will be to generate stringified keys on our side and don't delegate to Rails.cache.expanded_key
|
|
|
|
# method.
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.full_cache_multi_key(cache_key, pipeline_name)
|
2016-08-24 12:49:21 +05:30
|
|
|
return unless cache_key
|
2016-11-24 13:41:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
Rails.cache.__send__(:expanded_key, full_cache_key(cache_key, pipeline_name)) # rubocop:disable GitlabSecurity/PublicSend
|
2016-11-24 13:41:30 +05:30
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|