feat: use xpier media library for uploads and markdown embeds
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:
@@ -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]);
|
||||
|
||||
@@ -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:  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.')';
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user