Add custom image size syntax for Markdown files

Support ![alt](url){:width="300" height="200"} syntax in .md files
to set image dimensions via attributes inside {: :}
This commit is contained in:
2026-07-21 13:16:33 +02:00
parent a048056b6b
commit e19433a389
+16
View File
@@ -564,12 +564,28 @@ class CodePressCMS {
$environment->addExtension(new \League\CommonMark\Extension\Table\TableExtension());
$environment->addExtension(new \League\CommonMark\Extension\TaskList\TaskListExtension());
// Handle custom image size syntax: ![alt](url){:width="300" height="200"}
$imagePlaceholders = [];
$content = preg_replace_callback('/!\[([^\]]*)\]\(([^)]+)\)\{:([^}]+)\}/', function ($m) use (&$imagePlaceholders) {
$index = count($imagePlaceholders);
$alt = htmlspecialchars($m[1], ENT_QUOTES, 'UTF-8');
$url = htmlspecialchars($m[2], ENT_QUOTES, 'UTF-8');
$attrs = trim($m[3]);
$imagePlaceholders[$index] = '<img src="' . $url . '" alt="' . $alt . '" ' . $attrs . '>';
return '@@IMG_' . $index . '@@';
}, $content);
// Create converter
$converter = new \League\CommonMark\MarkdownConverter($environment);
// Convert to HTML
$body = $converter->convert($content)->getContent();
// Restore custom image tags
foreach ($imagePlaceholders as $index => $imgTag) {
$body = str_replace('@@IMG_' . $index . '@@', $imgTag, $body);
}
// Extract clean filename for title (without language prefix and extension)
$filename = basename($actualFilePath);
$cleanName = $this->formatDisplayName($filename);