Skip to content

Commit 7ef80d8

Browse files
authored
Merge pull request #31 from opcodesio/feature/highlight-search-results
Feature / highlight search results
2 parents 525647b + 0d58e23 commit 7ef80d8

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
"autoload": {
3939
"psr-4": {
4040
"Opcodes\\LogViewer\\": "src"
41-
}
41+
},
42+
"files": [
43+
"src/helpers.php"
44+
]
4245
},
4346
"autoload-dev": {
4447
"psr-4": {

public/app.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/css/app.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@
274274
@apply h-4 w-4 ml-1 transition duration-200;
275275
}
276276
}
277+
278+
code, mark {
279+
@apply bg-amber-200 text-gray-900 px-1 py-0.5 rounded
280+
dark:bg-yellow-800 dark:text-white;
281+
}
277282
}
278283
}
279284

resources/views/livewire/log-list.blade.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ class="table-fixed min-w-full max-w-full border-separate"
7272
@else <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"><use href="#icon-info" /></svg>@endif
7373
</td>
7474
<td class="log-level truncate hidden lg:table-cell">{{ $log->level->getName() }}</td>
75-
<td class="whitespace-nowrap text-gray-900 dark:text-gray-200">{{ $log->time->toDateTimeString() }}</td>
76-
<td class="whitespace-nowrap text-gray-500 dark:text-gray-300 dark:opacity-90 hidden lg:table-cell">{{ $log->environment }}</td>
77-
<td class="max-w-[1px] w-full truncate text-gray-500 dark:text-gray-300 dark:opacity-90">{{ $log->text }}</td>
75+
<td class="whitespace-nowrap text-gray-900 dark:text-gray-200">{!! highlight_search_result($log->time->toDateTimeString(), $query) !!}</td>
76+
<td class="whitespace-nowrap text-gray-500 dark:text-gray-300 dark:opacity-90 hidden lg:table-cell">{!! highlight_search_result($log->environment, $query) !!}</td>
77+
<td class="max-w-[1px] w-full truncate text-gray-500 dark:text-gray-300 dark:opacity-90">{!! highlight_search_result($log->text, $query) !!}</td>
7878
<td class="whitespace-nowrap text-gray-500 dark:text-gray-300 dark:opacity-90 text-xs">@include('log-viewer::partials.log-list-link-button')</td>
7979
</tr>
80-
<tr x-show="$store.logViewer.isOpen({{$index}})"><td colspan="6"><pre class="log-stack">{{ $log->fullText }}</pre></td></tr>
80+
<tr x-show="$store.logViewer.isOpen({{$index}})"><td colspan="6"><pre class="log-stack">{!! highlight_search_result($log->fullText, $query) !!}</pre></td></tr>
8181
</tbody>
8282
@empty
8383
<tbody class="log-group">

src/helpers.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
if (! function_exists('highlight_search_result')) {
4+
/**
5+
* Highlights search query results and escapes HTML.
6+
* Safe to use within {!! !!} in Blade.
7+
*
8+
* @param string $text
9+
* @param string|null $query
10+
* @return string
11+
*/
12+
function highlight_search_result(string $text, string $query = null): string
13+
{
14+
if (! empty($query)) {
15+
if (! \Illuminate\Support\Str::endsWith($query, '/i')) {
16+
$query = '/'.$query.'/i';
17+
}
18+
19+
$text = preg_replace_callback(
20+
$query,
21+
function ($matches) {
22+
return '<mark>'.$matches[0].'</mark>';
23+
},
24+
$text
25+
);
26+
}
27+
28+
// Let's return the <mark> tags which we use for highlighting the search results
29+
// while escaping the rest of the HTML entities
30+
return str_replace(
31+
[htmlspecialchars('<mark>'), htmlspecialchars('</mark>')],
32+
['<mark>', '</mark>'],
33+
htmlspecialchars($text)
34+
);
35+
}
36+
}

0 commit comments

Comments
 (0)