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-05-23 07:16:02 +00:00
|
|
|
const 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-25 07:53:29 +00:00
|
|
|
const cloudinary = 'https://res.cloudinary.com/' + user +
|
2019-08-25 09:39:26 +00:00
|
|
|
'/image/upload/f_auto,q_auto'
|
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-25 09:39:26 +00:00
|
|
|
const img = `<img
|
|
|
|
srcset="${cloudinary}/c_limit,w_320/${legacy} 320w,
|
|
|
|
${cloudinary}/c_limit,w_468/${legacy} 468w,
|
|
|
|
${cloudinary}/c_limit,w_768/${legacy} 768w,
|
|
|
|
${cloudinary}/${legacy} 800w"
|
|
|
|
sizes="(max-width: 320px) 320px,
|
|
|
|
(max-width: 468px) 468px,
|
|
|
|
(max-width: 768px) 768px,
|
|
|
|
800px">
|
|
|
|
src="${cloudinary}w_768/${legacy}"
|
|
|
|
alt="${alt}">`
|
|
|
|
|
2019-08-25 06:37:58 +00:00
|
|
|
if (fileName.endsWith('.png') || fileName.endsWith('.webp')) {
|
|
|
|
return `<a href="https://res.cloudinary.com/curben/${fileName}">
|
|
|
|
<picture>
|
|
|
|
<source type="image/webp"
|
2019-08-25 09:39:26 +00:00
|
|
|
srcset="${cloudinary}/c_limit,w_320/${modern} 320w,
|
|
|
|
${cloudinary}/c_limit,w_468/${modern} 468w,
|
|
|
|
${cloudinary}/c_limit,w_768/${modern} 768w,
|
|
|
|
${cloudinary}/${modern} 800w"
|
|
|
|
sizes="(max-width: 320px) 320px,
|
|
|
|
(max-width: 468px) 468px,
|
|
|
|
(max-width: 768px) 768px,
|
|
|
|
800px">
|
|
|
|
${img}
|
2019-08-25 06:37:58 +00:00
|
|
|
</picture></a>`
|
|
|
|
} else {
|
|
|
|
return `<a href="https://res.cloudinary.com/curben/${fileName}">
|
2019-08-25 09:39:26 +00:00
|
|
|
${img}</a>`
|
2019-08-25 06:37:58 +00:00
|
|
|
}
|
2018-10-25 09:03:38 +00:00
|
|
|
})
|