# ampify

> Convert plain HTML to Google Accelerated Mobile Pages (AMP)

Latest version **1.0.0** (published 2022-07-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install ampify
pnpm add ampify
yarn add ampify
bun add ampify
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-07-01 |
| First published | 2016-03-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 4 |
| Unpacked size | 15.7 KB |
| Known vulnerabilities | 0 (+26 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 109 |
| Author | rkazakov |
| Maintainers | rkazakov |
| Keywords | html, transform, amp, amp-img, amp-video, amp-youtube, amp-analytics |

## Links

- npm: https://www.npmjs.com/package/ampify
- Repository: https://github.com/rkazakov/ampify
- Homepage: https://github.com/rkazakov/ampify#readme
- Issues: https://github.com/rkazakov/ampify/issues
- npm.io page: https://npm.io/package/ampify

## Dependencies (4)

- [axios](https://npm.io/package/axios.md) ^0.27.2
- [cheerio](https://npm.io/package/cheerio.md) ^1.0.0-rc.12
- [clean-css](https://npm.io/package/clean-css.md) ^5.3.0
- [image-size](https://npm.io/package/image-size.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

- 1.0.0 (latest) — 2022-07-01
- 0.7.0 — 2022-07-01
- 0.6.0 — 2022-07-01
- 0.5.0 — 2019-04-05
- 0.4.2 — 2018-06-21
- 0.4.1 — 2018-06-21
- 0.4.0 — 2018-06-12
- 0.3.0 — 2018-06-02
- 0.2.6 — 2017-04-27
- 0.2.5 — 2017-04-09
- 0.2.4 — 2016-04-21
- 0.2.3 — 2016-04-05
- 0.2.2 — 2016-04-02
- 0.2.1 — 2016-03-12
- 0.1.0 — 2016-03-04

## README

# ampify

[![NPM Version][npm-image]][npm-url]
[![Downloads Stats][npm-downloads]][npm-url]

[![Build Status](https://travis-ci.org/rkazakov/ampify.svg?branch=master)](https://travis-ci.org/rkazakov/ampify)
[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)
[![Known Vulnerabilities](https://snyk.io/test/github/rkazakov/ampify/badge.svg)](https://snyk.io/test/github/rkazakov/ampify)
[![Greenkeeper badge](https://badges.greenkeeper.io/rkazakov/ampify.svg)](https://greenkeeper.io/)

> Convert plain HTML to Google AMP (Accelerated Mobile Pages)

## Installation

```sh
npm install ampify
```

## Usage

```js
const ampify = require('ampify');
const html = '<YOUR_HTML_CONTENT>';
const amp = ampify(html, {
  cwd: 'amp',
  canonicalURL: 'https://<your-canoncial-url>',
});
console.log(amp); // Content of AMP HTML
```

## Options

### cwd

#### Assets (images/styles) file path

- Type: `String`
- Default: `''`

### round

#### Enable images dimensions rounding

- Type: `String`
- Default: `true`

### canonicalURL
- Type: `String`
- Default: `''`

Cannonical URL should be a full valid URL. Please see [Make your pages discoverable](https://www.ampproject.org/docs/fundamentals/discovery).

## Example

### Input

```html
<html>
  <head>
    <link rel="stylesheet" href="styles.css">
  </head>
  <img src="image.png">
</html>
```

#### image.png

[image.png](/test/image.png)

#### style.css

```css
body {
  background-color: #fff;
}
```

### Output

```html
<html amp="">
  <head>
    <style amp-custom="">body{background-color:#fff}</style>
  </head>
  <amp-img src="image.png" width="600" height="400"></amp-img>
</html>
```

## More examples

See `/examples` folder for full source code.

### Using in Express App

```js
const ampify = require('ampify');
const express = require('express');

const app = express();

app.get('/article', async (req, res) => {
  const html = `
    <html>
      <head>
      <title>AMP page</title>
      </head>
      <body>
        <div>
          <p>This is an AMP article</p>
        </div>
      </body>
    </html>
  `;

  const amp = await ampify(html, {cwd: 'amp'});
  res.send(amp); // serving AMP content
});

app.listen(3000, () => {
  console.log('Listening on port 3000!');
});
```

### Using as Express middleware

```js
const ampify = require('ampify');
const express = require('express');

const app = express();

app.use((req, res, next) => {
  if (req.url.startsWith('/amp')) {
    const send = res.send;
    res.send = async (html) => {
      const amp = await ampify(html, {cwd: 'amp'});
      send.call(this, amp);
    };
  }
  next();
});

app.get('/amp/article', (req, res) => {
  const html = `
    <html>
      <head>
        <title>AMP page</title>
      </head>
      <body>
        <div>
          <p>This is AMP article</p>
        </div>
      </body>
    </html>
  `;
  res.send(html);
});

app.get('/article', (req, res) => {
  const html = `
    <html>
      <head>
        <title>HMTL page</title>
      </head>
      <body>
        <div>
          <p>This is HTML article</p>
        </div>
      </body>
    </html>
  `;
  res.send(html);
});

app.listen(3000, () => {
  console.log('Listening on port 3000!');
});
```

## Release History

[HISTORY](./HISTORY.md)

## Licence

MIT (c) Ruslan Kazakov and [contributors](https://github.com/rkazakov/ampify/graphs/contributors)

[PostXML]: https://github.com/postxml/postxml
[npm-url]: https://www.npmjs.org/package/ampify
[npm-image]: https://img.shields.io/npm/v/ampify.svg?style=flat-square
[npm-downloads]: https://img.shields.io/npm/dm/ampify.svg?style=flat-square

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