Skip to content

Commit 2104f80

Browse files
authored
Merge pull request #9 from Xoshbin:fix/search-functionality
fix: dynamic search paths for localized and versioned docs
2 parents 98dfdda + 7dc10e3 commit 2104f80

File tree

5 files changed

+86
-11
lines changed

5 files changed

+86
-11
lines changed

dist/pertuk.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/pertuk.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ class DocsManager {
256256
const ensureIndex = async () => {
257257
if (index) return index;
258258
try {
259-
const res = await fetch("/docs/index.json", {
259+
const indexUrl = input.getAttribute("data-index-url") || "/docs/index.json";
260+
const res = await fetch(indexUrl, {
260261
headers: { Accept: "application/json" },
261262
});
262263
const data = await res.json();
@@ -299,12 +300,14 @@ class DocsManager {
299300
return;
300301
}
301302

303+
const baseUrl = input.getAttribute("data-base-url") || "/docs";
304+
302305
results.innerHTML = items
303306
.slice(0, 8)
304307
.map((it) => {
305308
const href = it.anchor
306-
? `/docs/${it.slug}#${it.anchor}`
307-
: `/docs/${it.slug}`;
309+
? `${baseUrl}/${it.slug}#${it.anchor}`
310+
: `${baseUrl}/${it.slug}`;
308311
const displayTitle = it.heading
309312
? `${it.title} > ${it.heading}`
310313
: it.title;

resources/views/components/header.blade.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,20 @@ class="flex h-8 w-8 items-center justify-center rounded-lg bg-gradient-to-br fro
2626
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
2727
</svg>
2828
</div>
29+
@php
30+
$currentLocale = app()->getLocale();
31+
$currentVersion = $current_version ?? null;
32+
33+
$searchIndexUrl = $currentVersion
34+
? route('pertuk.docs.version.search.json', ['version' => $currentVersion, 'locale' => $currentLocale])
35+
: route('pertuk.docs.search.json', ['locale' => $currentLocale]);
36+
37+
$searchBaseUrl = url('/' . config('pertuk.route_prefix', 'docs') . ($currentVersion ? '/' . $currentVersion : '') . '/' . $currentLocale);
38+
@endphp
2939
<input id="docs-search-input" type="search" placeholder="{{ __('Search documentation...') }}"
3040
aria-label="{{ __('Search documentation') }}"
41+
data-index-url="{{ $searchIndexUrl }}"
42+
data-base-url="{{ $searchBaseUrl }}"
3143
class="w-full rounded-lg border border-gray-300 bg-gray-50/50 ps-10 pe-4 py-2.5 text-sm placeholder-gray-500 outline-none transition-all duration-200 focus:border-orange-500 focus:bg-white focus:ring-2 focus:ring-orange-500/20 dark:border-gray-700 dark:bg-gray-900/50 dark:text-white dark:placeholder-gray-400 dark:focus:border-orange-400 dark:focus:bg-gray-900 dark:focus:ring-orange-400/20"
3244
autocomplete="off" />
3345
<div class="absolute inset-y-0 end-0 pe-3 flex items-center pointer-events-none">

src/Http/Controllers/DocumentController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,13 @@ protected function responseWithCacheHeaders(HttpResponse $response, int $mtime,
134134
/**
135135
* Search index endpoint.
136136
*/
137-
public function searchIndex(Request $request, string $locale): JsonResponse
137+
public function searchIndex(Request $request): JsonResponse
138138
{
139139
$version = $request->route('version');
140+
$locale = $request->route('locale');
141+
140142
$version = is_string($version) ? $version : null;
143+
$locale = is_string($locale) ? $locale : config('pertuk.default_locale', 'en');
141144

142145
$items = DocumentationService::make($version)->buildIndex($locale);
143146

tests/Feature/DocumentationTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
$response->assertSee('Payments', false);
1212
});
1313

14+
it('discovers available versions correctly', function () {
15+
// Set up supported locales
16+
config(['pertuk.supported_locales' => ['en', 'ckb']]);
17+
18+
// Create versioned directories
19+
$this->createTestMarkdownFile('test.md', '# Test', '', 'en', 'v1.0');
20+
$this->createTestMarkdownFile('test.md', '# Test', '', 'ckb', 'v2.0');
21+
22+
$versions = \Xoshbin\Pertuk\Services\DocumentationService::getAvailableVersions();
23+
24+
expect($versions)->toBeArray();
25+
expect($versions)->toContain('v1.0');
26+
expect($versions)->toContain('v2.0');
27+
expect($versions[0])->toBe('v2.0'); // Latest first
28+
});
29+
1430
it('renders a doc page with TOC and breadcrumbs', function () {
1531
// Create a test document with multiple headings for TOC
1632
$content = "---\ntitle: Receipt and Payment Vouchers\norder: 2\n---\n\n# Receipt and Payment Vouchers\n\n## Overview\n\nThis section covers vouchers.\n\n### Types of Vouchers\n\nDifferent types available.\n\n```php\n\$voucher = new Voucher();\n```\n\n## Processing\n\nHow to process vouchers.";
@@ -82,3 +98,44 @@
8298
expect($data)->toHaveCount(1);
8399
expect($data[0])->toHaveKeys(['id', 'slug', 'title', 'heading', 'content', 'anchor']);
84100
});
101+
102+
it('shows versioned documentation search index', function () {
103+
// Create a test document in a versioned directory
104+
$this->createTestMarkdownFile('test-v1.md', "# V1 Document\n\n## Subheading\n\nContent", '', 'en', 'v1');
105+
106+
// Attempt to access versioned search index via route
107+
$response = $this->get('/docs/v1/en/index.json');
108+
109+
$response->assertStatus(200);
110+
$response->assertJsonFragment(['slug' => 'test-v1']);
111+
});
112+
113+
it('renders search input with dynamic data attributes', function () {
114+
// Set up test doc
115+
$this->createTestMarkdownFile('welcome.md', '# Welcome');
116+
117+
// Get a page with the header
118+
$response = $this->get('/docs/en/welcome');
119+
$response->assertOk();
120+
121+
$indexUrl = route('pertuk.docs.search.json', ['locale' => 'en']);
122+
$baseUrl = url('/docs/en');
123+
124+
$response->assertSee('data-index-url="'.$indexUrl.'"', false);
125+
$response->assertSee('data-base-url="'.$baseUrl.'"', false);
126+
});
127+
128+
it('renders versioned search input with dynamic data attributes', function () {
129+
// Set up versioned test doc
130+
$this->createTestMarkdownFile('v10-welcome.md', '# V10 Welcome', '', 'en', 'v10.0');
131+
132+
// Get a versioned page
133+
$response = $this->get('/docs/v10.0/en/v10-welcome');
134+
$response->assertOk();
135+
136+
$indexUrl = route('pertuk.docs.version.search.json', ['version' => 'v10.0', 'locale' => 'en']);
137+
$baseUrl = url('/docs/v10.0/en');
138+
139+
$response->assertSee('data-index-url="'.$indexUrl.'"', false);
140+
$response->assertSee('data-base-url="'.$baseUrl.'"', false);
141+
});

0 commit comments

Comments
 (0)