Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

ArmorDarks/grunt-gray-matter

Repository files navigation

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

grunt-gray-matter task demo

Getting Started

If you haven't usedGrunt before, be sure to check out theGetting Started guide, as it explains how to createaGruntfile 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.initConfiggrayMatter:build:src:'source/templates/{,**/}*.html'dest:'data/matter.json'

Finally, run task with following command:

grunt grayMatter

ThegrayMatter task

Task scans specified infilessrc files, reads Gray Matter (Front Matter) header withGray Matter and outputs collected result in form of JSON to specifieddest file.

Path of each discoveredsrc will serve as property in final JSON for extracted Gray Matter data of thatsrc.

Options

options.baseDir

Type:String Default:''

Path portion which should be subtracted from path to discoveredsrc file.

Result will be used indest 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 indest JSON file.

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

You can seehere 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 subtractingoptions.baseDir path of currentsrc, used to nest data in final JSON;
  • filepath — filepath of currentsrc.

this will correspond to currentfile of task.

Should return altered path in form ofString orString[].

options.preprocessMatterData

Type:Function Default:undefined

Allows to alter extracted Gray Matter data for each discoveredsrc.

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

Will be invoked with injected values for following arguments:

  • matterData — extracted Matter data of currentsrc;
  • path — formed after subtractingoptions.baseDir path of currentsrc, used to nest data in final JSON;
  • filepath — filepath of currentsrc.

this will correspond to currentfile object.

Should return altered Gray Matter data in form ofObject.

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 todest file.

Will be invoked with injected values for following arguments:

  • data — extracted and assembled data from all currentsrces Matter data, processed bypreprocessPath andpreprocessMatterData (if specified).

this will correspond to currentthis of task.

Should return altered final data in form ofObject, which will be printed todest file.

options.replacer

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

Passreplacer parameter toJSON.stringify().

options.space

Type:String|Integer Default:2

Passspace parameter toJSON.stringify().

Controls indentation for outputted JSON file.

options.parser

Type:Function Default:undefined

Passcustom parser to Gray Matter.

options.eval

Type:Boolean Default:false

Passeval parameter to Gray Matter.

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

options.lang

Type:String Default:undefined

Passlang parameter to Gray Matter.

options.delims

Type:String Default:undefined

Passdelims parameter to Gray Matter.

Usage examples

@config'grayMatter',build:options:baseDir:'source/templates'preprocessPath: (path)-># do something with paths in constructed json filereturn pathpreprocessMatterData: (data,path,src)-># do something with extracted datareturn datasrc:'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

Runnpm test to launch tests ornpm run test:watch to watch tests. Observe how magic happens.

Testing requires Node >= 4.0.0.

Examples

License

Copyright 2016 Serj Lavrin.

Licensed under theApache 2.0 license.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp