Skip to content

NOW MANAGED ON CODEBERG

Added tag highlighting in search

Dan Brown339518e2a6ad
diff --git a/app/Entities/Tools/SearchResultsFormatter.php b/app/Entities/Tools/SearchResultsFormatter.php
index 24dc820a4..1ddee5830 100644
--- a/app/Entities/Tools/SearchResultsFormatter.php
+++ b/app/Entities/Tools/SearchResultsFormatter.php
@@ -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);
+ }
+ }
+ }
}
/**
diff --git a/resources/sass/_blocks.scss b/resources/sass/_blocks.scss
index ef03699f1..ae3e7a441 100644
--- a/resources/sass/_blocks.scss
+++ b/resources/sass/_blocks.scss
@@ -262,6 +262,10 @@
}
}
+.tag-name.highlight, .tag-value.highlight {
+ font-weight: bold;
+}
+
.tag-list div:last-child .tag-item {
margin-bottom: 0;
}
diff --git a/resources/views/entities/tag.blade.php b/resources/views/entities/tag.blade.php
index 057c70921..de4750c13 100644
--- a/resources/views/entities/tag.blade.php
+++ b/resources/views/entities/tag.blade.php
@@ -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