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
|
2021-01-12 03:32:41 +00:00
|
|
|
* Image is resized on-the-fly using Statically (https://statically.io/)
|
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
|
|
|
|
2022-03-05 08:06:43 +00:00
|
|
|
const { join } = require('path').posix
|
|
|
|
|
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
|
2021-01-12 03:32:41 +00:00
|
|
|
|
2021-01-15 01:30:27 +00:00
|
|
|
if (href.endsWith('.svg')) return `<img class="svg" src="${href}" alt="${alt}" title="${title}">`
|
2019-05-23 07:16:02 +00:00
|
|
|
|
2020-03-08 09:39:14 +00:00
|
|
|
// embed external image
|
2023-02-25 06:41:45 +00:00
|
|
|
if (href.startsWith('http')) return `<img src="${href}" alt="${alt}" title="${title}">`
|
2019-08-25 06:37:58 +00:00
|
|
|
|
2024-10-04 10:46:57 +00:00
|
|
|
const fLink = (path, width = '') => {
|
|
|
|
const url = new URL(join('images', width, path), 'http://example.com/')
|
2021-01-12 03:32:41 +00:00
|
|
|
|
2024-10-04 10:46:57 +00:00
|
|
|
return url.pathname
|
2021-01-12 03:32:41 +00:00
|
|
|
}
|
2019-08-30 03:31:44 +00:00
|
|
|
|
2022-06-14 07:29:25 +00:00
|
|
|
return `<a href="${join('/img', href)}">` +
|
2024-10-04 10:46:57 +00:00
|
|
|
`<img srcset="${fLink(href, '320')} 320w,` +
|
|
|
|
`${fLink(href, '468')} 468w,` +
|
|
|
|
`${fLink(href, '768')} 768w,` +
|
2021-01-12 03:32:41 +00:00
|
|
|
`${fLink(href)} 800w"` +
|
2019-12-26 06:14:26 +00:00
|
|
|
' sizes="(max-width: 320px) 320px,' +
|
|
|
|
'(max-width: 468px) 468px,' +
|
|
|
|
'(max-width: 768px) 768px,' +
|
|
|
|
'800px"' +
|
2021-01-12 03:32:41 +00:00
|
|
|
` src="${fLink(href)}" title="${title}" alt="${alt}" loading="lazy"></a>`
|
2019-08-25 06:37:58 +00:00
|
|
|
}
|
2018-10-25 09:03:38 +00:00
|
|
|
})
|