Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,40 @@ Alpine.data('countdown', (iso) => ({
Livewire.start()

// Docsearch
docsearch({
const docsPathMatch = window.location.pathname.match(/^\/docs\/(desktop|mobile)\/(\d+)/)
const docsearchOptions = {
appId: 'ZNII9QZ8WI',
apiKey: '9be495a1aaf367b47c873d30a8e7ccf5',
indexName: 'nativephp',
insights: true,
container: '#docsearch-desktop',
debug: false,
})
...(docsPathMatch && {
transformItems(items) {
const prefix = `/docs/${docsPathMatch[1]}/${docsPathMatch[2]}/`
return items.filter((item) => {
try {
return new URL(item.url).pathname.startsWith(prefix)
} catch {
return item.url.includes(prefix)
}
})
},
}),
}

docsearch({
appId: 'ZNII9QZ8WI',
apiKey: '9be495a1aaf367b47c873d30a8e7ccf5',
indexName: 'nativephp',
insights: true,
container: '#docsearch-mobile',
debug: false,
...docsearchOptions,
container: '#docsearch-desktop',
})

// Mirror the desktop DocSearch button into the mobile container so that
// pressing Cmd+K only registers one handler (avoiding duplicate modals).
const mobileContainer = document.getElementById('docsearch-mobile')
if (mobileContainer) {
const desktopButton = document.querySelector('#docsearch-desktop .DocSearch-Button')
if (desktopButton) {
const mobileButton = desktopButton.cloneNode(true)
mobileContainer.appendChild(mobileButton)
mobileButton.addEventListener('click', () => desktopButton.click())
}
}
5 changes: 5 additions & 0 deletions resources/views/docs/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
@push('head')
<meta name="docsearch:platform" content="{{ $platform }}" />
<meta name="docsearch:version" content="{{ $version }}" />
@endpush

<x-docs-layout>
<x-slot name="sidebarLeft">
{!! $navigation !!}
Expand Down
45 changes: 45 additions & 0 deletions tests/Feature/DocsSearchMetaTagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class DocsSearchMetaTagsTest extends TestCase
{
use RefreshDatabase;

#[Test]
public function docs_page_includes_docsearch_platform_and_version_meta_tags_for_mobile(): void
{
$this
->withoutVite()
->get('/docs/mobile/3/getting-started/introduction')
->assertStatus(200)
->assertSee('<meta name="docsearch:platform" content="mobile" />', false)
->assertSee('<meta name="docsearch:version" content="3" />', false);
}

#[Test]
public function docs_page_includes_docsearch_platform_and_version_meta_tags_for_desktop(): void
{
$this
->withoutVite()
->get('/docs/desktop/2/getting-started/introduction')
->assertStatus(200)
->assertSee('<meta name="docsearch:platform" content="desktop" />', false)
->assertSee('<meta name="docsearch:version" content="2" />', false);
}

#[Test]
public function non_docs_page_does_not_include_docsearch_meta_tags(): void
{
$this
->withoutVite()
->get('/')
->assertStatus(200)
->assertDontSee('docsearch:platform', false)
->assertDontSee('docsearch:version', false);
}
}
Loading