Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

posthtml-insert-at

posthtml2.4kMIT0.2.7TypeScript support: included

PostHTML plugin to append or prepend HTML to a selector

html, posthtml, posthtml-plugin, insert, prepend, append

readme

posthtml-insert-at

NPM Build Coverage

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

Examples

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.
       * Selectors include tag name (e.g. `main`), class (e.g. `.main`) or id (e.g. `#main`).
       */
      selector: 'main',

      /**
       * Prepend HTML markup at the selector.
       */
      prepend: `
        <header>
          <a href="/">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.

Changelog

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

  • Add more intuitive default options

0.2.7 - 2020-06-30

0.2.6 - 2019-10-16

  • Upgrade posthtml from 0.11.6 to 0.12.0

  • Upgrade development dependencies (@types/jest, husky, typescript, pretty-quick)

0.2.5 - 2019-10-12

  • Fix regression where content is not prepended to an empty tag (#3)

0.2.4 - 2019-09-21

  • Upgrade posthtml version from 0.14 to 0.16

  • Refactor typings

0.2.3 - 2019-08-17

  • Update documentation

0.2.2 - 2019-06-28

  • Add alias default export insertAt and posthtmlInsertAt to allow more intuitive importing (07a5c0c)

0.2.1 - 2019-06-25

  • Copy all properties of targeted node (6fc2d85)

0.2.0 - 2019-06-23

  • Add project documentation on configuration

  • Unit test for createMatcher (e14bd8a)

  • Collect code coverage using Codecov and Travis CI

0.1.0 - 2019-06-23

  • Initial release