\n";
foreach ($imgMatches as $m) {
$src = trim($m['src']);
$alt = trim($m['alt']);
$title = trim($m['title'] ?? '');
$rawCaption = trim($m['caption'] ?? '');
$caption = '';
if ($rawCaption !== '') {
$caption = trim(preg_replace('/^\s*[*_](.*?)[*_]\s*$/s', '$1', $rawCaption));
} elseif ($title !== '') {
$caption = $title;
}
$lightboxCaption = $caption !== '' ? $caption : $alt;
$html .= '
' . "\n";
$html .= ' ' . "\n";
$html .= ' ' . "\n";
if ($caption !== '') {
$html .= ' ' . htmlspecialchars($caption, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '' . "\n";
}
$html .= ' ' . "\n";
}
$html .= "
\n";
return "\n\n" . $html . "\n\n";
}, $markdown) ?? $markdown;
}
function protect_fenced_code_blocks(string $markdown, array &$blocks): string
{
$blocks = [];
$index = 0;
$patterns = [
'/```[\s\S]*?```/',
'/~~~[\s\S]*?~~~/',
];
foreach ($patterns as $pattern) {
$markdown = preg_replace_callback($pattern, function (array $matches) use (&$blocks, &$index): string {
$token = '__PUREBLOG_CODE_BLOCK_' . $index . '__';
$blocks[$token] = $matches[0];
$index++;
return $token;
}, $markdown) ?? $markdown;
}
return $markdown;
}
function restore_fenced_code_blocks(string $markdown, array $blocks): string
{
if (!$blocks) {
return $markdown;
}
return strtr($markdown, $blocks);
}
function protect_inline_code_spans(string $markdown, array &$spans): string
{
$spans = [];
$index = 0;
return preg_replace_callback('/`[^`\n]*`/', function (array $matches) use (&$spans, &$index): string {
$token = '__PUREBLOG_INLINE_CODE_' . $index . '__';
$spans[$token] = $matches[0];
$index++;
return $token;
}, $markdown) ?? $markdown;
}
function restore_inline_code_spans(string $markdown, array $spans): string
{
if (!$spans) {
return $markdown;
}
return strtr($markdown, $spans);
}
function render_global_shortcodes(string $markdown, array $context = []): string
{
static $siteEmail = null;
if ($siteEmail === null) {
$config = load_config();
$siteEmail = trim((string) ($config['site_email'] ?? ''));
}
$postTitle = trim((string) ($context['post_title'] ?? ''));
$pageTitle = trim((string) ($context['page_title'] ?? ''));
$contentTitle = trim((string) ($context['content_title'] ?? ($postTitle !== '' ? $postTitle : $pageTitle)));
$shortcodes = [
'site_email' => $siteEmail,
'site.email' => $siteEmail,
'post_title' => $postTitle,
'page_title' => $pageTitle,
'content_title' => $contentTitle,
];
return preg_replace_callback('/\{\{\s*([a-zA-Z0-9_.]+)\s*\}\}/', function (array $matches) use ($shortcodes): string {
$key = $matches[1];
if (!array_key_exists($key, $shortcodes)) {
return $matches[0];
}
return (string) $shortcodes[$key];
}, $markdown) ?? $markdown;
}
function filter_content(string $markdown, array $context = []): string
{
// Do not process loop syntax inside fenced code examples.
$codeBlocks = [];
$markdown = protect_fenced_code_blocks($markdown, $codeBlocks);
$inlineCodeSpans = [];
$markdown = protect_inline_code_spans($markdown, $inlineCodeSpans);
$markdown = render_global_shortcodes($markdown, $context);
$markdown = render_any_data_loops($markdown);
$markdown = render_gallery_blocks($markdown);
$markdown = apply_filter('on_filter_content', $markdown);
$markdown = restore_inline_code_spans($markdown, $inlineCodeSpans);
return restore_fenced_code_blocks($markdown, $codeBlocks);
}
function get_excerpt(string $markdown, int $length = 200): string
{
$parts = explode('', $markdown, 2);
$excerpt = $parts[0];
if (mb_strlen($excerpt) > $length * 6) {
$excerpt = mb_substr($excerpt, 0, $length * 6);
}
$excerpt = preg_replace('/```.*?```/s', ' ', $excerpt) ?? $excerpt;
$excerpt = preg_replace('/`[^`]*`/', ' ', $excerpt) ?? $excerpt;
$excerpt = preg_replace('/:::\s*gallery/i', ' ', $excerpt) ?? $excerpt;
$excerpt = preg_replace('/:::/', ' ', $excerpt) ?? $excerpt;
$excerpt = preg_replace('/!\[[^\]]*\]\([^)]+\)/', ' ', $excerpt) ?? $excerpt;
$excerpt = preg_replace('/\[([^\]]*)\]\([^)]+\)/', '$1', $excerpt) ?? $excerpt;
$excerpt = preg_replace('/^[ \t]*[-*>]+[ \t]*/m', ' ', $excerpt) ?? $excerpt; // list markers / blockquotes
$excerpt = preg_replace('/^[ \t]*#{1,6}[ \t]+/m', ' ', $excerpt) ?? $excerpt; // ATX headings
$excerpt = preg_replace('/^[-*_]{2,}[ \t]*$/m', ' ', $excerpt) ?? $excerpt; // setext headers / HR
$excerpt = preg_replace('/[*_~]+/', '', $excerpt) ?? $excerpt; // inline emphasis
$excerpt = strip_tags($excerpt);
$excerpt = preg_replace('/\s+/', ' ', $excerpt) ?? $excerpt;
$excerpt = trim($excerpt);
if (mb_strlen($excerpt) > $length) {
return rtrim(mb_substr($excerpt, 0, $length)) . '...';
}
return $excerpt;
}
function content_has_h1(string $content): bool
{
if (trim($content) === '') {
return false;
}
$cleaned = preg_replace('/```.*?```/s', '', $content) ?? $content;
return (bool) preg_match('/(^|\n)(#\s+[^\n]+|[^\n]+\r?\n={3,}|<\s*h1(\s+[^>]*)?>)/i', $cleaned);
}
// ---------------------------------------------------------------------------
// Tag and layout helpers
// ---------------------------------------------------------------------------
function normalize_tag(string $tag): string
{
$tag = trim($tag);
if (function_exists('mb_strtolower')) {
$tag = mb_strtolower($tag, 'UTF-8');
} else {
$tag = strtolower($tag);
}
// Transliterate common diacritics to ASCII equivalents
$tag = strtr($tag, [
'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss',
'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'å' => 'a',
'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e',
'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o',
'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ý' => 'y',
'ÿ' => 'y',
]);
// Retain letters, numbers, emojis, pictographs, variation selectors, and joiners
$tag = preg_replace('/[^\p{L}\p{N}\p{So}\p{M}\x{1F000}-\x{1FAFF}\x{2600}-\x{27BF}\x{200D}\x{FE0E}\x{FE0F}\s_-]/u', '', $tag) ?? '';
$tag = preg_replace('/[\s_-]+/u', '-', $tag) ?? '';
return trim($tag, '-');
}
function render_tag_links(array $tags): string
{
$tags = array_values(array_filter(array_map('trim', $tags)));
if (!$tags) {
return '';
}
$links = [];
foreach ($tags as $tag) {
$slug = normalize_tag($tag);
$links[] = '