blog/scripts/cloudinary.js

80 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-08-09 13:02:51 +00:00
'use strict'
/* global hexo */
2018-10-01 02:24:11 +00:00
/*
* Put {% cloudinary 'folder/filename.jpg' 'description' %} in your post.
* Change the username in 'user' variable
2018-10-01 02:24:11 +00:00
* More info:
* 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
hexo.extend.tag.register('cloudinary', (args) => {
const user = 'curben'
const fileName = args[0]
2019-07-10 11:47:17 +00:00
const alt = args[1] || ''
let modern = ''
let legacy = ''
const cloudinary = 'https://res.cloudinary.com/' + user +
'/image/upload/w_auto,f_auto,q_auto,c_scale/c_limit,'
if (fileName.endsWith('.png')) {
modern = fileName.replace(/\.png$/, '.webp')
legacy = fileName
} else if (fileName.endsWith('.webp')) {
modern = fileName
legacy = fileName.replace(/\.webp$/, '.gif')
}
if (fileName.endsWith('.png') || fileName.endsWith('.webp')) {
return `<a href="https://res.cloudinary.com/curben/${fileName}">
<picture>
<source type="image/webp"
2019-08-25 07:45:44 +00:00
srcset="${cloudinary}w_300/${modern} 300w,
${cloudinary}w_450/${modern} 450w,
${cloudinary}w_600/${modern} 600w,
${cloudinary}w_900/${modern} 900w,
${cloudinary}w_1200/${modern} 1200w,
${cloudinary}w_1500/${modern} 1500w"
sizes="(max-width: 300px) 280px,
(max-width: 450px) 430px,
(max-width: 600px) 580px,
(max-width: 900px) 880px,
(max-width: 1200px) 1180px,
1500px">
<img
srcset="${cloudinary}w_300/${legacy} 300w,
${cloudinary}w_450/${legacy} 450w,
${cloudinary}w_600/${legacy} 600w,
${cloudinary}w_900/${legacy} 900w,
${cloudinary}w_1200/${legacy} 1200w,
${cloudinary}w_1500/${legacy} 1500w"
sizes="(max-width: 300px) 280px,
(max-width: 450px) 430px,
2019-08-25 07:45:44 +00:00
(max-width: 600px) 580px,
(max-width: 900px) 880px,
(max-width: 1200px) 1180px,
1500px"
src="${cloudinary}w_600/${legacy}"
alt="${alt}">
</picture></a>`
} else {
return `<a href="https://res.cloudinary.com/curben/${fileName}">
2019-08-25 07:45:44 +00:00
<img
srcset="${cloudinary}w_300/${fileName} 300w,
${cloudinary}w_450/${fileName} 450w,
${cloudinary}w_600/${fileName} 600w,
${cloudinary}w_900/${fileName} 900w,
${cloudinary}w_1200/${fileName} 1200w,
${cloudinary}w_1500/${fileName} 1500w"
sizes="(max-width: 300px) 280px,
(max-width: 450px) 430px,
2019-08-25 07:45:44 +00:00
(max-width: 600px) 580px,
(max-width: 900px) 880px,
(max-width: 1200px) 1180px,
1500px"
src="${cloudinary}w_600/${fileName}"
alt="${alt}">
2019-08-25 07:45:44 +00:00
</a>`
}
})