easy-html-injector
Introduction
easy-html-injector is a simple Node module that allows you to inject HTML elements into an HTML string. The package exports a single function inject(html: string, element: HtmlElement)
that takes an HTML string and an object representing an HTML element to be injected, and returns the modified HTML string with the new element inserted at the specified position.
Installation
To install easy-html-injector
, use pnpm, npm, or yarn:
pnpm add easy-html-injector
npm install easy-html-injector
yarn add easy-html-injector
Bonus: Using my package manager
I've made a package manager called Ultra. If you want to try it out, you can install it with:
npm install -g @ultrapkg/core
u add easy-html-injector
Usage
First, import the inject function:
import { inject } from 'easy-html-injector';
The inject function takes two arguments:
Here is an example usage:
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<title>My page</title>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
`;
const newElement = {
parent: 'head',
position: 'prepend',
tag: 'meta',
attributes: {
charset: 'utf-8',
},
};
const modifiedHtml = inject(html, newElement);
console.log(modifiedHtml);
This will output:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My page</title>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
License
easy-html-injector
is released under the MIT License. See the LICENSE file for more details.
Benchmark
Benchmark | Average | Min | Max | Median | Std Dev | Variance | Ops | Ops/sec |
---|
inject | 0.016 | 0.0030 | 0.11 | 0.0043 | 0.032 | 2.0 | 62631 | 62630905 |
cheerio | 0.87 | 0.35 | 4.5 | 0.43 | 1.2 | 1.4 | 1147 | 1146707 |