npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

amps-in-the-trunk

v1.3.0

Published

Turns HTML in Google AMP HTML. Includes express middleware.

Downloads

19

Readme

A module to transform HTML directly to Google AMP HTML

amps-in-the-trunk turns HTML into AM HTML: changing <img> into <amp-img>, including:

  • Adding missing height / width stats
  • Performing server-side syntax highlighting for <pre><code> blocks.
  • Stripping inline syles

It also comes with express middleware so you can res.renderAMP(). Woo!

How to use with express.js (and how to do the bits this module doesn't)

The biggest challenges for AMP are:

  • No script tags (except the AMP script tag)
  • CSS needs to be included in the HTML
  • No inline style attributes (most web developers don't use these, but some libraries eg for markdown generation output them)
  • Images always need height and width

Most apps use a templating engine to generate their HTML. So as a developer:

  • Use a different layout that:
    • doesn't have all the usual analytics <script> tags
    • inlines all stylesheets
  • Take the HTML generated by your templating engine and use amps-in-the-trunk to turn HTML into AMP HTML.

So you're using express, this looks like

  • Use layout= to specify the AMP layout rather than your normal one
  • Use res.renderAMP(view, local) to render HTML and convert it to AMP:
// Load the module and pass in any overrides
// imageOverrides optional Object - see below
// enableCodeHighlighting optional Boolean - see below
// languageSubset - optional Array - for highlightjs. See http://highlightjs.readthedocs.io/en/latest/api.html#highlightauto-value-languagesubset
var ampsInTheTrunk = require('amps-in-the-trunk')(imageOverrides, enableCodeHighlighting, languageSubset)

// Enable the renderAMP Express middleware
app.use(ampsInTheTrunk.renderAMP);

// Handle a route by respondingwith an AMP page
router.get('/blog', function(req, res){
	res.renderAMP('blog-index', {
		'title': 'My Blog',
		inlineStyles,
		styles: [ 'style', 'articles']
	});
});

The imageOverrides option - when SVGs aren't sized correctly, or I want a different layout for an image

amps-in-the-trunk sets images to responsive by default. You can change that with imageOverrides. Also most SVGs have incorrect sizing. You could fix the SVGs, or you could override them with imageOverrides.

var imageOverrides = {
	'logo.svg': {
		width: 22,
		height: 22,
		layout: 'fixed'
	},
	'rss.svg': {
		width: 26,
		height: 16,
		layout: 'fixed'
	}
}

var ampsInTheTrunk = require('amps-in-the-trunk')(imageOverrides)

The enableCodeHighlighting option - highlights <pre><code> blocks using highlight.js

If you want syntax highlighting, AMPS in the Trunk will use highlight.js to highlight the specific blocks.

Why another AMP module?

I tried html-to-amp but that uses html-to-article-json first and it lost images. I don't care about article.json and I don't care about non-AMP alternative formats. I just wanted an AMP version of my HTML that didn't lose any data and would pass the AMP validator.

Pull requests welcome

This is the first version of the module, created really for my own needs. I'm quite happy to take pull requests for additional features.

Why the name?

Because amplify and html-to-amp were taken and because House of Pain