UNPKG

rehype-plugin-image-native-lazy-loading

Version:

![build](https://github.com/potato4d/rehype-plugin-image-native-lazy-loading/workflows/build/badge.svg) [![codecov](https://codecov.io/gh/potato4d/rehype-plugin-image-native-lazy-loading/branch/master/graph/badge.svg)](https://codecov.io/gh/potato4d/rehyp

27 lines (22 loc) 582 B
import { Processor, Transformer } from 'unified' import { Node } from 'unist' import visit from 'unist-util-visit' import hast from 'hast' function lazyloadPlugin(this: Processor): Transformer { function visitor(el: hast.Element) { if (el.tagName !== 'img') { return } el.properties = { ...(el.properties || {}), loading: 'lazy' } } function transformer(htmlAST: Node): Node { visit<hast.Element>(htmlAST, 'element', visitor) return htmlAST } return transformer } export default lazyloadPlugin module.exports = lazyloadPlugin