2026-02-11 17:28:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
class MarkdownRenderer
|
|
|
|
|
{
|
|
|
|
|
public function render(?string $content): string
|
|
|
|
|
{
|
|
|
|
|
$markdown = trim((string) $content);
|
|
|
|
|
|
|
|
|
|
if ($markdown === '') {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 13:06:12 +08:00
|
|
|
$markdown = $this->normalizeStorageLinks($markdown);
|
|
|
|
|
|
2026-02-11 17:28:36 +08:00
|
|
|
return (string) Str::markdown($markdown, [
|
|
|
|
|
'html_input' => 'strip',
|
|
|
|
|
'allow_unsafe_links' => false,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-02-12 13:06:12 +08:00
|
|
|
|
|
|
|
|
private function normalizeStorageLinks(string $markdown): string
|
|
|
|
|
{
|
|
|
|
|
$pattern = '/https?:\/\/(?:localhost|127\.0\.0\.1)(?::\d+)?\/storage\/([^\s)"\'\<\>]+)/i';
|
|
|
|
|
$normalized = preg_replace($pattern, '/storage/$1', $markdown);
|
|
|
|
|
|
|
|
|
|
return is_string($normalized) ? $normalized : $markdown;
|
|
|
|
|
}
|
2026-02-11 17:28:36 +08:00
|
|
|
}
|