Commit d54f3e64 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '39509-todos-should-not-depend-on-gitaly-calls' into 'master'

Prevent unnecessary Gitaly calls when rendering comment excerpts

See merge request gitlab-org/gitlab!23100
parents ff77bdf6 2a755762
......@@ -76,13 +76,14 @@ module MarkupHelper
# +max_chars+ limit. If the length limit falls within a tag's contents, then
# the tag contents are truncated without removing the closing tag.
def first_line_in_markdown(object, attribute, max_chars = nil, options = {})
md = markdown_field(object, attribute, options)
md = markdown_field(object, attribute, options.merge(post_process: false))
return unless md.present?
tags = %w(a gl-emoji b pre code p span)
tags << 'img' if options[:allow_images]
text = truncate_visible(md, max_chars || md.length)
text = prepare_for_rendering(text, markdown_field_render_context(object, attribute, options))
text = sanitize(
text,
tags: tags,
......@@ -107,15 +108,12 @@ module MarkupHelper
def markdown_field(object, field, context = {})
object = object.for_display if object.respond_to?(:for_display)
redacted_field_html = object.try(:"redacted_#{field}_html")
return '' unless object.present?
return redacted_field_html if redacted_field_html
html = Banzai.render_field(object, field, context)
context.reverse_merge!(object.banzai_render_context(field)) if object.respond_to?(:banzai_render_context)
redacted_field_html = object.try(:"redacted_#{field}_html")
return redacted_field_html if redacted_field_html
prepare_for_rendering(html, context)
render_markdown_field(object, field, context)
end
def markup(file_name, text, context = {})
......@@ -277,6 +275,23 @@ module MarkupHelper
Gitlab::OtherMarkup.render(file_name, text, context)
end
def render_markdown_field(object, field, context = {})
post_process = context.delete(:post_process)
post_process = true if post_process.nil?
html = Banzai.render_field(object, field, context)
return html unless post_process
prepare_for_rendering(html, markdown_field_render_context(object, field, context))
end
def markdown_field_render_context(object, field, base_context = {})
return base_context unless object.respond_to?(:banzai_render_context)
base_context.reverse_merge(object.banzai_render_context(field))
end
def prepare_for_rendering(html, context = {})
return '' unless html.present?
......
---
title: Prevent unnecessary Gitaly calls when rendering comment excerpts in todos and
activity feed
merge_request: 23100
author:
type: performance
......@@ -138,10 +138,20 @@ describe MarkupHelper do
describe 'without redacted attribute' do
it 'renders the markdown value' do
expect(Banzai).to receive(:render_field).with(commit, attribute, {}).and_call_original
expect(Banzai).to receive(:post_process)
helper.markdown_field(commit, attribute)
end
end
context 'when post_process is false' do
it 'does not run Markdown post processing' do
expect(Banzai).to receive(:render_field).with(commit, attribute, {}).and_call_original
expect(Banzai).not_to receive(:post_process)
helper.markdown_field(commit, attribute, post_process: false)
end
end
end
describe '#link_to_markdown_field' do
......@@ -565,6 +575,14 @@ describe MarkupHelper do
expect(doc.content).to eq "foo 😉\nbar 😀"
end
it 'does not post-process truncated text', :request_store do
object = create_object("hello \n\n [Test](README.md)")
expect do
first_line_in_markdown(object, attribute, nil, project: project)
end.not_to change { Gitlab::GitalyClient.get_request_count }
end
end
context 'when the asked attribute can be redacted' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment