markdowm-it-image-attrs.js 652 字节
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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;
	});
};