diff --git a/CHANGELOG.md b/CHANGELOG.md index 0136c2d..d1f555f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to `sky` will be documented in this file +## 2.1.5 - 2022-07-05 + +### What's Changed + +- introduce full-text search by @thyseus in https://github.com/lara-zeus/sky/pull/23 + +**Full Changelog**: https://github.com/lara-zeus/sky/compare/2.1.4...2.1.5 + ## 2.1.3 - 2022-06-21 update core version diff --git a/config/zeus-sky.php b/config/zeus-sky.php index c7e3df9..4d1dbe4 100644 --- a/config/zeus-sky.php +++ b/config/zeus-sky.php @@ -68,6 +68,11 @@ */ 'theme' => 'zeus', + /** + * css class to apply on found search result, e.g. `bg-yellow-400`. + */ + 'search_result_highlight_css_class' => 'highlight', + /** * available locales, this currently used only in tags manager. */ diff --git a/database/migrations/2022_07_04_104832_add_indices_to_posts.php b/database/migrations/2022_07_04_104832_add_indices_to_posts.php new file mode 100644 index 0000000..b2ac0ab --- /dev/null +++ b/database/migrations/2022_07_04_104832_add_indices_to_posts.php @@ -0,0 +1,18 @@ +index('title'); + $table->index('slug'); + $table->index('description'); + $table->fullText('content'); + }); + } +}; diff --git a/docs/configuration.md b/docs/configuration.md index 0acee82..fba459d 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -37,6 +37,16 @@ return [ * set the prefix for pages URL. */ 'page_uri_prefix' => 'page', + + /** + * enable or disable individual Resources. + */ + 'enabled_resources' => [ + LaraZeus\Sky\Filament\Resources\PostResource::class, + LaraZeus\Sky\Filament\Resources\PageResource::class, + LaraZeus\Sky\Filament\Resources\TagResource::class, + LaraZeus\Sky\Filament\Resources\FaqResource::class, + ], /** * this will be setup the default seo site title. read more about it in 'laravel-seo' diff --git a/resources/lang/ar.json b/resources/lang/ar.json index 503af1a..c3c56a6 100644 --- a/resources/lang/ar.json +++ b/resources/lang/ar.json @@ -43,7 +43,7 @@ "Post Slug": "الرابط المختصر", "Write an excerpt for your post": "اكتب ملخص للمقالة", "Show All posts in": "هرض كافة المثالات في", - "No posts yet": "لاتوجد تدوينات حاليا", + "No posts found": "لاتوجد تدوينات حاليا", "FAQ": "الأسئلة الشائعة", "frequently asked questions": "الأسئلة الشائعة", "FAQs": "الأسئلة الشائعة", diff --git a/resources/lang/en.json b/resources/lang/en.json index 19bc312..6ddef43 100644 --- a/resources/lang/en.json +++ b/resources/lang/en.json @@ -43,7 +43,7 @@ "Post Slug": "Post Slug", "Write an excerpt for your post": "Write an excerpt for your post", "Show All posts in": "Show All posts in", - "No posts yet": "No posts yet", + "No posts found": "No posts found", "FAQ": "FAQ", "frequently asked questions": "frequently asked questions", "FAQs": "FAQs", diff --git a/resources/views/themes/zeus/home.blade.php b/resources/views/themes/zeus/home.blade.php index fb5ce36..f703c24 100644 --- a/resources/views/themes/zeus/home.blade.php +++ b/resources/views/themes/zeus/home.blade.php @@ -1,4 +1,8 @@
+ {{-- You can override the used class in config/zeus-sky.php 'search_result_highlight_css_class'' --}} + @unless($stickies->isEmpty())
@foreach($stickies as $post) @@ -10,23 +14,19 @@
- @unless ($posts->isEmpty() && $stickies->isEmpty()) -
- @unless ($posts->isEmpty()) -
-

Posts

-
- @each($theme.'.partial.post', $posts, 'post') - @else - @include($theme.'.partial.empty') - @endunless +
+ @unless ($posts->isEmpty()) +
+

Posts

-
- @include($theme.'.partial.sidebar') -
- @else - @include($theme.'.partial.empty') - @endunless + @each($theme.'.partial.post', $posts, 'post') + @else + @include($theme.'.partial.empty') + @endunless +
+
+ @include($theme.'.partial.sidebar') +
diff --git a/resources/views/themes/zeus/partial/empty.blade.php b/resources/views/themes/zeus/partial/empty.blade.php index aa2b820..9e96e24 100644 --- a/resources/views/themes/zeus/partial/empty.blade.php +++ b/resources/views/themes/zeus/partial/empty.blade.php @@ -1,4 +1,4 @@
- {{ __('No posts yet') }}! + {{ __('No posts found') }}!
diff --git a/resources/views/themes/zeus/partial/sidebar.blade.php b/resources/views/themes/zeus/partial/sidebar.blade.php index acbf366..04f2f19 100644 --- a/resources/views/themes/zeus/partial/sidebar.blade.php +++ b/resources/views/themes/zeus/partial/sidebar.blade.php @@ -1,4 +1,5 @@ {{--@include($theme.'.partial.authors')--}} +@include($theme.'.partial.sidebar.search') @include($theme.'.partial.sidebar.categories') @include($theme.'.partial.sidebar.recent') @include($theme.'.partial.sidebar.pages') diff --git a/resources/views/themes/zeus/partial/sidebar/search.blade.php b/resources/views/themes/zeus/partial/sidebar/search.blade.php new file mode 100644 index 0000000..13d398f --- /dev/null +++ b/resources/views/themes/zeus/partial/sidebar/search.blade.php @@ -0,0 +1,8 @@ +
+

Search

+
+
+ +
+
+
diff --git a/src/Http/Livewire/Posts.php b/src/Http/Livewire/Posts.php index 1e7e578..2fc087d 100644 --- a/src/Http/Livewire/Posts.php +++ b/src/Http/Livewire/Posts.php @@ -2,6 +2,9 @@ namespace LaraZeus\Sky\Http\Livewire; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; +use Illuminate\Support\Facades\DB; use LaraZeus\Sky\Models\Post; use LaraZeus\Sky\Models\Tag; use Livewire\Component; @@ -10,28 +13,84 @@ class Posts extends Component { public function render() { - return view(app('theme').'.home') - ->with([ - 'posts' => Post::NotSticky() - ->orderBy('published_at', 'desc') - ->get(), + $search = strtolower(request()->get('search')); - 'pages' => Post::page() - ->orderBy('published_at', 'desc') - ->whereNull('parent_id') - ->get(), + $posts = Post::NotSticky(); + $posts = $this->applySearch($posts, $search); + $posts = $posts + ->orderBy('published_at', 'desc') + ->get(); - 'tags' => Tag::withCount('postsPublished') - ->where('type', 'category') - ->get(), // $this->tag->postsPublished + $pages = Post::page(); + $pages = $this->applySearch($pages, $search); + $pages = $pages + ->orderBy('published_at', 'desc') + ->whereNull('parent_id') + ->get(); - 'stickies' => Post::sticky()->get(), + $pages = $this->highlightSearchResults($pages, $search); + $posts = $this->highlightSearchResults($posts, $search); - 'recent' => Post::posts() - ->take(config('zeus-sky.site_recent_count', 5)) + $recent = Post::posts() + ->limit(config('zeus-sky.site_recent_count', 5)) ->orderBy('published_at', 'desc') - ->get(), + ->get(); + + return view(app('theme').'.home') + ->with([ + 'posts' => $posts, + 'pages' => $pages, + 'recent' => $recent, + 'tags' => Tag::withCount('postsPublished')->where('type', 'category')->get(), + 'stickies' => Post::sticky()->get(), ]) ->layout(config('zeus-sky.layout')); } + + private function applySearch(Builder $query, string $search): Builder + { + if ($search) { + return $query->where(function ($query) use ($search) { + foreach (['title', 'slug', 'content', 'description'] as $attribute) { + $query->orWhere(DB::raw("lower($attribute)"), 'like', "%$search%"); + } + + return $query; + }); + } + + return $query; + } + + private function highlightSearchResults(Collection $posts, ?string $search = null): Collection + { + if (! $search) { + return $posts; + } + + foreach ($posts as $i => $post) { + $posts[$i]->content = static::hl($post->content, [$search]); + } + + return $posts; + } + + /** + * Credits for this code goes to link below. + * + * @link https://stackoverflow.com/questions/8564578/php-search-text-highlight-function + */ + public static function hl(string $inp, array $words): string + { + $class = config('zeus-sky.search_result_highlight_css_class', 'highlight'); + $replace = array_flip(array_flip($words)); + $pattern = []; + + foreach ($replace as $k => $fword) { + $pattern[] = '/\b('.$fword.')(?!>)\b/i'; + $replace[$k] = sprintf('$1', $class); + } + + return preg_replace($pattern, $replace, $inp); + } }