From 29371f4adc1b40ee08b8ee4c4626f44da162112a Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Tue, 13 Jun 2023 14:26:01 +0800 Subject: [PATCH] feat: img loading="lazy" --- docs/.vuepress/config.js | 7 ++++-- .../markdown/markdowm-it-image-attrs.js | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 docs/.vuepress/markdown/markdowm-it-image-attrs.js diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index bee651e38..2134203cb 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -24,8 +24,8 @@ const config = { }], ['script', { src: `/js/redirect.js?${Date.now()}&v=${Date.now()}&version=${Date.now()}` }], ['script', { src: 'https://hm.baidu.com/hm.js?fe3b7a223fc08c795f0f4b6350703e6f' }], - ['script', { src: 'miku-delivery-1.1.0.js' }], - ['script', { src: `/js/miku.js?${Date.now()}&v=${Date.now()}&version=${Date.now()}` }] + ['script', { src: 'miku-delivery-1.1.0.js' }], + ['script', { src: `/js/miku.js?${Date.now()}&v=${Date.now()}&version=${Date.now()}` }] ], locales: { '/': { @@ -80,6 +80,9 @@ const config = { .end() .plugin('normallize-link') .use(require('./markdown/normallizeLink')) + .end() + .plugin('img-attrs') + .use(require('./markdown/markdowm-it-image-attrs')) } }, chainWebpack (config, isServer) { diff --git a/docs/.vuepress/markdown/markdowm-it-image-attrs.js b/docs/.vuepress/markdown/markdowm-it-image-attrs.js new file mode 100644 index 000000000..006ac5303 --- /dev/null +++ b/docs/.vuepress/markdown/markdowm-it-image-attrs.js @@ -0,0 +1,22 @@ +module.exports = function (md, opts) { + md.core.ruler.after('inline', 'image-attrs', function (state) { + state.tokens.forEach(function (blockToken) { + if (blockToken.type === 'inline' && blockToken.children) { + blockToken.children.forEach(function (token) { + const type = token.type; + if (type === 'image') { + const src = token.attrs.find(attr => attr[0] === 'src'); + if ( + src && + src[1].indexOf('qiniu-web-assets.dcloud.net.cn') > -1 && + token.attrs.map(attr => attr[0]).indexOf('loading') < 0 + ) { + token.attrs.push(['loading', 'lazy']); + } + } + }); + } + }); + return false; + }); +}; -- GitLab