A module to transform HTML directly to Google AMP HTML
amps-in-the-trunk will go through your HTML, use the DOM to change <img>
into <amp-img>
and add missing height / width stats.
How to use with express.js (and how to do the bits this module doesn't)
So the main things with AMP for me were:
- No script tags (except the AMP script tag)
- Styles need to be inline
- 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
<img>
into AMP HTML <amp-img>
So you're using express, this looks like
- Use
layout=
to specify the AMP layout rather than your normal one - Provide a callback to
res.render()
to allow us to intercept the rendered HTML and convert it to AMP:
var ampsInTheTrunk = require('amps-in-the-trunk')()
router.get('/blog', function(req, res){
res.render('blog-index', {
'title': 'My Blog',
inlineStyles,
styles: [ 'style', 'articles']
}, function(err, output){
if ( err ) {
// Your error handling here
return;
}
var ampHTML = ampsInTheTrunk.toAmp(output);
res.send(200, ampHTML)
});
});
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)
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