npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-gray-matter

v1.3.0

Published

A Grunt task for extracting data header from file contents using Gray Matter

Downloads

32

Readme

grunt-gray-matter Build Status

A Grunt task for extracting data header from file contents using Gray Matter

grunt-gray-matter task demo

Getting Started

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins.

Install plugin with following command:

npm install grunt-gray-matter --save

Once installed, eenable task inside your Gruntfile:

grunt.loadNpmTasks('grunt-gray-matter')

Add minimal configuration for task in Gruntfile:

grunt.initConfig
  grayMatter:
    build:
      src: 'source/templates/{,**/}*.html'
      dest: 'data/matter.json'

Finally, run task with following command:

grunt grayMatter

The grayMatter task

Task scans specified in files src files, reads Gray Matter (Front Matter) header with Gray Matter and outputs collected result in form of JSON to specified dest file.

Path of each discovered src will serve as property in final JSON for extracted Gray Matter data of that src.

Options

options.baseDir

Type: String Default: ''

Path portion which should be subtracted from path to discovered src file.

Result will be used in dest file JSON as property of extracted Gray Matter data.

@config 'grayMatter',
  build:
    options:
      baseDir: 'source/templates'
    src: 'source/templates/page.html'
    dest: 'matter.json'

Will result in

{
  "page": {
    "html": {
      ...
    },
  }
}

instead of

{
  "source/templates/page": {
    "html": {
      ...
    },
  }
}

options.preprocessPath

Type: Function Default: undefined

Allows to alter path (property) under which extracted Matter data will be placed in dest JSON file.

Nesting will be done with _.set, thus any . in path or array of strings will result in nesting data deeper.

You can see here as Kotsu uses this option to mirror templates directory structure in final JSON.

Will be invoked with injected values for following arguments:

  • path — formed after subtracting options.baseDir path of current src, used to nest data in final JSON;
  • filepath — filepath of current src.

this will correspond to current file of task.

Should return altered path in form of String or String[].

options.preprocessMatterData

Type: Function Default: undefined

Allows to alter extracted Gray Matter data for each discovered src.

As for an example, check out how Kotsu injects additional data for each page into final Matter data.

Will be invoked with injected values for following arguments:

  • matterData — extracted Matter data of current src;
  • path — formed after subtracting options.baseDir path of current src, used to nest data in final JSON;
  • filepath — filepath of current src.

this will correspond to current file object.

Should return altered Gray Matter data in form of Object.

options.preprocessData

Type: Function Default: undefined

Allows to alter final data, constructed from extracted Matter data.

Result of this function will be used for printing Object to dest file.

Will be invoked with injected values for following arguments:

  • data — extracted and assembled data from all current srces Matter data, processed by preprocessPath and preprocessMatterData (if specified).

this will correspond to current this of task.

Should return altered final data in form of Object, which will be printed to dest file.

options.replacer

Type: Function|String[]|Number[] Default: null

Pass replacer parameter to JSON.stringify().

options.space

Type: String|Integer Default: 2

Pass space parameter to JSON.stringify().

Controls indentation for outputted JSON file.

options.parser

Type: Function Default: undefined

Pass custom parser to Gray Matter.

options.eval

Type: Boolean Default: false

Pass eval parameter to Gray Matter.

Forces Gray Matter to evaluate CoffeeScript or JavaScript in Front Matter.

options.lang

Type: String Default: undefined

Pass lang parameter to Gray Matter.

options.delims

Type: String Default: undefined

Pass delims parameter to Gray Matter.

Usage examples

@config 'grayMatter',
  build:
    options:
      baseDir: 'source/templates'

      preprocessPath: (path) ->
        # do something with paths in constructed json file
        return path

      preprocessMatterData: (data, path, src) ->
        # do something with extracted data
        return data

    src: 'source/templates/{,**/}*.html'
    dest: 'data/matter.json'

Node support

Requires Node 6.0 or higher.

Just in case, grunt-gray-matter 1.2.0 was the last version to support Node 0.10.0. But better go update your Node. Seriously.

Testing

Run npm test to launch tests or npm run test:watch to watch tests. Observe how magic happens.

Testing requires Node >= 4.0.0.

Examples

License

Copyright 2016 Serj Lavrin.

Licensed under the Apache 2.0 license.