pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/posthtml/posthtml-insert-at

sorigen="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/repository-6534fbc3f5e83ac0.css" /> GitHub - posthtml/posthtml-insert-at: PostHTML plugin to append or prepend HTML to a selector · GitHub
Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

163 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

posthtml-insert-at

NPM

posthtml-insert-at is a PostHTML plugin to append or prepend HTML to a selector.

Before:

<html>
  <body>
    <main></main>
  </body>
</html>

After:

<html>
  <body>
    <header></header>
    <main></main>
    <footer></footer>
  </body>
</html>

Install

yarn add -D posthtml-insert-at
# OR
npm i posthtml-insert-at

Usage

const fs = require("fs");
const posthtml = require("posthtml");
const { insertAt } = require("posthtml-insert-at");

const html = fs.readFileSync("./index.html");

posthtml()
  .use(
    insertAt({
      /**
       * Specify the selector to append/prepend content to.
       * Example selectors: `main`, `#id`, `.class`, `main.foo`.
       */
      selector: "main",

      /**
       * Prepend HTML markup at the selector.
       */
      prepend: `
        <header>
          <a href="?u=http://github.com/">Home</a>
        </header>
      `,

      /**
       * Append HTML markup at the selector.
       */
      append: `
        <footer>
          &copy; ${new Date().getFullYear()}
        </footer>
      `,

      /**
       * Specify whether to append/prepend content inside or outside (i.e. adjacent to) of the selector.
       *
       * The default behavior is `inside`.
       */
      behavior: "outside",
    })
  )
  .process(html)
  .then((result) => fs.writeFileSync("./after.html", result.html));

Options

Name Kind Description
selector required string Selector to insert markup at (e.g. .classname, #id or tag)
prepend optional string Markup to prepend to the selector
append optional string Markup to append to the selector
behavior optional ("inside" or "outside") - default is "inside" Whether to append/prepend content inside or outside of the selector

The plugin accepts an object or an an array of objects.

const option = {
  selector: "body",
  prepend: "<header></header>",
  append: "<footer></footer>",
  behavior: "inside",
};

insertAt(option);
// OR
insertAt([option /*, ...more options */]);

Limitations

Currently, this plugin does not supported nested selectors.

Contributing

See the PostHTML Guidelines.

License

MIT

About

PostHTML plugin to append or prepend HTML to a selector

Topics

Resources

Stars

Watchers

Forks

Used by

Contributors

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy