Only highlight search results under the highlighting size limit

We should use this limit everywhere, but especially in project search results,
where we could be highlighting very long single lines. (Typical examples:
minified JavaScript, and JSON data files.)
This commit is contained in:
Sean McGivern 2018-01-15 15:00:58 +00:00
parent 82f4564fb7
commit 91939161aa
3 changed files with 14 additions and 0 deletions

View File

@ -1,6 +1,8 @@
module BlobHelper module BlobHelper
def highlight(blob_name, blob_content, repository: nil, plain: false) def highlight(blob_name, blob_content, repository: nil, plain: false)
plain ||= blob_content.length > Blob::MAXIMUM_TEXT_HIGHLIGHT_SIZE
highlighted = Gitlab::Highlight.highlight(blob_name, blob_content, plain: plain, repository: repository) highlighted = Gitlab::Highlight.highlight(blob_name, blob_content, plain: plain, repository: repository)
raw %(<pre class="code highlight"><code>#{highlighted}</code></pre>) raw %(<pre class="code highlight"><code>#{highlighted}</code></pre>)
end end

View File

@ -0,0 +1,5 @@
---
title: Only highlight search results under the highlighting size limit
merge_request: 16462
author:
type: performance

View File

@ -22,6 +22,13 @@ describe BlobHelper do
expect(result).to eq(%[<pre class="code highlight"><code><span id="LC1" class="line" lang="">:type "assem"))</span></code></pre>]) expect(result).to eq(%[<pre class="code highlight"><code><span id="LC1" class="line" lang="">:type "assem"))</span></code></pre>])
end end
it 'returns plaintext for long blobs' do
stub_const('Blob::MAXIMUM_TEXT_HIGHLIGHT_SIZE', 1)
result = helper.highlight(blob_name, blob_content)
expect(result).to eq(%[<pre class="code highlight"><code><span id="LC1" class="line" lang="">(make-pathname :defaults name</span>\n<span id="LC2" class="line" lang="">:type "assem"))</span></code></pre>])
end
it 'highlights single block' do it 'highlights single block' do
expected = %Q[<pre class="code highlight"><code><span id="LC1" class="line" lang="common_lisp"><span class="p">(</span><span class="nb">make-pathname</span> <span class="ss">:defaults</span> <span class="nv">name</span></span> expected = %Q[<pre class="code highlight"><code><span id="LC1" class="line" lang="common_lisp"><span class="p">(</span><span class="nb">make-pathname</span> <span class="ss">:defaults</span> <span class="nv">name</span></span>
<span id="LC2" class="line" lang="common_lisp"><span class="ss">:type</span> <span class="s">"assem"</span><span class="p">))</span></span></code></pre>] <span id="LC2" class="line" lang="common_lisp"><span class="ss">:type</span> <span class="s">"assem"</span><span class="p">))</span></span></code></pre>]