@extends('layouts.modern')
@php
$locale = app()->getLocale();
// Set Carbon locale for date formatting
\Carbon\Carbon::setLocale($locale);
$metaTitleField = $locale === 'tr' ? 'meta_title' : 'meta_title_' . $locale;
$metaDescField = $locale === 'tr' ? 'meta_description' : 'meta_description_' . $locale;
$metaKeywordsField = $locale === 'tr' ? 'meta_keywords' : 'meta_keywords_' . $locale;
$seoTitle = ($post->$metaTitleField ?? $post->meta_title ?? null) ?: $post->localized_title . ' - FTurkey.com';
$seoDescription = ($post->$metaDescField ?? $post->meta_description ?? null) ?: $post->localized_excerpt;
$seoKeywords = $post->$metaKeywordsField ?? $post->meta_keywords ?? null;
// Featured Image - FIX .webp issues
$featuredImage = $post->featured_image;
if ($featuredImage) {
// Remove .webp extension if present
$featuredImage = preg_replace('/\.webp(\?[^"\']*)?$/i', '.jpg$1', $featuredImage);
// Normalize path
if (!str_starts_with($featuredImage, 'http')) {
$featuredImage = ltrim($featuredImage, '/');
if (!str_starts_with($featuredImage, 'images/')) {
if (!str_contains($featuredImage, '/')) {
$featuredImage = 'images/blog/' . $featuredImage;
} else {
$featuredImage = 'images/blog/' . basename($featuredImage);
}
}
// Check if file exists
if (!file_exists(public_path($featuredImage))) {
$featuredImage = 'images/og-blog.jpg';
}
}
$seoImage = str_starts_with($featuredImage, 'http') ? $featuredImage : asset($featuredImage);
} else {
$seoImage = asset('images/og-blog.jpg');
$featuredImage = 'images/og-blog.jpg';
}
$seoType = 'article';
// Process content - REMOVE ALL .webp and picture/source tags
$rawContent = $post->localized_content;
// STEP 1: Remove ALL picture and source elements completely
$rawContent = preg_replace('/]*>[\s\S]*?<\/picture>/i', '', $rawContent);
$rawContent = preg_replace('/]*>/i', '', $rawContent);
// STEP 2: Replace ALL .webp with .jpg (ultra aggressive)
$rawContent = str_ireplace('.webp', '.jpg', $rawContent);
$rawContent = preg_replace('/\.webp(\?[^"\'>\s<]*)?/i', '.jpg$1', $rawContent);
// STEP 3: Fix all img tags - use og-blog.jpg as placeholder (it exists)
$placeholderImage = asset('images/og-blog.jpg');
$rawContent = preg_replace_callback(
'/]*)>/i',
function($matches) use ($placeholderImage) {
$tag = $matches[0];
// Remove .webp from tag
$tag = str_ireplace('.webp', '.jpg', $tag);
// Add onerror if not exists
if (strpos($tag, 'onerror') === false) {
$tag = str_replace('>', ' onerror="this.onerror=null;this.src=\'' . $placeholderImage . '\'">', $tag);
}
return $tag;
},
$rawContent
);
// STEP 4: Extract headings for TOC
preg_match_all('/]*>(.*?)<\/h[2-4]>/i', $rawContent, $headings);
$tocItems = [];
if (!empty($headings[0])) {
foreach ($headings[0] as $index => $heading) {
$level = $headings[1][$index];
$text = strip_tags($headings[2][$index]);
$slug = \Illuminate\Support\Str::slug($text) . '-' . $index;
$tocItems[] = ['level' => $level, 'text' => $text, 'slug' => $slug];
// Add ID to heading
$rawContent = preg_replace(
'/]*>' . preg_quote($headings[2][$index], '/') . '<\/h' . $level . '>/i',
'' . $headings[2][$index] . '',
$rawContent,
1
);
}
}
// STEP 5: Insert featured image in the middle of content for SEO
// Split content by paragraphs and insert image after middle paragraph
$paragraphs = preg_split('/(<\/p>|<\/h[1-6]>)/i', $rawContent, -1, PREG_SPLIT_DELIM_CAPTURE);
$totalParagraphs = count($paragraphs);
$middleIndex = floor($totalParagraphs / 2);
// Find a good insertion point (after a closing
or tag)
$insertionPoint = 0;
$paragraphCount = 0;
for ($i = 0; $i < $totalParagraphs; $i++) {
if (preg_match('/<\/p>|<\/h[1-6]>/i', $paragraphs[$i])) {
$paragraphCount++;
if ($paragraphCount >= $middleIndex && $paragraphCount > 2) {
$insertionPoint = $i + 1;
break;
}
}
}
// If we found a good insertion point, insert the featured image
if ($insertionPoint > 0 && $insertionPoint < $totalParagraphs) {
$featuredImageHtml = '