Certainly! To auto-embed another post’s link with its title and excerpt into the current post body (like WordPress embeds), you’ll want to:
- Detect a pattern in the body, e.g. if a post URL is pasted or referenced (
/posts/123). - When displaying the post (not when writing/editing), parse the content server-side or client-side.
- Replace detected post links with an HTML snippet containing the title and excerpt of the referenced post.
Assumption: You're using Laravel with a Post model, and rendering post body as HTML.
1. Detect and Replace URLs on the Server
Create a helper to process the post body:
use App\Models\Post;
function parsePostEmbeds($body)
{
// Pattern for your post URLs (adjust as needed)
$pattern = '/\/posts\/(\d+)/';
return preg_replace_callback($pattern, function ($matches) {
$postId = $matches[1];
$post = Post::find($postId);
if (!$post) return $matches[0]; // Leave link as-is if post not found
// Generate your embed HTML
$embed = view('partials.post-embed', ['post' => $post])->render();
return $embed;
}, $body);
}
Example Blade partial (resources/views/partials/post-embed.blade.php):
<div class="post-embed">
<a href="{{ route('posts.show', $post) }}">
<strong>{{ $post->title }}</strong>
</a>
<p>{{ Str::limit(strip_tags($post->body), 100) }}</p>
</div>
2. Use the Helper When Rendering
In your show route/controller:
// In your controller
$parsedBody = parsePostEmbeds($post->body);
return view('posts.show', ['post' => $post, 'parsedBody' => $parsedBody]);
In your blade:
{!! $parsedBody !!}
3. Optional: TipTap Custom Extension
If you want to auto-convert URLs while editing in TipTap, you would write a custom TipTap extension/plugin in JavaScript. But for most Laravel setups, server-side parsing as above works well and is robust (and protects against broken/missing posts).
Summary:
- Clients just paste the post URL.
- When rendering, scan for
/posts/{id}links and replace with embed box. - Keeps the writing flow simple, and presents consistent embeds.
Let me know if you need a TipTap extension example for the editor itself!