1212use Illuminate \Support \Str ;
1313use Illuminate \View \View ;
1414use InvalidArgumentException ;
15- use Spatie \Menu \Link ;
15+ use Spatie \Menu \Html ;
1616use Spatie \Menu \Menu ;
1717use Spatie \YamlFrontMatter \YamlFrontMatter ;
1818use Symfony \Component \Finder \Finder ;
@@ -39,9 +39,10 @@ public function __invoke(Request $request, string $version, ?string $page = null
3939 }
4040
4141 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+ });
4546 } catch (InvalidArgumentException $ e ) {
4647 return $ this ->redirectToFirstNavigationPage ($ navigation , $ page );
4748 }
@@ -77,11 +78,33 @@ protected function getPageProperties($version, $page = null): array
7778 if (array_key_exists ('path ' , $ nav )) {
7879 $ menu ->link ($ nav ['path ' ], $ nav ['title ' ]);
7980 } 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 );
85108 }
86109 })
87110 ->setActive (\request ()->path ())
@@ -223,6 +246,7 @@ protected function extractTableOfContents(string $document): array
223246 'anchor ' => Str::slug (Str::replace ('` ' , 'code ' , $ title )),
224247 ];
225248 })
249+ ->values ()
226250 ->toArray ();
227251 }
228252
@@ -263,7 +287,7 @@ protected function redirectToFirstNavigationPage(array $navigation, $page = null
263287 ->filter (function ($ nav ) {
264288 return array_key_exists ('path ' , $ nav ) || array_key_exists ('children ' , $ nav );
265289 })
266- ->map (function ($ nav ) {
290+ ->flatMap (function ($ nav ) {
267291 if (array_key_exists ('path ' , $ nav )) {
268292 return $ nav ;
269293 }
@@ -273,14 +297,14 @@ protected function redirectToFirstNavigationPage(array $navigation, $page = null
273297
274298 return null ;
275299 })
276- ->flatten (1 )
277300 ->first ();
278301
279302 if (is_null ($ firstNavigationPath ) && ! is_null ($ page )) {
280303 return $ this ->redirectToFirstNavigationPage ($ navigation );
281304 }
282305
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 );
284308 }
285309
286310 protected function getMarkdownView ($ view , array $ data = [], array $ mergeData = []): View
0 commit comments