GithubHelp home page GithubHelp logo

grunt-frontmatter's Introduction

grunt-frontmatters

Extract YAML front-matter from files to a single JSON file

The use case of grunt-frontmatter task is to strip all YAML front-matter pressent in multiple files (.html, .md, whatsoever) and combine them all in a single JSON file that can be loaded at once as a single file. It also is able to strip few firsts characters from each file to make a preview.

In addition to data already present in the YAML following fields are added:

  • preview of the actual body content
  • basename of the filename
  • md5 with the md5 signature of the file body

The "frontmatter" task

Pretend you have a folder structure like this:

.
├── Gruntfile.coffee
├── component.json
├── package.json
└── source
    ├── articles
    │   ├── bellflower.md    <--
    │   ├── fiddler.html     <--
    │   └── lottery.md       <--
    ├── favicon.ico
    ├── index.jade
    ├── style.styl
    ├── styles
    │   ├── h5bp.css
    │   ├── main.css
    │   └── normalize.css
    └── templates
        └── h5bp.jade

Each file in the articles directory has a bit of YAML front-matter metadata, like the title of the article, author, and tags. We want just the metadata from all three to be combined into a single JSON stringified file, called articles.json.

Now grunt release will build a release folder that looks like this:

.
├── Gruntfile.coffee
├── component.json
├── package.json
├── release
│   ├── articles
│   │   ├── bellflower.html
│   │   ├── fiddle.html
│   │   └── lottery.html
│   ├── articles.json            <---
│   ├── favicon.ico
│   ├── index.html
│   └── style.css
└── source

Configuring your "frontmatter" task

In your project's Gruntfile, add a section named frontmatter to the data object passed into grunt.initConfig().

frontmatter: {
    release: {
        options: {
            minify: true,
            width: 60
        },
        files: {
            'release/articles.json': ['source/articles/*.md']
        },
    }
}

Options

options.minify

Type: Boolean Default value: false

If true, then the JSON.stringify is instructed to strip unnecessary linebreaks, making the resulting .json file smaller.

options.dirname

Type: Boolean Default value: false

If true, then each entry has the dirname entry and object keys are composed with dirname + '/' + basename.

options.width

Type: Int | String Default value: "sentence"

If it is an Integer no more than width charactes from the Markdown file's body is saved in the preview element. Trailing ellipses are not added. If it is "sentence" saves until the first ". ", if it is a String number + "s" (ex: "2s") it is the number of sentences.

If preview defined in the frontmatter of a file, it takes it as preview and ignores this option.

More Examples

See examples.

grunt-frontmatter's People

Contributors

drpicox avatar

Watchers

James Cloos avatar

grunt-frontmatter's Issues

Same key for 2 different files

Suppose i have the following structure

/foo/index.md
/bar/index.md

Then grunt-frontmatter is not able to handle it because it is using basename as key to the json object.

It would be good to be able to :

  • Output an array in the json
    OR
  • Use the full path name as the key in the json output

Thank you for your help

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.