90 lines
2.3 KiB
PHP
90 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Str;
|
|
use League\CommonMark\Environment\Environment;
|
|
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
|
|
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
|
|
use League\CommonMark\MarkdownConverter;
|
|
|
|
class ProjectDocumentationContent
|
|
{
|
|
public static function markdownPath(): string
|
|
{
|
|
return base_path('dev/entwicklung.md');
|
|
}
|
|
|
|
public static function html(): string
|
|
{
|
|
$mdPath = self::markdownPath();
|
|
|
|
if (! file_exists($mdPath)) {
|
|
return '<p class="text-red-600">Dokumentation nicht gefunden.</p>';
|
|
}
|
|
|
|
$markdown = file_get_contents($mdPath);
|
|
|
|
$environment = new Environment([
|
|
'html_input' => 'allow',
|
|
'allow_unsafe_links' => false,
|
|
]);
|
|
|
|
$environment->addExtension(new CommonMarkCoreExtension);
|
|
$environment->addExtension(new GithubFlavoredMarkdownExtension);
|
|
|
|
$converter = new MarkdownConverter($environment);
|
|
|
|
return $converter->convert($markdown)->getContent();
|
|
}
|
|
|
|
/**
|
|
* @return list<array{level: int, title: string, slug: string}>
|
|
*/
|
|
public static function tableOfContents(): array
|
|
{
|
|
$mdPath = self::markdownPath();
|
|
|
|
if (! file_exists($mdPath)) {
|
|
return [];
|
|
}
|
|
|
|
$markdown = file_get_contents($mdPath);
|
|
$toc = [];
|
|
|
|
preg_match_all('/^(#{2,3})\s+(.+)$/m', $markdown, $matches, PREG_SET_ORDER);
|
|
|
|
foreach ($matches as $match) {
|
|
$level = strlen($match[1]);
|
|
$title = trim($match[2]);
|
|
$slug = Str::slug($title);
|
|
|
|
$toc[] = [
|
|
'level' => $level,
|
|
'title' => $title,
|
|
'slug' => $slug,
|
|
];
|
|
}
|
|
|
|
return $toc;
|
|
}
|
|
|
|
/**
|
|
* @return array{size: string, modified: string, lines: int}|null
|
|
*/
|
|
public static function fileInfo(): ?array
|
|
{
|
|
$mdPath = self::markdownPath();
|
|
|
|
if (! file_exists($mdPath)) {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
'size' => round(filesize($mdPath) / 1024, 1).' KB',
|
|
'modified' => Carbon::parse(filemtime($mdPath))->format('d.m.Y H:i'),
|
|
'lines' => count(file($mdPath)),
|
|
];
|
|
}
|
|
}
|