3 namespace BookStack\Entities\Tools;
5 use BookStack\Actions\Tag;
6 use BookStack\Entities\Models\Entity;
7 use Illuminate\Support\HtmlString;
9 class SearchResultsFormatter
13 * For the given array of entities, Prepare the models to be shown in search result
14 * output. This sets a series of additional attributes.
15 * @param Entity[] $results
17 public function format(array $results, SearchOptions $options): void
19 foreach ($results as $result) {
20 $this->setSearchPreview($result, $options);
25 * Update the given entity model to set attributes used for previews of the item
26 * primarily within search result lists.
28 protected function setSearchPreview(Entity $entity, SearchOptions $options)
30 $textProperty = $entity->textField;
31 $textContent = $entity->$textProperty;
32 $terms = array_merge($options->exacts, $options->searches);
34 $originalContentByNewAttribute = [
35 'preview_name' => $entity->name,
36 'preview_content' => $textContent,
39 foreach ($originalContentByNewAttribute as $attributeName => $content) {
40 $matchRefs = $this->getMatchPositions($content, $terms);
41 $mergedRefs = $this->sortAndMergeMatchPositions($matchRefs);
42 $formatted = $this->formatTextUsingMatchPositions($mergedRefs, $content);
43 $entity->setAttribute($attributeName, new HtmlString($formatted));
46 $tags = $entity->relationLoaded('tags') ? $entity->tags->all() : [];
47 $this->highlightTagsContainingTerms($tags, $terms);
51 * Highlight tags which match the given terms.
53 * @param string[] $terms
55 protected function highlightTagsContainingTerms(array $tags, array $terms): void
57 foreach ($tags as $tag) {
58 $tagName = strtolower($tag->name);
59 $tagValue = strtolower($tag->value);
61 foreach ($terms as $term) {
62 $termLower = strtolower($term);
64 if (strpos($tagName, $termLower) !== false) {
65 $tag->setAttribute('highlight_name', true);
68 if (strpos($tagValue, $termLower) !== false) {
69 $tag->setAttribute('highlight_value', true);
76 * Get positions of the given terms within the given text.
77 * Is in the array format of [int $startIndex => int $endIndex] where the indexes
78 * are positions within the provided text.
80 * @return array<int, int>
82 protected function getMatchPositions(string $text, array $terms): array
85 $text = strtolower($text);
87 foreach ($terms as $term) {
89 $term = strtolower($term);
90 $pos = strpos($text, $term, $offset);
91 while ($pos !== false) {
92 $end = $pos + strlen($term);
93 $matchRefs[$pos] = $end;
95 $pos = strpos($text, $term, $offset);
103 * Sort the given match positions before merging them where they're
104 * adjacent or where they overlap.
106 * @param array<int, int> $matchPositions
107 * @return array<int, int>
109 protected function sortAndMergeMatchPositions(array $matchPositions): array
111 ksort($matchPositions);
116 foreach ($matchPositions as $start => $end) {
117 if ($start > $lastEnd) {
118 $mergedRefs[$start] = $end;
121 } else if ($end > $lastEnd) {
122 $mergedRefs[$lastStart] = $end;
131 * Format the given original text, returning a version where terms are highlighted within.
132 * Returned content is in HTML text format.
134 protected function formatTextUsingMatchPositions(array $matchPositions, string $originalText): string
138 $maxEnd = strlen($originalText);
143 foreach ($matchPositions as $start => $end) {
144 // Get our outer text ranges for the added context we want to show upon the result.
145 $contextStart = max($start - $contextRange, 0, $lastEnd);
146 $contextEnd = min($end + $contextRange, $maxEnd);
148 // Adjust the start if we're going to be touching the previous match.
149 $startDiff = $start - $lastEnd;
150 if ($startDiff < 0) {
151 $contextStart = $start;
152 $content = substr($content, 0, strlen($content) + $startDiff);
155 // Add ellipsis between results
156 if ($contextStart !== 0 && $contextStart !== $start) {
160 // Add our content including the bolded matching text
161 $content .= e(substr($originalText, $contextStart, $start - $contextStart));
162 $content .= '<strong>' . e(substr($originalText, $start, $end - $start)) . '</strong>';
163 $content .= e(substr($originalText, $end, $contextEnd - $end));
165 // Update our last end position
166 $lastEnd = $contextEnd;
168 // Update the first start position if it's not already been set
169 if (is_null($firstStart)) {
170 $firstStart = $contextStart;
173 // Stop if we're near our target
174 if (strlen($content) >= $targetLength - 10) {
179 // Just copy out the content if we haven't moved along anywhere.
180 if ($lastEnd === 0) {
181 $content = e(substr($originalText, 0, $targetLength));
182 $lastEnd = $targetLength;
185 // Pad out the end if we're low
186 $remainder = $targetLength - strlen($content);
187 if ($remainder > 10) {
188 $content .= e(substr($originalText, $lastEnd, $remainder));
189 $lastEnd += $remainder;
192 // Pad out the start if we're still low
193 $remainder = $targetLength - strlen($content);
194 $firstStart = $firstStart ?: 0;
195 if ($remainder > 10 && $firstStart !== 0) {
196 $padStart = max(0, $firstStart - $remainder);
197 $content = ($padStart === 0 ? '' : '...') . e(substr($originalText, $padStart, $firstStart - $padStart)) . substr($content, 4);
200 // Add ellipsis if we're not at the end
201 if ($lastEnd < $maxEnd) {