512f1ce9b2
Two new shortcodes for immersive storytelling: - [full-bleed image="" caption="" credit=""] — viewport-wide image, max 80vh - [image-caption image="" caption="" credit="" width="column|full|bleed"] — photo at configurable width with caption Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WPJztrVGbwic2xTG7G9fjM
38 lines
1.6 KiB
PHP
38 lines
1.6 KiB
PHP
<?php
|
|
namespace Grav\Plugin\Shortcodes;
|
|
|
|
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
|
|
|
class ImageCaptionShortcode extends Shortcode
|
|
{
|
|
public function init(): void
|
|
{
|
|
$this->shortcode->getHandlers()->add('image-caption', function (ShortcodeInterface $sc) {
|
|
$plugin = $this->grav['plugins']->getPlugin('story-blocks');
|
|
$page = $plugin ? $plugin->getCurrentPage() : null;
|
|
|
|
$imageName = htmlspecialchars($sc->getParameter('image', ''), ENT_QUOTES);
|
|
$alt = htmlspecialchars($sc->getParameter('alt', ''), ENT_QUOTES);
|
|
$caption = htmlspecialchars($sc->getParameter('caption', ''), ENT_QUOTES);
|
|
$credit = htmlspecialchars($sc->getParameter('credit', ''), ENT_QUOTES);
|
|
$width = $sc->getParameter('width', 'column');
|
|
if (!in_array($width, ['column', 'full', 'bleed'])) $width = 'column';
|
|
$imageUrl = ($page && $imageName) ? $page->url() . '/' . $imageName : $imageName;
|
|
|
|
$widthClass = $width !== 'column' ? ' img-caption--' . $width : '';
|
|
$captionHtml = '';
|
|
if ($caption || $credit) {
|
|
$creditHtml = $credit ? '<span class="img-caption__credit"> · ' . $credit . '</span>' : '';
|
|
$captionHtml = '<figcaption class="img-caption__text">' . $caption . $creditHtml . '</figcaption>';
|
|
}
|
|
|
|
return <<<HTML
|
|
<figure class="img-caption{$widthClass}">
|
|
<img src="{$imageUrl}" alt="{$alt}" class="img-caption__img" loading="lazy">
|
|
{$captionHtml}
|
|
</figure>
|
|
HTML;
|
|
});
|
|
}
|
|
}
|