Uses picture sets generated by metalsmith-picset-generate to give browsers choice
Add to your pipeline like
npm i metalsmith-picset-handlebars-helper --save
const picsetHandlearsHelper = require('metalsmith-picset-handlebars-helper')
Metalsmith(__dirname)
...
.use(picsetHandlearsHelper())
...
Assume
/img/picset/anthony_80webp_90jpg_2000,1000,500w.jpg
as a 2000px wide 90% quality photoThen use the handlebars helper like {{{picset "anthony" 500}}}
to output something like
<picture>
<source type="image/webp"
srcset="img/srcsets/anthony-2000.webp 2000w, img/srcsets/anthony-1000.webp 1000w, img/srcsets/anthony-500.webp 500w" sizes="100vw" />
<img src="https://github.com/AnthonyAstige/metalsmith-picset-handlebars-helper/raw/master/img/srcsets/anthony-500.jpg"
srcset="img/srcsets/anthony-2000.jpg 2000w, img/srcsets/anthony-1000.jpg 1000w, img/srcsets/anthony-500.jpg 500w" sizes="100vw" />
</picture>
{
path: 'img/picset'
}
path
img/picset/
Helper use
Like {{{picset NAME DEFAULT_WIDTH SIZES ALT}}}
Generates <picture>
elements with:
srcset
parameters that include all files found following metalsmith-picset-generate's naming convention<source>
with .webp
type in srcset
<img>
with:
srcset
with file type of .jpg
, .png
, or .svg
as a fallbacksrc
at default width and same file type as it's srcsetsizes
attribute as specified duplicated on both the <source>
and <img ...>
tags.webp
is first choice<picture>
with inner <img src="https://github.com/AnthonyAstige/metalsmith-picset-handlebars-helper/raw/master/..">
fallback)