Code Monkey home page Code Monkey logo

koa-meta's Introduction

koa-meta

build status code style styled with prettier made with lass license

Meta <title> and <meta name="description"> middleware for Koa and Lad

Table of Contents

Install

npm:

npm install koa-meta

Usage

Use middleware:

const path = require('path');

const views = require('@ladjs/koa-views');
const Meta = require('koa-meta');

/// ...

// set template rendering engine (see @ladjs/web for inspiration)
app.use(views(path.join(__dirname, 'views')));

const meta = new Meta({
  '/': [ 'Home', 'Our home page description' ],
  '/contact', [ 'Contact', 'Contact us with questions' ]
});

// note: you can also pass a second argument of a custom `logger`
// the default logger is `console` and must have a `.error` method
// `const meta = new Meta({}, console);`

app.use(meta.middleware);

app.use((ctx, next) => {
  // since the previous middleware was defined before this
  // the `ctx.state` object has been populated with metadata
  // when the render call occurs (it will not override any existing set values)
  // for a request with `ctx.path` of `/` it will output:
  // { title: 'Home', description: 'Our home page description' }
  ctx.render('home');
});

Therefore in your views you can render the meta data easily:

doctype html
html
  head
    title= meta.title
    meta(name="description", content=meta.description)

Programmatically get a meta object/translated version of title and description:

const Meta = require('koa-meta');

const meta = new Meta({
  '/': [ 'Home', 'Our home page description' ],
  '/posts': [ 'Posts', 'Posts by our team' ]
});

console.log(meta.getByPath('/posts/123456'));
// `{ title: 'Posts', description: 'Posts by our team' }`

Translation Support

This package supports translation out of the box.

It checks for a function set on ctx.request.t and utilizes that function to translate based off the request's locale.

Child Path Support

This package supports parent meta data lookup for children of paths.

This means if you define in your configuration a path of /posts and a request is made to /posts/123456 (with this path not being defined in your configuration), then it will use /posts definition for /posts/123456.

Error Catching

By default this package will throw an error if a child path was found that does not have a parent defined.

This is extremely useful for retaining quality control with your configuration.

However this is configurable based off the third argument passed to new Meta(), e.g. new Meta(config, logger, levelForMissing.

The value of levelForMissing defaults to error for when process.env.NODE_ENV is equal to development, otherwise it defaults to debug.

This is configurable, therefore you can simply pass new Meta(config, logger, 'warn') if you want warn to be the log level for missing meta configurations.

Contributors

Name Website
Nick Baugh http://niftylettuce.com/

License

MIT © Nick Baugh

koa-meta's People

Contributors

niftylettuce avatar omgimalexis avatar shaunwarman avatar titanism avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

koa-meta's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because we are using your CI build statuses to figure out when to notify you about breaking changes.

Since we did not receive a CI status on the greenkeeper/initial branch, we assume that you still need to configure it.

If you have already set up a CI for this repository, you might need to check your configuration. Make sure it will run on all new branches. If you don’t want it to run on every branch, you can whitelist branches starting with greenkeeper/.

We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

Once you have installed CI on this repository, you’ll need to re-trigger Greenkeeper’s initial Pull Request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper integration’s white list on Github. You'll find this list on your repo or organiszation’s settings page, under Installed GitHub Apps.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.