项目作者: clay

项目描述 :
AMP HTML renderer for component data structures
高级语言: JavaScript
项目地址: git://github.com/clay/amphora-amp.git
创建时间: 2018-05-10T18:51:47Z
项目社区:https://github.com/clay/amphora-amp

开源协议:MIT License

下载


Amphora AMP HTML

CircleCI Coverage Status

The AMP HTML Format renderer for Clay components.

Install

$ npm install --save amphora-amp

Integration

Basic Configuration

First, ensure that you have a compatible version of Amphora installed (v3.x or greater) and require amphora-amp at the from wherever you are running Amphora.

  1. const amphoraAmp = require('amphora-amp');

Component Rendering

To make a Clay component renderable for AMP HTML, add a amp.template.hbs file to your component’s directory. Additionally if you need to include styles for your component you will need to add an amp variation of your component in your public/css directory which will be inlined similarly to how CSS is inlined for amphora-html. An example directory structure might look like:

  1. components
  2. clay-paragraph
  3. amp.template.hbs
  4. public
  5. css
  6. clay-paragraph_amp.css

Handlebars Helpers

Similar to amphora-html, if your templates require any custom Handlebars Helpers you can register them with the renderer’s Handlebars instance. Simply pass in an object whose keys are the names of your helpers and whose values are the helper themselves. Like so:

  1. // My helpers
  2. const helpers = {
  3. // set up handlebars helpers that rely on internal services
  4. 'nameOfHelper': () => {
  5. // helper that does something you need.
  6. return 'foobar';
  7. }
  8. };
  9. // Register helpers
  10. amphoraAmp.addHelpers(helpers);

Register Amphora AMP with your Amphora Instance

Now that you have registered any helpers you can register your renderer with Amphora. Registering consists of providing a renderers object whose keys are the extension of an HTTP request and whose values are the renderer.

  1. return amphora({
  2. app: app,
  3. renderers: {
  4. amp: amphoraAmp,
  5. html: amphoraHtml,
  6. default: 'html'
  7. },
  8. providers: ['apikey', amphoraProvider],
  9. sessionStore: redisStore,
  10. plugins: [
  11. amphoraSearch
  12. ]
  13. });

This will allow you to render when the extension is explicitly specified (i.e. using a URL like example.com/article.amp), if you want to have other routes render using the AMP renderer you will need to create routes in your Express app which include the :ext param (see the amphora rendering logic) for how the determination of which extension is considered for a route. An example of an Express route with this format would be:

  1. var express = require('express');
  2. var app = express();
  3. app.get('/:ext/article/:name', ...);

So that if we hit a URL like example.com/amp/article/article.html it will serve using the AMP renderer.

Contributing

Want a feature or find a bug? Create an issue or a PR and someone will get on it.