@@ -192,7 +192,7 @@ protected function setUniqueId($element, array &$idMap)
192192 // Create an unique id for the element
193193 // Uses the content as a basis to ensure output is the same every time
194194 // the same content is passed through.
195- $ contentId = 'bkmrk- ' . substr (strtolower (preg_replace ('/\s+/ ' , '- ' , trim ($ element ->nodeValue ))), 0 , 20 );
195+ $ contentId = 'bkmrk- ' . mb_substr (strtolower (preg_replace ('/\s+/ ' , '- ' , trim ($ element ->nodeValue ))), 0 , 20 );
196196 $ newId = urlencode ($ contentId );
197197 $ loopIndex = 0 ;
198198
@@ -422,25 +422,29 @@ public function getPageNav(string $pageContent)
422422 return [];
423423 }
424424
425- $ tree = collect ([]);
426- foreach ($ headers as $ header ) {
427- $ text = $ header ->nodeValue ;
428- $ tree ->push ([
425+ $ tree = collect ($ headers )->map (function ($ header ) {
426+ $ text = trim (str_replace ("\xc2\xa0" , '' , $ header ->nodeValue ));
427+ if (mb_strlen ($ text ) > 30 ) {
428+ $ text = mb_substr ($ text , 0 , 27 ) . '... ' ;
429+ }
430+
431+ return [
429432 'nodeName ' => strtolower ($ header ->nodeName ),
430433 'level ' => intval (str_replace ('h ' , '' , $ header ->nodeName )),
431434 'link ' => '# ' . $ header ->getAttribute ('id ' ),
432- 'text ' => strlen ($ text ) > 30 ? substr ($ text , 0 , 27 ) . '... ' : $ text
433- ]);
434- }
435+ 'text ' => $ text ,
436+ ];
437+ })->filter (function ($ header ) {
438+ return mb_strlen ($ header ['text ' ]) > 0 ;
439+ });
435440
436441 // Normalise headers if only smaller headers have been used
437- if (count ($ tree ) > 0 ) {
438- $ minLevel = $ tree ->pluck ('level ' )->min ();
439- $ tree = $ tree ->map (function ($ header ) use ($ minLevel ) {
440- $ header ['level ' ] -= ($ minLevel - 2 );
441- return $ header ;
442- });
443- }
442+ $ minLevel = $ tree ->pluck ('level ' )->min ();
443+ $ tree = $ tree ->map (function ($ header ) use ($ minLevel ) {
444+ $ header ['level ' ] -= ($ minLevel - 2 );
445+ return $ header ;
446+ });
447+
444448 return $ tree ->toArray ();
445449 }
446450
0 commit comments