Git Product home page Git Product logo

metalsmith-matters's Introduction

metalsmith-matters Build Status

A Metalsmith plugin to read file metadata from frontmatter. Supports all frontmatter formats supported by the gray-matter library (including YAML, JSON, TOML, CSON, and more).

"But wait!" you say. "Doesn't Metalsmith already have frontmatter parsing built-in"? Well, yes. For now anyway. This plugin is an attempt to extract that functionality out of the Metalsmith core. Eventually the goal is to have frontmatter parsing removed from the Metalsmith core entirely in favor of this plugin or a similar one (see segmentio/metalsmith#157).

Even in the absence of that change though, this plugin has several key advantages over the built-in frontmatter parsing in Metalsmith. Firstly, it's composable with other plugins. For example, you could use it with metalsmith-branch to turn on frontmatter parsing for only a subset of files, or you could place it after metalsmith-s3 in the plugin list so that it parses frontmatter in downloaded files.

Secondly, this plugin provides a way to pass options to the underlying frontmatter parsing library, gray-matter. This allows you to do things like change the delimiter used to separate frontmatter from the rest of the file, or set TOML as the default frontmatter format.

Thirdly, this plugin provides an additional feature allowing parsed frontmatter to be automatically namespaced under a single key (e.g. page.keys instead of keys).

These are all things you can't do with Metalsmith's built-in frontmatter parsing.

Installation

npm install --save metalsmith-matters

CLI Usage

After installing metalsmith-matters, simply add the metalsmith-matters key to the plugins in your metalsmith.json file. Be sure to include it before any plugins which need to use the metadata in your frontmatter, or which expect as input files with no frontmatter header. Generally speaking, this means that metalsmith-matters should be the first plugin in the list.

{
  "frontmatter": false, // Disable built-in frontmatter parsing (recommended)
  "plugins": {
    "metalsmith-matters": {
      // Options
    }
    // Other plugins...
  }
}

JavaScript Usage

After installing metalsmith-matters, you can require metalsmith-matters in your code, then call the exported value to initialize the plugin and pass the result to Metalsmith.use (just as you would with any other Metalsmith plugin). Again, be sure to use metalsmith-matters before any plugins which need to use the metadata defined your frontmatter, or which expect as input files with no frontmatter header. Generally speaking, this means that metalsmith-matters should be the first plugin in the list.

var frontmatter = require('metalsmith-matters');

Metalsmith(__dirname)
  .frontmatter(false) // Disable built-in frontmatter parsing (recommended)
  .use(frontmatter({
    // Options
  }))
  .use(/* Other plugins... */)
  .build(function(err) {
    if (err) throw err;
  });

Options

namespace

Type: string
Default: undefined

Namespace all parsed data under a single key.

Example

Your frontmatter:

---
title: My title
author: Joe Writer
---

In the JavaScript:

  // new Metalsmith…
  .use(frontmatter({ namespace: 'page' }))
  // …build()

Now in your HTML, you can access the metadata like this (example with Handlebars):

  <h1>{{ page.title }}</h1>
  <p>Author: {{ page.author }}</p>

gray-matter Options

metalsmith-matters also supports all the options supported by gray-matter, metalsmith-matters' underlying frontmatter parsing library (it's the same one used by the Metalsmith core). These include:

  • parser
  • eval
  • lang
  • delims

For details on how to use these options, see the documentation for gray-matter.

metalsmith-matters's People

Contributors

adamkiss avatar ajedi32 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

metalsmith-matters's Issues

Global metadata

Is there a way to expose the collected frontmatter metadata as a separate top-level namespace? or within the metadata property of metalsmith?

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.