feat: use xpier media library for uploads and markdown embeds
Build and Deploy / verify (push) Successful in 35s
Build and Deploy / deploy (push) Successful in 33s

Store media:ID in article 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 0b0110a681
commit 64655ced1e
49 changed files with 5615 additions and 2132 deletions
+2
View File
@@ -36,12 +36,14 @@ class ContentRenderer
if ($format === ContentFormat::MARKDOWN) {
$content = $this->attachEmbed->legacyToMarkdownReference($content, $articleId);
$content = $this->attachEmbed->expandMarkdownAttachProtocol($content);
$content = app(\App\Domain\Media\MarkdownMediaEmbed::class)->expandMarkdown($content);
$html = $this->markdownToHtml($content);
} else {
$html = $content;
}
$html = $this->attachEmbed->hydrateHtml($html, $articleId);
$html = app(\App\Domain\Media\MarkdownMediaEmbed::class)->hydrateHtml($html);
$html = Purifier::clean($html, 'article');
return $this->applyTocAnchors($html);
+11
View File
@@ -117,6 +117,17 @@ class ArticleCoverService
{
$content = (string) $article->content;
if (preg_match('/!\[[^\]]*\]\(media:(\d+)\)/i', $content, $matches) === 1) {
$media = app(MarkdownMediaEmbed::class)->find((int) $matches[1]);
if ($media !== null && app(MarkdownMediaEmbed::class)->isImage($media) && filled($media->url)) {
return [
'disk' => 'url',
'path' => (string) $media->url,
'source' => self::SOURCE_CONTENT_IMAGE,
];
}
}
if (preg_match('/!\[[^\]]*\]\(attach:(\d+)\)/i', $content, $matches) === 1
|| preg_match('/\[attach=(\d+)\]/i', $content, $matches) === 1) {
$attachment = Attachment::query()->find((int) $matches[1]);
+131
View File
@@ -0,0 +1,131 @@
<?php
declare(strict_types=1);
namespace App\Domain\Media;
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]
*
* Legacy sablog tokens ([attach=ID] / attach:ID) stay on the Attachment table.
*/
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)) {
$alt = e($this->label($media, true));
return '<img src="'.e($url).'" alt="'.$alt.'" />';
}
$label = e($media ? $this->label($media, false) : 'file-'.$id);
return '<a href="'.e($url).'">'.$label.'</a>';
},
$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);
}
}