@@ -348,14 +348,13 @@ public function isOlderVersion()
348348 */
349349 public function update ()
350350 {
351-
352351 $ this ->getModel ()->fill ([
353352 'behind ' => $ this ->fetchBehind (),
354353 'last_commit ' => $ this ->fetchLastCommit (),
355354 'current_commit ' => $ this ->variables ('git ' ),
356355 ])->save ();
357356
358- // $this->updateSections();
357+ $ this ->updateSections ();
359358 }
360359
361360 /**
@@ -365,49 +364,60 @@ public function update()
365364 */
366365 public function getSections (): Collection
367366 {
367+ $ content = Str::of ($ this ->content ());
368+
368369 // Разбиваем HTML содержимое на разделы по заголовкам
369- preg_match_all ('/<h(\d)>(.+)<\/h\d>(.*)/sU ' , $ this -> content (), $ matches , PREG_SET_ORDER );
370+ preg_match_all ('/<h(\d)>(.+)<\/h\d>(.*)/sU ' , $ content -> toString (), $ matches , PREG_SET_ORDER );
370371
371372 // Массив для хранения разделов
372373 $ sections = collect ();
373- $ prevEnd = 0 ;
374+ $ titlePage = $ this -> title () ;
374375
375- $ titlePage = null ;
376376 foreach ($ matches as $ index => $ match ) {
377+ $ tag = $ match [0 ];
378+ $ level = (int ) $ match [1 ];
377379 $ sectionTitle = $ match [2 ];
378380
379- if ($ match [ 1 ] == ' 1 ' ) {
381+ if ($ level === 1 ) {
380382 $ titlePage = $ sectionTitle ;
381383 }
382- // Получаем начальную и конечную позицию текущего заголовка в тексте
383- $ startPos = strpos ($ this ->content (), $ match [0 ], $ prevEnd );
384-
385- // Получаем текст между текущим и предыдущим заголовком
386- if ($ index > 0 ) {
387- $ prevMatch = $ matches [$ index - 1 ];
388- $ prevEnd = strpos ($ this ->content (), $ prevMatch [0 ]) + strlen ($ prevMatch [0 ]);
389- $ sectionContent = substr ($ this ->content (), $ prevEnd , $ startPos - $ prevEnd );
390- } else {
391- $ sectionContent = substr ($ this ->content (), 0 , $ startPos );
384+
385+ $ sectionContent = $ content ->after ($ tag );
386+
387+ if (isset ($ matches [$ index + 1 ])) {
388+ $ sectionContent = $ sectionContent ->before ($ matches [$ index + 1 ][0 ]);
392389 }
393390
394391 $ sections ->push ([
395392 'title_page ' => $ titlePage ,
396- 'title ' => $ sectionTitle ,
397- 'slug ' => Str::of ($ sectionTitle )->slug ()->toString (),
398- 'content ' => $ sectionContent ,
399- 'file ' => $ this ->file ,
400- 'version ' => $ this ->version ,
401- 'id ' => Str::uuid (),
393+ 'title ' => $ sectionTitle ,
394+ 'slug ' => Str::of ($ sectionTitle )->slug ()->toString (),
395+ 'content ' => $ sectionContent ,
396+ 'file ' => $ this ->file ,
397+ 'version ' => $ this ->version ,
398+ 'id ' => Str::uuid (),
399+ 'level ' => $ level ,
400+ 'created_at ' => now (),
401+ 'updated_at ' => now (),
402402 ]);
403403 }
404404
405405 return $ sections ;
406406 }
407407
408+ /**
409+ * @return void
410+ */
408411 public function updateSections ()
409412 {
410- DocumentationSection::where ('file ' , $ this ->file )->where ('version ' , $ this ->version )->delete ();
413+ if ($ this ->file === 'documentation.md ' ) {
414+ return ;
415+ }
416+
417+ DocumentationSection::where ('file ' , $ this ->file )
418+ ->where ('version ' , $ this ->version )
419+ ->delete ();
420+
411421 DocumentationSection::insert ($ this ->getSections ()->toArray ());
412422 }
413423}
0 commit comments