Code Monkey home page Code Monkey logo

node-inject-html's Introduction

🧷 node-inject-html

Fast, lightweight HTML injection via string, for when you don’t have access to the DOM (e.g. Node.js).

⚠️ If you do have access to the DOM, don’t use this! Use appendChild() instead.

This library only has 1 dependency: moo, a highly-optimized JS lexer.

If you are searching for a tool to perform this change in bulk, check out the CLI version.

Example

npm i node-inject-html
import { inject } from 'node-inject-html';

const html = `<!doctype HTML>
<html>
  <head>
    <link rel="stylesheet" href="/styles.css" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
`;

inject(html, {
  headStart: '<script src="https://someanalyticsfunction.com"></script>',
  headEnd:
    '<link href="https://fonts.googleapis.com/css2?family=Public+Sans:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet">',
  bodyStart:
    '<noscript>You need JavaScript enabled to run this application.</noscript>',
  bodyEnd: '<script async src="./hmr.js"></script>',
});

// <!doctype HTML>
// <html>
//   <head>
//     <script src="https://someanalyticsfunction.com"></script> <!-- NEW -->
//     <link rel="stylesheet" href="/styles.css" />
//     <link href="https://fonts.googleapis.com/css2?family=Public+Sans:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet"> <!-- NEW -->
//   </head>
//   <body>
//     <noscript>You need JavaScript enabled to run this application.</noscript> <!-- NEW -->
//     <div id="app"></div>
//     <script async src="./hmr.js"></script>
//   </body>
// </html>

FAQ

When do I need this?

This is a useful tool for SSR or any time you need to scan the DOM without having access to the DOM itself, like in Node.

Can I use this in conjunction with jsdom, cheerio, etc.?

Yes! This library is lightweight and performant enough you may find it handles better than a heavy DOM parser / AST library.

Can I insert any HTML?

Yes! Any HTML (but note that malformed HTML will break your app—this won’t validate it!)

I need an AST. Can this do that?

No. Try node-html-parser or something.

Can I add other hooks?

Yes! Contributions are welcome. Please see CONTRIBUTING.md to get started.

Can I use this in a browser?

**No!**️ If you are running this in the context of a browser, you should use appendChild() instead.

node-inject-html's People

Contributors

drwpow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

gr2m sators

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.