Commit 26865038 authored by Dmitry Gruzd's avatar Dmitry Gruzd

Optimize markdown rendering in search results

parent c2d68b94
...@@ -250,15 +250,16 @@ module SearchHelper ...@@ -250,15 +250,16 @@ module SearchHelper
# Sanitize a HTML field for search display. Most tags are stripped out and the # Sanitize a HTML field for search display. Most tags are stripped out and the
# maximum length is set to 200 characters. # maximum length is set to 200 characters.
def search_md_sanitize(object, field) def search_md_sanitize(source)
html = markdown_field(object, field) source = Truncato.truncate(
html = Truncato.truncate( source,
html,
count_tags: false, count_tags: false,
count_tail: false, count_tail: false,
max_length: 200 max_length: 200
) )
html = markdown(source)
# Truncato's filtered_tags and filtered_attributes are not quite the same # Truncato's filtered_tags and filtered_attributes are not quite the same
sanitize(html, tags: %w(a p ol ul li pre code)) sanitize(html, tags: %w(a p ol ul li pre code))
end end
......
...@@ -8,6 +8,6 @@ ...@@ -8,6 +8,6 @@
.float-right ##{issue.iid} .float-right ##{issue.iid}
- if issue.description.present? - if issue.description.present?
.description.term .description.term
= search_md_sanitize(issue, :description) = search_md_sanitize(issue.description)
%span.light %span.light
#{issue.project.full_name} #{issue.project.full_name}
...@@ -9,6 +9,6 @@ ...@@ -9,6 +9,6 @@
.float-right= merge_request.to_reference .float-right= merge_request.to_reference
- if merge_request.description.present? - if merge_request.description.present?
.description.term .description.term
= search_md_sanitize(merge_request, :description) = search_md_sanitize(merge_request.description)
%span.light %span.light
#{merge_request.project.full_name} #{merge_request.project.full_name}
...@@ -5,4 +5,4 @@ ...@@ -5,4 +5,4 @@
- if milestone.description.present? - if milestone.description.present?
.description.term .description.term
= search_md_sanitize(milestone, :description) = search_md_sanitize(milestone.description)
...@@ -22,4 +22,4 @@ ...@@ -22,4 +22,4 @@
.note-search-result .note-search-result
.term .term
= search_md_sanitize(note, :note) = search_md_sanitize(note.note)
---
title: Optimize markdown rendering in search results
merge_request: 39833
author:
type: performance
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe SearchHelper do RSpec.describe SearchHelper do
include MarkupHelper
# Override simple_sanitize for our testing purposes # Override simple_sanitize for our testing purposes
def simple_sanitize(str) def simple_sanitize(str)
str str
...@@ -228,6 +230,20 @@ RSpec.describe SearchHelper do ...@@ -228,6 +230,20 @@ RSpec.describe SearchHelper do
end end
end end
describe 'search_md_sanitize' do
it 'does not do extra sql queries for partial markdown rendering' do
@project = create(:project)
description = FFaker::Lorem.characters(210)
control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { search_md_sanitize(description) }.count
issues = create_list(:issue, 4, project: @project)
description_with_issues = description + ' ' + issues.map { |issue| "##{issue.iid}" }.join(' ')
expect { search_md_sanitize(description_with_issues) }.not_to exceed_all_query_limit(control_count)
end
end
describe 'search_filter_link' do describe 'search_filter_link' do
it 'renders a search filter link for the current scope' do it 'renders a search filter link for the current scope' do
@scope = 'projects' @scope = 'projects'
......
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