2019-08-09 13:02:51 +00:00
|
|
|
'use strict'
|
|
|
|
/* global hexo */
|
|
|
|
|
2018-10-01 02:24:11 +00:00
|
|
|
/*
|
2019-05-23 07:16:02 +00:00
|
|
|
* Put {% cloudinary 'folder/filename.jpg' 'description' %} in your post.
|
2019-08-25 07:53:29 +00:00
|
|
|
* Change the username in 'user' variable
|
2018-10-01 02:24:11 +00:00
|
|
|
* More info:
|
2019-08-25 07:53:29 +00:00
|
|
|
* https://cloudinary.com/blog/responsive_images_with_srcset_sizes_and_cloudinary
|
2018-10-01 02:24:11 +00:00
|
|
|
*/
|
2019-08-25 07:45:44 +00:00
|
|
|
|
2019-05-23 07:16:02 +00:00
|
|
|
hexo.extend.tag.register('cloudinary', (args) => {
|
2019-08-25 07:53:29 +00:00
|
|
|
const user = 'curben'
|
2019-08-30 04:50:10 +00:00
|
|
|
let fileName = args[0]
|
2019-07-10 11:47:17 +00:00
|
|
|
const alt = args[1] || ''
|
2019-08-25 06:37:58 +00:00
|
|
|
let modern = ''
|
|
|
|
let legacy = ''
|
2019-08-30 03:31:44 +00:00
|
|
|
const link = 'https://cdn.statically.io/img/res.cloudinary.com/' + user
|
2019-05-23 07:16:02 +00:00
|
|
|
|
2019-08-25 06:37:58 +00:00
|
|
|
if (fileName.endsWith('.png')) {
|
|
|
|
modern = fileName.replace(/\.png$/, '.webp')
|
|
|
|
legacy = fileName
|
|
|
|
} else if (fileName.endsWith('.webp')) {
|
|
|
|
modern = fileName
|
|
|
|
legacy = fileName.replace(/\.webp$/, '.gif')
|
2019-08-25 09:39:26 +00:00
|
|
|
} else {
|
|
|
|
legacy = fileName
|
2019-08-25 06:37:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 04:50:10 +00:00
|
|
|
fileName += '?auto_format=false'
|
|
|
|
modern += '?auto_format=false'
|
|
|
|
legacy += '?auto_format=false'
|
2019-08-30 03:31:44 +00:00
|
|
|
|
2019-08-25 09:39:26 +00:00
|
|
|
const img = `<img
|
2019-08-30 04:50:10 +00:00
|
|
|
srcset="${link}/${legacy}&w=320 320w,
|
|
|
|
${link}/${legacy}&w=468 468w,
|
|
|
|
${link}/${legacy}&w=768 768w,
|
|
|
|
${link}/${legacy} 800w"
|
2019-08-25 09:39:26 +00:00
|
|
|
sizes="(max-width: 320px) 320px,
|
|
|
|
(max-width: 468px) 468px,
|
|
|
|
(max-width: 768px) 768px,
|
2019-08-25 23:44:35 +00:00
|
|
|
800px"
|
2019-08-30 04:50:10 +00:00
|
|
|
src="${link}/${legacy}"
|
2019-08-25 23:56:02 +00:00
|
|
|
alt="${alt}" loading="lazy">`
|
2019-08-25 09:39:26 +00:00
|
|
|
|
2019-08-25 06:37:58 +00:00
|
|
|
if (fileName.endsWith('.png') || fileName.endsWith('.webp')) {
|
2019-08-30 04:50:10 +00:00
|
|
|
return `<a href="${link}/${fileName}">
|
|
|
|
<picture><noscript>
|
2019-08-25 06:37:58 +00:00
|
|
|
<source type="image/webp"
|
2019-08-30 04:50:10 +00:00
|
|
|
srcset="${link}/${modern}&w=320 320w,
|
|
|
|
${link}/${modern}&w=468 468w,
|
|
|
|
${link}/${modern}&w=768 768w,
|
|
|
|
${link}/${modern} 800w"
|
2019-08-25 09:39:26 +00:00
|
|
|
sizes="(max-width: 320px) 320px,
|
|
|
|
(max-width: 468px) 468px,
|
|
|
|
(max-width: 768px) 768px,
|
|
|
|
800px">
|
|
|
|
${img}
|
2019-08-30 04:50:10 +00:00
|
|
|
</noscript></picture></a>`
|
2019-08-25 06:37:58 +00:00
|
|
|
} else {
|
2019-08-30 04:50:10 +00:00
|
|
|
return `<a href="${link}/${fileName}"><noscript>${img}</noscript></a>`
|
2019-08-25 06:37:58 +00:00
|
|
|
}
|
2018-10-25 09:03:38 +00:00
|
|
|
})
|