img-add-attrs.js 1.8 KB
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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
module.exports = function (md, opts) {
	md.core.ruler.after('inline', 'image-attrs', function (state) {
		state.tokens.forEach(function (blockToken) {
			if (blockToken.type === 'html_block') {
				replaceHTML(blockToken, addLoadingAttr, state.env);
			}
			if (blockToken.type === 'inline' && blockToken.children) {
				blockToken.children.forEach(function (token) {
					const type = token.type;
					if (type === 'html_inline') {
						replaceHTML(token, addLoadingAttr, state.env);
					}
					if (type === 'image') {
						const src = token.attrGet('src');
						if (!token.attrGet('loading') && matchSrc(src)) {
							token.attrPush(['loading', 'lazy']);
						}
					}
				});
			}
		});
		return false;
	});
};

/**
 *
 * @param {Record<string,any>} attribs
 */
function addLoadingAttr(attribs) {
	if (matchSrc(attribs.src) && !attribs.loading) {
		attribs.loading = 'lazy';
	}
}

/**
 *
 * @param {string} [src] img src
 * @returns
 */
function matchSrc(src) {
	return typeof src === 'string'
D
DCloud_LXH 已提交
43
		? (src.indexOf('qiniu-web-assets.dcloud.net.cn') > -1 || src.indexOf('web-ext-storage.dcloud.net.cn') > -1)
D
DCloud_LXH 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
		: false;
}

function replaceNodes(nodes, replace, env, token) {
	if (!nodes) return;
	for (let i = 0; i < nodes.length; i++) {
		const node = nodes[i];
		if (node.attribs) {
			if (node.name === 'img' && node.attribs.src) {
				replace(node.attribs);
			}
		}
		replaceNodes(node.children, replace, env, token);
	}
}

function replaceHTML(token, replace, env) {
	const htmlparser = require('htmlparser2');
	const serializer = require('dom-serializer');
	const dom = new htmlparser.parseDocument(token.content, {
		recognizeCDATA: true,
		recognizeSelfClosing: true,
	});
	replaceNodes(dom.children, replace, env, token);
	token.content = serializer.render(dom, { encodeEntities: 'utf8' });
}