X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/755dc99c724d63ed7feb9ccc1ff607c2579a9f75..refs/pull/3081/head:/app/Entities/Tools/SearchResultsFormatter.php diff --git a/app/Entities/Tools/SearchResultsFormatter.php b/app/Entities/Tools/SearchResultsFormatter.php index a30c96003..5dbcfb663 100644 --- a/app/Entities/Tools/SearchResultsFormatter.php +++ b/app/Entities/Tools/SearchResultsFormatter.php @@ -141,11 +141,12 @@ class SearchResultsFormatter */ protected function formatTextUsingMatchPositions(array $matchPositions, string $originalText, int $targetLength): string { - $contextRange = 32; $maxEnd = strlen($originalText); - $lastEnd = 0; - $firstStart = null; $fetchAll = ($targetLength === 0); + $contextLength = ($fetchAll ? 0 : 32); + + $firstStart = null; + $lastEnd = 0; $content = ''; $contentTextLength = 0; @@ -155,8 +156,8 @@ class SearchResultsFormatter foreach ($matchPositions as $start => $end) { // Get our outer text ranges for the added context we want to show upon the result. - $contextStart = max($start - $contextRange, 0, $lastEnd); - $contextEnd = min($end + $contextRange, $maxEnd); + $contextStart = max($start - $contextLength, 0, $lastEnd); + $contextEnd = min($end + $contextLength, $maxEnd); // Adjust the start if we're going to be touching the previous match. $startDiff = $start - $lastEnd; @@ -172,7 +173,7 @@ class SearchResultsFormatter if (!$fetchAll && $contextStart !== 0 && $contextStart !== $start) { $content .= ' ...'; $contentTextLength += 4; - } else if ($fetchAll) { + } elseif ($fetchAll) { // Or fill in gap since the previous match $fillLength = $contextStart - $lastEnd; $content .= e(substr($originalText, $lastEnd, $fillLength));