Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eleventy-plugin-transformdom

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eleventy-plugin-transformdom

An eleventy plugin to modify the DOM when outputting HTML

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
23
increased by130%
Maintainers
1
Weekly downloads
 
Created
Source

DOM Transforming plugin

Banner image

Process & change the generated HTML output of your Eleventy site.

Configure the plugin with your CSS selectors and transform functions, then the plugin will run them on each HTML file generated by Eleventy.

Some of the things you could use it to do include:


Like this project? Buy me a coffee via PayPal or ko-fi


Getting started

Install the plugin

In your project directory run:

# Using npm
npm install eleventy-plugin-transformdom --save-dev

# Or using yarn
yarn add eleventy-plugin-transformdom --dev

Then update your project's .eleventy.js to use the plugin:

const transformDomPlugin = require('eleventy-plugin-transformdom');

module.exports = function (eleventyConfig) {
  eleventyConfig.addPlugin(transformDomPlugin, [
    {
      selector: 'img',
      transform: ({ elements }) => {
        elements.forEach((img) => {
          img.setAttribute('loading', 'lazy');
        });
      },
    },
  ]);
};

☝️ The example above shows a Transform Item that finds all the images in your HTML and adds loading="lazy" to the markup. Read on to learn how to customise your own transforms.

Write your Transform Items

The plugin takes a Transform Item array as configuration.

A Transform Item is an Object that contains two keys; selector & transform

Each Transform Item will be run in order. This means you can write a Transform Item that modifies the DOM, then the next Transform Item can further modify the resulting DOM, and so on.

selector

The "selector" is a CSS selector (string).

Multiple selectors may be separated with commas.

transform

The "transform" is a function ((args) => void) that will be run on the generated HTML.

The function will be passed an Object as argument. The args Object has the following entries:

KeyTypeDescription
elementsElement[]An array of elements in the DOM matching the provided selector
windowDOMWindowThe window of the generated HTML
documentDocumentThe window.document of the generated HTML
inputPathstringThe source file path from which Eleventy is generating the HTML
outputPathstringThe output path/filename of the HTML file being generated
inputDirstringThe source directory from which Eleventy is building the site (see the Eleventy docs)
outputDirstringThe directory inside which the finished templates will be written to by Eleventy (see the Eleventy docs)

Note: async transform functions are supported, however they will be run in sequence to prevent race conditions.

Contributing

This project welcomes suggestions and Pull Requests!

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

  • The wonderfully supportive team at Future Friendly
  • Everyone who has contributed to the 11ty and JSDOM projects, without whom this plugin wouldn't run

Like this project? Buy me a coffee via PayPal or ko-fi


Keywords

FAQs

Package last updated on 26 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc