node package manager
Easy collaboration. Discover, share, and reuse code in your team. Create a free org »

metadataify

metadataify

Stream meta tags into html

Build Status npm version Dependency Status Semistandard Style unstable

Introduction

Add metadata tags to a stream of html so that it looks pretty when you share it. See: indexhtmlify#5.

NB: My only use-case for this module is for building my own static pages. I've tried to reasonably escape entities so that scrapers handle the meta tags well. Escaping via the entities module is too strong so that entities show up in the scraped data. Instead I've escaped &, ", < and >. That should provide basic sanitization, but you should exercise caution if passing end-user data through to a publicly distributed page. I'm not willing to assert that malicious injection is absolutely impossible. Please don't hesitate to file an issue or PR with any issues or concerns.

Installation

$ npm i -g metadataify 

Usage

metadataify is designed to just work without much configuration, but allows overrides when necessary. The input must be valid html containing head and title tags. Insertion of the meta tags is not idempotent. Used without any arguments, the command line version looks for the nearest package.json. For example:

$ metadataify < input.html

Given package.json:

{
  "name": "metadataify",
  "description": "Stream meta tags into html",
  "author": "Ricky Reusser"
}

and given a minimal html input

<!doctype html>
<html>
  <head>
    <title>-</title>
  </head>
  <body>
  </body>
</html>

it produces the output:

<!doctype html>
<html>
  <head>
    <title>metadataify</title>
    <meta name="application-name" content="metadataify">
    <meta name="subject" content="Stream meta tags into html">
    <meta name="abstract" content="Stream meta tags into html">
    <meta name="twitter:title" content="metadataify">
    <meta name="description" content="Stream meta tags into html">
    <meta name="twitter:description" content="Stream meta tags into html">
    <meta name="author" content="Ricky Reusser">
    <meta name="twitter:creator" content="Ricky Reusser">
    <meta property="og:title" content="metadataify">
    <meta property="og:description" content="Stream meta tags into html">
    <meta property="article:author" content="Ricky Reusser">
  </head>
  <body>
  </body>
</html>

If you don't want to pollute your package.json with metadata for sharing, you can override the defaults by adding a metadataify field to your package.json, e.g:

{
  "name": "metadataify",
  "description": "My npm package description",
  "author": "Ricky Reusser",
  "metadataify": {
    "name": "My metadataify demo",
    "description": "Here's a neat demo",
    "url": "http://metadataify.github.io",
    "author": "Ricky Reusser",
    "image": "http://rawgit.com/rreusser/metadataify/master/images/screenshot.png"
  }
}

Even better, use it with indexhtmlify:

$ browserify index.js | indexhtmlify | metadataify > index.html

To override the nearest package.json with your own input JSON:

$ metadataify --input=mydata.json < input.html

You can disable json input with --no-input and instead specify fields on the command line:

$ metadataify --no-input --title="My page!" --description="A description..." --author="My Name" < input.html

If you need further customization or more specificity, look in the code to see the precise logic. PRs with improvements are welcome!

API

require('metadataify')([data])

Returns a transform stream that applies to a stream of html the changes specified in data. The format of data identically matches the format of package.json that is read by the command line version, including the optional metadataify field.

See also

This module is heaviliy inspired by and works great with indexhtmlify. Many thanks to Dima Yv for the initial inspiration.

License

© 2016 Ricky Reusser. MIT License.