# posthtml-insert-at

> PostHTML plugin to append or prepend HTML to a selector

Latest version **0.2.7** (published 2020-06-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install posthtml-insert-at
pnpm add posthtml-insert-at
yarn add posthtml-insert-at
bun add posthtml-insert-at
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.2.7 |
| Published | 2020-06-30 |
| First published | 2019-06-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 13 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Eric Liu |
| Maintainers | metonym |
| Keywords | html, posthtml, posthtml-plugin, insert, prepend, append |

## Links

- npm: https://www.npmjs.com/package/posthtml-insert-at
- Repository: https://github.com/posthtml/posthtml-insert-at
- Issues: https://github.com/posthtml/posthtml-insert-at/issues
- npm.io page: https://npm.io/package/posthtml-insert-at

## Dependencies (3)

- [posthtml](https://npm.io/package/posthtml.md) ^0.12.0
- [posthtml-parser](https://npm.io/package/posthtml-parser.md) ^0.4.1
- [posthtml-match-helper](https://npm.io/package/posthtml-match-helper.md) ^1.0.1

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 0.2.7 (latest) — 2020-06-30
- 0.2.6 — 2019-10-16
- 0.2.5 — 2019-10-12
- 0.2.4 — 2019-09-21
- 0.2.3 — 2019-08-17
- 0.2.2 — 2019-06-28
- 0.2.1 — 2019-06-26
- 0.2.0 — 2019-06-23
- 0.1.0 — 2019-06-23

## README

# posthtml-insert-at <img align="right" width="220" height="200" title="PostHTML logo" src="http://posthtml.github.io/posthtml/logo.svg">

[![NPM][npm]][npm-url]
[![Build][build]][build-badge]
[![Coverage][codecov-shield]][codecov]

`posthtml-insert-at` is a [PostHTML](https://github.com/posthtml/posthtml) plugin to append or prepend HTML to a selector.

## [Examples](examples/)

**Before:**

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

**After:**

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

## Install

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

## Usage

```js
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.

```js
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](https://github.com/posthtml/posthtml/tree/master/docs).

## [Changelog](CHANGELOG.md)

## License

[MIT](LICENSE)

[npm]: https://img.shields.io/npm/v/posthtml-insert-at.svg?color=blue
[npm-url]: https://npmjs.com/package/posthtml-insert-at
[build]: https://travis-ci.org/posthtml/posthtml-insert-at.svg?branch=master
[build-badge]: https://travis-ci.com/posthtml/posthtml-insert-at
[codecov]: https://codecov.io/gh/posthtml/posthtml-insert-at
[codecov-shield]: https://img.shields.io/codecov/c/github/posthtml/posthtml-insert-at.svg

---
_Source: https://npm.io/package/posthtml-insert-at · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
