12
12
use Illuminate \Support \Str ;
13
13
use Illuminate \View \View ;
14
14
use InvalidArgumentException ;
15
- use Spatie \Menu \Link ;
15
+ use Spatie \Menu \Html ;
16
16
use Spatie \Menu \Menu ;
17
17
use Spatie \YamlFrontMatter \YamlFrontMatter ;
18
18
use Symfony \Component \Finder \Finder ;
@@ -39,9 +39,10 @@ public function __invoke(Request $request, string $version, ?string $page = null
39
39
}
40
40
41
41
try {
42
- $ pageProperties = Cache::remember ("docs_ {$ version }_ {$ page }" , now ()->addDay (), function () use ($ version , $ page ) {
43
- return $ this ->getPageProperties ($ version , $ page );
44
- });
42
+ $ pageProperties = Cache::remember ("docs_ {$ version }_ {$ page }" , now ()->addDay (),
43
+ function () use ($ version , $ page ) {
44
+ return $ this ->getPageProperties ($ version , $ page );
45
+ });
45
46
} catch (InvalidArgumentException $ e ) {
46
47
return $ this ->redirectToFirstNavigationPage ($ navigation , $ page );
47
48
}
@@ -77,11 +78,33 @@ protected function getPageProperties($version, $page = null): array
77
78
if (array_key_exists ('path ' , $ nav )) {
78
79
$ menu ->link ($ nav ['path ' ], $ nav ['title ' ]);
79
80
} elseif (array_key_exists ('children ' , $ nav )) {
80
- $ menu ->submenu (Link::to ($ nav ['children ' ][0 ]['path ' ], $ nav ['title ' ]), function (Menu $ submenu ) use ($ nav ) {
81
- foreach ($ nav ['children ' ] as $ child ) {
82
- $ submenu ->link ($ child ['path ' ], $ child ['title ' ]);
83
- }
84
- });
81
+ $ menu ->setItemParentAttribute ('x-data ' , '{ open: $el.classList.contains( \'active \') } ' );
82
+
83
+ $ header = Html::raw ('
84
+ <a href=" ' .$ nav ['children ' ][0 ]['path ' ].'" class="flex items-center gap-2 justify-between" x-on:click.prevent="open = !open">
85
+ <span> ' .$ nav ['title ' ].'</span>
86
+ <span class="text-gray-400 dark:text-gray-600">
87
+ <svg x-show="open" x-cloak class="size-3" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
88
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
89
+ </svg>
90
+ <svg x-show="!open" x-cloak class="size-3" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
91
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 15l7-7 7 7"></path>
92
+ </svg>
93
+ </span>
94
+ </a>
95
+ ' );
96
+
97
+ $ submenu = Menu::new ()
98
+ ->setAttributes ([
99
+ 'x-show ' => 'open ' ,
100
+ 'x-collapse ' => '' ,
101
+ 'x-cloak ' => '' ,
102
+ ]);
103
+ foreach ($ nav ['children ' ] as $ child ) {
104
+ $ submenu ->link ($ child ['path ' ], $ child ['title ' ]);
105
+ }
106
+
107
+ $ menu ->submenu ($ header , $ submenu );
85
108
}
86
109
})
87
110
->setActive (\request ()->path ())
@@ -223,6 +246,7 @@ protected function extractTableOfContents(string $document): array
223
246
'anchor ' => Str::slug (Str::replace ('` ' , 'code ' , $ title )),
224
247
];
225
248
})
249
+ ->values ()
226
250
->toArray ();
227
251
}
228
252
@@ -263,7 +287,7 @@ protected function redirectToFirstNavigationPage(array $navigation, $page = null
263
287
->filter (function ($ nav ) {
264
288
return array_key_exists ('path ' , $ nav ) || array_key_exists ('children ' , $ nav );
265
289
})
266
- ->map (function ($ nav ) {
290
+ ->flatMap (function ($ nav ) {
267
291
if (array_key_exists ('path ' , $ nav )) {
268
292
return $ nav ;
269
293
}
@@ -273,14 +297,14 @@ protected function redirectToFirstNavigationPage(array $navigation, $page = null
273
297
274
298
return null ;
275
299
})
276
- ->flatten (1 )
277
300
->first ();
278
301
279
302
if (is_null ($ firstNavigationPath ) && ! is_null ($ page )) {
280
303
return $ this ->redirectToFirstNavigationPage ($ navigation );
281
304
}
282
305
283
- return is_string ($ firstNavigationPath ) ? redirect ($ firstNavigationPath , 301 ) : redirect ($ firstNavigationPath ['path ' ], 301 );
306
+ return is_string ($ firstNavigationPath ) ? redirect ($ firstNavigationPath ,
307
+ 301 ) : redirect ($ firstNavigationPath ['path ' ], 301 );
284
308
}
285
309
286
310
protected function getMarkdownView ($ view , array $ data = [], array $ mergeData = []): View
0 commit comments