Commit 26865038 authored by Dmitry Gruzd's avatar Dmitry Gruzd

Optimize markdown rendering in search results

parent c2d68b94
......@@ -250,15 +250,16 @@ module SearchHelper
# Sanitize a HTML field for search display. Most tags are stripped out and the
# maximum length is set to 200 characters.
def search_md_sanitize(object, field)
html = markdown_field(object, field)
html = Truncato.truncate(
html,
def search_md_sanitize(source)
source = Truncato.truncate(
source,
count_tags: false,
count_tail: false,
max_length: 200
)
html = markdown(source)
# Truncato's filtered_tags and filtered_attributes are not quite the same
sanitize(html, tags: %w(a p ol ul li pre code))
end
......
......@@ -8,6 +8,6 @@
.float-right ##{issue.iid}
- if issue.description.present?
.description.term
= search_md_sanitize(issue, :description)
= search_md_sanitize(issue.description)
%span.light
#{issue.project.full_name}
......@@ -9,6 +9,6 @@
.float-right= merge_request.to_reference
- if merge_request.description.present?
.description.term
= search_md_sanitize(merge_request, :description)
= search_md_sanitize(merge_request.description)
%span.light
#{merge_request.project.full_name}
......@@ -5,4 +5,4 @@
- if milestone.description.present?
.description.term
= search_md_sanitize(milestone, :description)
= search_md_sanitize(milestone.description)
......@@ -22,4 +22,4 @@
.note-search-result
.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 @@
require 'spec_helper'
RSpec.describe SearchHelper do
include MarkupHelper
# Override simple_sanitize for our testing purposes
def simple_sanitize(str)
str
......@@ -228,6 +230,20 @@ RSpec.describe SearchHelper do
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
it 'renders a search filter link for the current scope' do
@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