Fix rich editor image preview URLs

This commit is contained in:
2026-04-20 13:41:27 +08:00
parent 6acd0ccad0
commit 0b35e54fe1
4 changed files with 85 additions and 15 deletions

View File

@@ -30,6 +30,35 @@ class GuidePage extends Model
return route('guides.pages.show', $this->id);
}
public function getNormalizedContentAttribute(): string
{
return static::normalizeRichTextContent($this->content);
}
public static function normalizeRichTextContent(?string $content): string
{
if (blank($content)) {
return '';
}
$content = preg_replace_callback(
'~(?:https?:)?//[^"\'\s<>()]+(?<path>/storage/guide-pages/[^"\'\s<>()]*)~i',
static fn (array $matches): string => $matches['path'],
$content,
) ?? $content;
return preg_replace(
'~(?<=["\'])storage/guide-pages/~i',
'/storage/guide-pages/',
$content,
) ?? $content;
}
public static function uploadedAttachmentUrl(string $path): string
{
return '/storage/'.ltrim($path, '/');
}
public function guide()
{
return $this->belongsTo(Guide::class);
@@ -60,7 +89,6 @@ class GuidePage extends Model
public function isEntry(): bool
{
return !$this->incomingEdges()->exists();
return ! $this->incomingEdges()->exists();
}
}