Skip to content

Commit a49cac7

Browse files
simonhampclaude
andcommitted
Scope Algolia DocSearch to current docs platform and version
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 40b1a5e commit a49cac7

File tree

3 files changed

+79
-9
lines changed

3 files changed

+79
-9
lines changed

resources/js/app.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,40 @@ Alpine.data('countdown', (iso) => ({
114114
Livewire.start()
115115

116116
// Docsearch
117-
docsearch({
117+
const docsPathMatch = window.location.pathname.match(/^\/docs\/(desktop|mobile)\/(\d+)/)
118+
const docsearchOptions = {
118119
appId: 'ZNII9QZ8WI',
119120
apiKey: '9be495a1aaf367b47c873d30a8e7ccf5',
120121
indexName: 'nativephp',
121122
insights: true,
122-
container: '#docsearch-desktop',
123123
debug: false,
124-
})
124+
...(docsPathMatch && {
125+
transformItems(items) {
126+
const prefix = `/docs/${docsPathMatch[1]}/${docsPathMatch[2]}/`
127+
return items.filter((item) => {
128+
try {
129+
return new URL(item.url).pathname.startsWith(prefix)
130+
} catch {
131+
return item.url.includes(prefix)
132+
}
133+
})
134+
},
135+
}),
136+
}
125137

126138
docsearch({
127-
appId: 'ZNII9QZ8WI',
128-
apiKey: '9be495a1aaf367b47c873d30a8e7ccf5',
129-
indexName: 'nativephp',
130-
insights: true,
131-
container: '#docsearch-mobile',
132-
debug: false,
139+
...docsearchOptions,
140+
container: '#docsearch-desktop',
133141
})
142+
143+
// Mirror the desktop DocSearch button into the mobile container so that
144+
// pressing Cmd+K only registers one handler (avoiding duplicate modals).
145+
const mobileContainer = document.getElementById('docsearch-mobile')
146+
if (mobileContainer) {
147+
const desktopButton = document.querySelector('#docsearch-desktop .DocSearch-Button')
148+
if (desktopButton) {
149+
const mobileButton = desktopButton.cloneNode(true)
150+
mobileContainer.appendChild(mobileButton)
151+
mobileButton.addEventListener('click', () => desktopButton.click())
152+
}
153+
}

resources/views/docs/index.blade.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
@push('head')
2+
<meta name="docsearch:platform" content="{{ $platform }}" />
3+
<meta name="docsearch:version" content="{{ $version }}" />
4+
@endpush
5+
16
<x-docs-layout>
27
<x-slot name="sidebarLeft">
38
{!! $navigation !!}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use Tests\TestCase;
8+
9+
class DocsSearchMetaTagsTest extends TestCase
10+
{
11+
use RefreshDatabase;
12+
13+
#[Test]
14+
public function docs_page_includes_docsearch_platform_and_version_meta_tags_for_mobile(): void
15+
{
16+
$this
17+
->withoutVite()
18+
->get('/docs/mobile/3/getting-started/introduction')
19+
->assertStatus(200)
20+
->assertSee('<meta name="docsearch:platform" content="mobile" />', false)
21+
->assertSee('<meta name="docsearch:version" content="3" />', false);
22+
}
23+
24+
#[Test]
25+
public function docs_page_includes_docsearch_platform_and_version_meta_tags_for_desktop(): void
26+
{
27+
$this
28+
->withoutVite()
29+
->get('/docs/desktop/2/getting-started/introduction')
30+
->assertStatus(200)
31+
->assertSee('<meta name="docsearch:platform" content="desktop" />', false)
32+
->assertSee('<meta name="docsearch:version" content="2" />', false);
33+
}
34+
35+
#[Test]
36+
public function non_docs_page_does_not_include_docsearch_meta_tags(): void
37+
{
38+
$this
39+
->withoutVite()
40+
->get('/')
41+
->assertStatus(200)
42+
->assertDontSee('docsearch:platform', false)
43+
->assertDontSee('docsearch:version', false);
44+
}
45+
}

0 commit comments

Comments
 (0)