This is my plugin for the file button:
// From: https://forum.getkirby.com/t/srcset-and-the-kirby-image-tag-what-about-file-names-and-sizes/17061/8
$originalTag = Kirby\Text\KirbyTag::$types['image'];
Kirby::plugin('chinochano/image', [
'tags' => [
'image' => [
'attr' => array_merge(
$originalTag['attr'],
[
'srcset',
'sizes'
]
),
'html' => function($tag) use ($originalTag) {
$file = $tag->file($tag->value());
$srcset = $tag->srcset;
$srcset = option('thumbs.srcsets.' . $tag->srcset ) ?: option('thumbs.srcsets.default');
$sizes = option('srcset-sizes.' . $tag->sizes ) ?: option('srcset-sizes.default');
$result = $originalTag['html']($tag);
if (! $file === true || is_null($srcset) === true || is_null($sizes) === true) {
return $result;
}
$pattern = '/<img.*?>/i';
// src image with sizes and quality (if any) in filename
$src = $file->thumb('pixel')->url();
// build a new image tag with the srcset and sizes
$image = Html::img($src, [
'width' => $tag->width,
'height' => $tag->height,
'class' => $tag->imgclass . ' lazyload',
'title' => $tag->title,
'alt' => $tag->alt ?? ' ',
'data-src' => $file->thumb('default')->url(),
'data-srcset' => $file->srcset($srcset),
'data-sizes' => $sizes
]);
// replace the old image tag
$result = preg_replace($pattern, $image , $result);
return $result;
}
]
]
]);
EDIT:
To add other relevant pieces of code…
The concerning piece of the blueprint:
fields:
textbody:
label: Body
type: markdown
size: large
font:
family: sans-serif
required: true
maxlength: 2000
buttons:
- bold
- italic
- link
- file
- video
files: page.images
uploads: false
The markdown plugin is in /site/plugins/markdown-field/
.
I’ve added the custom button to the markdown fields in /site/plugins/markdown-field-video/
(chose that folder name to make sure it would be loaded after the markdown field plugin, as explained on the documentation).
In there I have two files:
index.php
with the standard:
Kirby::plugin('community/markdown-field-video', []);
And index.js
:
markdownEditor.button('video', {
extends: 'default',
data: function() {
return {
label: 'This is a video button',
icon: 'video',
type: 'video'
}
},
methods: {
action() {
this.insert('(video: '+ this.selection +')')
}
}
});