From c94d00dfe1e62d80ac885a9e21d1706776bd0a49 Mon Sep 17 00:00:00 2001 From: agoalofalife Date: Tue, 19 Mar 2024 21:42:29 +0800 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE?= =?UTF-8?q?=D0=BD=D0=B0=D0=BB=D0=B0=20=D0=BF=D0=BE=20=D0=BF=D0=BE=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 2 + app/Docs.php | 26 ++++---- app/Http/Controllers/DocsController.php | 17 ++++++ app/Models/DocumentationSection.php | 59 +++++++++++++++++++ composer.json | 2 +- composer.lock | 4 +- ...45_create_documentation_sections_table.php | 34 +++++++++++ package-lock.json | 4 +- public/build/manifest.json | 2 +- .../js/controllers/search-docs_controller.js | 14 +++++ resources/views/docs/_search_lines.blade.php | 17 ++++++ resources/views/docs/docs.blade.php | 32 ++++++++-- routes/web.php | 2 + 13 files changed, 194 insertions(+), 21 deletions(-) create mode 100644 app/Models/DocumentationSection.php create mode 100644 database/migrations/2024_03_17_133145_create_documentation_sections_table.php create mode 100644 resources/js/controllers/search-docs_controller.js create mode 100644 resources/views/docs/_search_lines.blade.php diff --git a/.env.example b/.env.example index 32b5d026..93c8efb6 100644 --- a/.env.example +++ b/.env.example @@ -71,3 +71,5 @@ QUIZ_STATUS=false TELEGRAM_BOT_TOKEN= TELEGRAM_CHAT_ID= TELEGRAM_CHANNEL_ID= + +SCOUT_DRIVER= diff --git a/app/Docs.php b/app/Docs.php index e092f7aa..6014b8b4 100644 --- a/app/Docs.php +++ b/app/Docs.php @@ -361,10 +361,11 @@ public function update() /** * Разбивает markdown файл на разделы по заголовкам. * - * @return Массив разделов с заголовками и содержимым + * @return Collection разделов с заголовками и содержимым */ - public function getSections() + public function getSections(): Collection { + // Разбиваем HTML содержимое на разделы по заголовкам preg_match_all('/(.+)<\/h\d>(.*)/sU', $this->content(), $matches, PREG_SET_ORDER); @@ -372,9 +373,13 @@ public function getSections() $sections = collect(); $prevEnd = 0; + $titlePage = null; foreach ($matches as $index => $match) { $sectionTitle = $match[2]; + if ($match[1] == '1') { + $titlePage = $sectionTitle; + } // Получаем начальную и конечную позицию текущего заголовка в тексте $startPos = strpos($this->content(), $match[0], $prevEnd); @@ -388,12 +393,13 @@ public function getSections() } $sections->push([ - 'title' => $sectionTitle, - 'slug' => Str::of($sectionTitle)->slug()->toString(), - 'content' => $sectionContent, - 'file' => $this->file, - 'version' => $this->version, - 'id' => Str::uuid(), + 'title_page' => $titlePage, + 'title' => $sectionTitle, + 'slug' => Str::of($sectionTitle)->slug()->toString(), + 'content' => $sectionContent, + 'file' => $this->file, + 'version' => $this->version, + 'id' => Str::uuid(), ]); } @@ -402,7 +408,7 @@ public function getSections() public function updateSections() { - // DocumentationSection::where('file', $this->file)->where('version', $this->version)->delete(); - // DocumentationSection::insert($this->getSections()->toArray()); + DocumentationSection::where('file', $this->file)->where('version', $this->version)->delete(); + DocumentationSection::insert($this->getSections()->toArray()); } } diff --git a/app/Http/Controllers/DocsController.php b/app/Http/Controllers/DocsController.php index 6fad13e3..65da5bbc 100644 --- a/app/Http/Controllers/DocsController.php +++ b/app/Http/Controllers/DocsController.php @@ -4,6 +4,8 @@ use App\Docs; use App\Models\Document; +use App\Models\DocumentationSection; +use Illuminate\Http\Request; class DocsController extends Controller { @@ -58,4 +60,19 @@ public function status(string $version = Docs::DEFAULT_VERSION) 'documents' => $documents, ]); } + + public function search(string $versionOfDocs, Request $request) + { + + if (empty($request->text)) { + return turbo_stream()->replace('found_candidates', view('docs._search_lines', [ + 'searchOffer' => [], + ])); + } + $searchOffers = DocumentationSection::search($request->text)->where('version', $versionOfDocs)->get(); + + return turbo_stream()->replace('found_candidates', view('docs._search_lines', [ + 'searchOffer' => $searchOffers, + ])); + } } diff --git a/app/Models/DocumentationSection.php b/app/Models/DocumentationSection.php new file mode 100644 index 00000000..ac4aef67 --- /dev/null +++ b/app/Models/DocumentationSection.php @@ -0,0 +1,59 @@ + + + Str::limit( + nl2br(strip_tags($attributes['content'])), + 90 + ), + ); + } + + protected function fileForUrl(): Attribute + { + return Attribute::make( + get: fn (mixed $value, array $attributes) => str_replace('.md', '', $attributes['file']), + ); + } + + public function toSearchableArray() + { + return [ + 'title' => $this->title, + 'content' => $this->content, + ]; + } +} diff --git a/composer.json b/composer.json index 71375dec..a1152f45 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "laravel-notification-channels/webpush": "^7.1", "laravel/framework": "^10.31", "laravel/sanctum": "^3.2", - "laravel/scout": "^10.5", + "laravel/scout": "^10.8", "laravel/socialite": "^5.9", "laravel/telescope": "^4.17", "laravel/tinker": "^2.8", diff --git a/composer.lock b/composer.lock index faa6ec06..e97edf0d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "37ed9f3e45ee8446ff1f8c65e595177c", + "content-hash": "173bc161a355dcbae3280d036ea82b30", "packages": [ { "name": "brick/math", @@ -11724,5 +11724,5 @@ "php": ">=8.1" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" } diff --git a/database/migrations/2024_03_17_133145_create_documentation_sections_table.php b/database/migrations/2024_03_17_133145_create_documentation_sections_table.php new file mode 100644 index 00000000..21150dec --- /dev/null +++ b/database/migrations/2024_03_17_133145_create_documentation_sections_table.php @@ -0,0 +1,34 @@ +uuid('id')->primary(); + $table->string('title_page')->comment('Заголовок всей страницы'); + $table->string('title'); + $table->string('slug'); + $table->string('version'); + $table->string('file'); + $table->text('content'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('documentation_sections'); + } +}; diff --git a/package-lock.json b/package-lock.json index e70ca53d..183fa817 100644 --- a/package-lock.json +++ b/package-lock.json @@ -674,7 +674,8 @@ "node_modules/@hotwired/stimulus": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/@hotwired/stimulus/-/stimulus-3.2.2.tgz", - "integrity": "sha512-eGeIqNOQpXoPAIP7tC1+1Yc1yl1xnwYqg+3mzqxyrbE5pg5YFBZcA6YoTiByJB6DKAEsiWtl6tjTJS4IYtbB7A==" + "integrity": "sha512-eGeIqNOQpXoPAIP7tC1+1Yc1yl1xnwYqg+3mzqxyrbE5pg5YFBZcA6YoTiByJB6DKAEsiWtl6tjTJS4IYtbB7A==", + "dev": true }, "node_modules/@hotwired/turbo": { "version": "8.0.4", @@ -1435,6 +1436,7 @@ "version": "1.29.0", "resolved": "https://registry.npmjs.org/prismjs/-/prismjs-1.29.0.tgz", "integrity": "sha512-Kx/1w86q/epKcmte75LNrEoT+lX8pBpavuAbvJWRXar7Hz8jrtF+e3vY751p0R8H9HdArwaCTNDDzHg/ScJK1Q==", + "dev": true, "engines": { "node": ">=6" } diff --git a/public/build/manifest.json b/public/build/manifest.json index 6ad83390..e6b46089 100644 --- a/public/build/manifest.json +++ b/public/build/manifest.json @@ -17,7 +17,7 @@ "isEntry": true }, "resources/js/app.js": { - "file": "assets/app-BUWIDZ8f.js", + "file": "assets/app-CnjHucs5.js", "src": "resources/js/app.js", "isEntry": true, "css": [ diff --git a/resources/js/controllers/search-docs_controller.js b/resources/js/controllers/search-docs_controller.js new file mode 100644 index 00000000..7d2cf036 --- /dev/null +++ b/resources/js/controllers/search-docs_controller.js @@ -0,0 +1,14 @@ +import { Controller } from '@hotwired/stimulus'; + +export default class extends Controller { + static targets = [ "text", "spinner"] + /** + * Port for laravel.com + */ + connect() {} + search(event) { + this.spinnerTarget.classList.remove('d-none'); + event.target.form.requestSubmit(); + setTimeout(() => this.spinnerTarget.classList.add('d-none'), 500) + } +} diff --git a/resources/views/docs/_search_lines.blade.php b/resources/views/docs/_search_lines.blade.php new file mode 100644 index 00000000..f9e40fb5 --- /dev/null +++ b/resources/views/docs/_search_lines.blade.php @@ -0,0 +1,17 @@ + + diff --git a/resources/views/docs/docs.blade.php b/resources/views/docs/docs.blade.php index 8a4f3add..2e869d87 100644 --- a/resources/views/docs/docs.blade.php +++ b/resources/views/docs/docs.blade.php @@ -7,12 +7,32 @@
-
+
+
+
+ +
+ @csrf + +
+{{-- --}} +
+
- {{-- - ---}}