@@ -348,14 +348,13 @@ public function isOlderVersion()
348
348
*/
349
349
public function update ()
350
350
{
351
-
352
351
$ this ->getModel ()->fill ([
353
352
'behind ' => $ this ->fetchBehind (),
354
353
'last_commit ' => $ this ->fetchLastCommit (),
355
354
'current_commit ' => $ this ->variables ('git ' ),
356
355
])->save ();
357
356
358
- // $this->updateSections();
357
+ $ this ->updateSections ();
359
358
}
360
359
361
360
/**
@@ -365,49 +364,60 @@ public function update()
365
364
*/
366
365
public function getSections (): Collection
367
366
{
367
+ $ content = Str::of ($ this ->content ());
368
+
368
369
// Разбиваем 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 );
370
371
371
372
// Массив для хранения разделов
372
373
$ sections = collect ();
373
- $ prevEnd = 0 ;
374
+ $ titlePage = $ this -> title () ;
374
375
375
- $ titlePage = null ;
376
376
foreach ($ matches as $ index => $ match ) {
377
+ $ tag = $ match [0 ];
378
+ $ level = (int ) $ match [1 ];
377
379
$ sectionTitle = $ match [2 ];
378
380
379
- if ($ match [ 1 ] == ' 1 ' ) {
381
+ if ($ level === 1 ) {
380
382
$ titlePage = $ sectionTitle ;
381
383
}
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 ]);
392
389
}
393
390
394
391
$ sections ->push ([
395
392
'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 (),
402
402
]);
403
403
}
404
404
405
405
return $ sections ;
406
406
}
407
407
408
+ /**
409
+ * @return void
410
+ */
408
411
public function updateSections ()
409
412
{
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
+
411
421
DocumentationSection::insert ($ this ->getSections ()->toArray ());
412
422
}
413
423
}
0 commit comments