]> BookStack Code Mirror - bookstack/commitdiff
Added tag highlighting in search
authorDan Brown <redacted>
Sat, 13 Nov 2021 13:02:32 +0000 (13:02 +0000)
committerDan Brown <redacted>
Sat, 13 Nov 2021 13:02:32 +0000 (13:02 +0000)
Using basic match of name or value containing a general term.

app/Entities/Tools/SearchResultsFormatter.php
resources/sass/_blocks.scss
resources/views/entities/tag.blade.php

index 24dc820a4e7b5865cf80a0bd7495d8a7b87a9a38..1ddee5830c53456ddf7e1c092d8359eb06ad02a5 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace BookStack\Entities\Tools;
 
+use BookStack\Actions\Tag;
 use BookStack\Entities\Models\Entity;
 use Illuminate\Support\HtmlString;
 
@@ -41,6 +42,34 @@ class SearchResultsFormatter
             $formatted = $this->formatTextUsingMatchPositions($mergedRefs, $content);
             $entity->setAttribute($attributeName, new HtmlString($formatted));
         }
+
+        $tags = $entity->relationLoaded('tags') ? $entity->tags->all() : [];
+        $this->highlightTagsContainingTerms($tags, $terms);
+    }
+
+    /**
+     * Highlight tags which match the given terms.
+     * @param Tag[] $tags
+     * @param string[] $terms
+     */
+    protected function highlightTagsContainingTerms(array $tags, array $terms): void
+    {
+        foreach ($tags as $tag) {
+            $tagName = strtolower($tag->name);
+            $tagValue = strtolower($tag->value);
+
+            foreach ($terms as $term) {
+                $termLower = strtolower($term);
+
+                if (strpos($tagName, $termLower) !== false) {
+                    $tag->setAttribute('highlight_name', true);
+                }
+
+                if (strpos($tagValue, $termLower) !== false) {
+                    $tag->setAttribute('highlight_value', true);
+                }
+            }
+        }
     }
 
     /**
index ef03699f1c5ee93c00ff583445d900400f7c928a..ae3e7a441e444556de50b6cb721dcb10f69f902c 100644 (file)
   }
 }
 
+.tag-name.highlight, .tag-value.highlight {
+  font-weight: bold;
+}
+
 .tag-list div:last-child .tag-item {
   margin-bottom: 0;
 }
index 057c709216c28e353fb82985d16bca07dd702f48..de4750c13e715344815d2b0c7488c13d3fd7157e 100644 (file)
@@ -1,9 +1,9 @@
 <div class="tag-item primary-background-light" data-name="{{ $tag->name }}" data-value="{{ $tag->value }}">
     @if($linked ?? true)
-        <div class="tag-name"><a href="{{ $tag->nameUrl() }}">@icon('tag'){{ $tag->name }}</a></div>
-        @if($tag->value) <div class="tag-value"><a href="{{ $tag->valueUrl() }}">{{$tag->value}}</a></div> @endif
+        <div class="tag-name {{ $tag->highlight_name ? 'highlight' : '' }}"><a href="{{ $tag->nameUrl() }}">@icon('tag'){{ $tag->name }}</a></div>
+        @if($tag->value) <div class="tag-value {{ $tag->highlight_value ? 'highlight' : '' }}"><a href="{{ $tag->valueUrl() }}">{{$tag->value}}</a></div> @endif
     @else
-        <div class="tag-name"><span>@icon('tag'){{ $tag->name }}</span></div>
-        @if($tag->value) <div class="tag-value"><span>{{$tag->value}}</span></div> @endif
+        <div class="tag-name {{ $tag->highlight_name ? 'highlight' : '' }}"><span>@icon('tag'){{ $tag->name }}</span></div>
+        @if($tag->value) <div class="tag-value {{ $tag->highlight_value ? 'highlight' : '' }}"><span>{{$tag->value}}</span></div> @endif
     @endif
 </div>
\ No newline at end of file