2019-08-09 13:02:51 +00:00
|
|
|
'use strict'
|
|
|
|
/* global hexo */
|
|
|
|
|
2018-10-01 02:24:11 +00:00
|
|
|
/*
|
2019-09-04 02:46:35 +00:00
|
|
|
* Embed an image with responsive images in a post
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images
|
|
|
|
* Image is resized on-the-fly using Statically Imgpx
|
|
|
|
* https://statically.io/imgpx
|
2019-12-26 06:14:26 +00:00
|
|
|
* Usage: ![alt](/path/to/img "title")
|
2018-10-01 02:24:11 +00:00
|
|
|
*/
|
2019-08-25 07:45:44 +00:00
|
|
|
|
2019-12-26 06:14:26 +00:00
|
|
|
hexo.extend.filter.register('marked:renderer', (renderer) => {
|
|
|
|
renderer.image = (href, title, alt) => {
|
|
|
|
if (!alt) alt = ''
|
|
|
|
if (!title) title = alt
|
|
|
|
let modern = href
|
|
|
|
let legacy = href
|
|
|
|
// /img/ is a reverse proxy to Statically CDN
|
|
|
|
// See source/_redirects
|
|
|
|
const link = '/img/'
|
2019-05-23 07:16:02 +00:00
|
|
|
|
2019-12-26 06:14:26 +00:00
|
|
|
if (href.endsWith('.png') || href.endsWith('.jpg')) {
|
|
|
|
modern = href.concat('?format=webp')
|
|
|
|
} else if (href.endsWith('.webp')) {
|
|
|
|
// Statically has yet to support animated webp
|
|
|
|
// https://github.com/marsble/statically/issues/36
|
|
|
|
// modern = href.concat('?auto_format=false')
|
|
|
|
modern = href.replace(/\.webp$/, '.gif')
|
|
|
|
legacy = href.replace(/\.webp$/, '.gif')
|
|
|
|
}
|
2019-08-25 06:37:58 +00:00
|
|
|
|
2019-12-26 06:14:26 +00:00
|
|
|
const modernLink = link + modern
|
|
|
|
const legacyLink = link + legacy
|
2019-08-30 03:31:44 +00:00
|
|
|
|
2019-12-26 06:14:26 +00:00
|
|
|
const img = `<img srcset="${legacyLink}&w=320 320w,` +
|
|
|
|
`${legacyLink}&w=468 468w,` +
|
|
|
|
`${legacyLink}&w=768 768w,` +
|
|
|
|
`${legacyLink} 800w"` +
|
|
|
|
' sizes="(max-width: 320px) 320px,' +
|
|
|
|
'(max-width: 468px) 468px,' +
|
|
|
|
'(max-width: 768px) 768px,' +
|
|
|
|
'800px"' +
|
|
|
|
` src="${legacyLink}"` +
|
|
|
|
` title="${title}" alt="${alt}" loading="lazy">`
|
2019-08-25 09:39:26 +00:00
|
|
|
|
2019-12-26 06:14:26 +00:00
|
|
|
if (href.endsWith('.png') || href.endsWith('.webp')) {
|
|
|
|
return `<a href="${legacyLink}">` +
|
|
|
|
'<picture>' +
|
|
|
|
'<source type="image/webp"' +
|
|
|
|
` srcset="${modernLink}&w=320 320w,` +
|
|
|
|
`${modernLink}&w=468 468w,` +
|
|
|
|
`${modernLink}&w=768 768w,` +
|
|
|
|
`${modernLink} 800w"` +
|
|
|
|
' sizes="(max-width: 320px) 320px,' +
|
|
|
|
'(max-width: 468px) 468px,' +
|
|
|
|
'(max-width: 768px) 768px,' +
|
|
|
|
'800px">' +
|
|
|
|
`${img}` +
|
|
|
|
'</picture></a>'
|
|
|
|
} else {
|
|
|
|
return `<a href="${legacyLink}">${img}</a>`
|
|
|
|
}
|
2019-08-25 06:37:58 +00:00
|
|
|
}
|
2018-10-25 09:03:38 +00:00
|
|
|
})
|