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>
132 lines
3.6 KiB
PHP
132 lines
3.6 KiB
PHP
<?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:  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.')';
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|