Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ampify

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ampify

Convert plain HTML to Google Accelerated Mobile Pages (AMP)

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1K
decreased by-12.58%
Maintainers
1
Weekly downloads
 
Created
Source

ampify

NPM Version Downloads Stats

Build Status tested with jest Known Vulnerabilities Greenkeeper badge

Convert plain HTML to Google AMP (Accelerated Mobile Pages)

Installation

npm install ampify

Usage

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.

Example

Input

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

image.png

style.css
body {
  background-color: #fff;
}

Output

<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

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

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

Licence

MIT (c) Ruslan Kazakov and contributors

Keywords

FAQs

Package last updated on 01 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc