feat: use xpier media library for uploads and markdown embeds

Store media:ID in post bodies instead of CDN URLs, and keep sablog attach tokens as a fallback.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
gouki
2026-09-16 19:03:52 +00:00
co-authored by Cursor
parent f6a9a8ea50
commit f81397a289
38 changed files with 5390 additions and 2059 deletions
+7 -2
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Blog\Services;
use App\Blog\Support\MarkdownMediaEmbed;
use App\Models\Post;
use Illuminate\Support\Facades\Cache;
use League\CommonMark\Environment\Environment;
@@ -36,11 +37,15 @@ class PostContentRenderer
*/
public function render(Post $post): string
{
$embed = app(MarkdownMediaEmbed::class);
$raw = (string) $post->content;
$content = $post->content_format === 'markdown'
? $this->toHtml($post->content)
: $post->content;
? $this->toHtml($embed->expandMarkdown($raw))
: $raw;
$html = $this->renderShortcodes($content, $post);
$html = $embed->hydrateHtml($html);
// 插件钩子:付费内容过滤、AI 处理等
$html = app(\App\Blog\Support\PluginManager::class)->applyFilters('post.rendered', $html, $post);
+131
View File
@@ -0,0 +1,131 @@
<?php
declare(strict_types=1);
namespace App\Blog\Support;
use Xpier\FilamentMediaLibrary\Models\MediaLibrary;
/**
* Markdown/HTML body must store library references, never CDN URLs.
*
* Markdown: ![alt](media:12) or [file.pdf](media:12)
* HTML: [media=12]
*/
class MarkdownMediaEmbed
{
public const PROTOCOL = 'media:';
public function snippet(MediaLibrary $media, string $format = 'markdown'): string
{
$id = (int) $media->getKey();
$isImage = $this->isImage($media);
if ($format === 'html') {
return '[media='.$id.']';
}
$label = $this->escapeMdLabel($this->label($media, $isImage));
return $isImage
? '!['.$label.']('.self::PROTOCOL.$id.')'
: '['.$label.']('.self::PROTOCOL.$id.')';
}
public function expandMarkdown(string $markdown): string
{
return preg_replace_callback(
'/\]\(media:(\d+)\)/i',
function (array $matches): string {
$url = $this->publicUrl((int) $matches[1]);
return $url !== null ? ']('.$url.')' : $matches[0];
},
$markdown
) ?? $markdown;
}
public function hydrateHtml(string $html): string
{
$html = preg_replace_callback(
'/\b(src|href)=([\'"])media:(\d+)\2/i',
function (array $matches): string {
$url = $this->publicUrl((int) $matches[3]);
if ($url === null) {
return $matches[0];
}
return $matches[1].'='.$matches[2].$url.$matches[2];
},
$html
) ?? $html;
return preg_replace_callback(
'/\[media=(\d+)\]/i',
function (array $matches): string {
$id = (int) $matches[1];
$media = $this->find($id);
$url = $this->publicUrl($id);
if ($url === null) {
return '';
}
if ($media && $this->isImage($media)) {
return sprintf(
'<img src="%s" alt="%s" loading="lazy" />',
e($url),
e($this->label($media, true))
);
}
$label = $media ? $this->label($media, false) : 'file-'.$id;
return sprintf('<a href="%s">%s</a>', e($url), e($label));
},
$html
) ?? $html;
}
public function find(int $id): ?MediaLibrary
{
try {
return MediaLibrary::query()->find($id);
} catch (\Throwable) {
return null;
}
}
public function publicUrl(int $id): ?string
{
$media = $this->find($id);
$url = $media?->url;
return filled($url) ? (string) $url : null;
}
public function isImage(MediaLibrary $media): bool
{
if ($media->type === MediaLibrary::TYPE_IMAGE) {
return true;
}
return is_string($media->mime_type) && str_starts_with($media->mime_type, 'image/');
}
protected function label(MediaLibrary $media, bool $isImage): string
{
if (filled($media->alt_text)) {
return (string) $media->alt_text;
}
$name = (string) ($media->original_name ?: basename((string) $media->path) ?: 'media-'.$media->getKey());
return $isImage ? (pathinfo($name, PATHINFO_FILENAME) ?: $name) : $name;
}
protected function escapeMdLabel(string $label): string
{
return str_replace(['[', ']', '!'], ['\\[', '\\]', '\\!'], $label);
}
}