node package manager


yaml front matter for JS. Parse yaml or JSON from the beginning of files.

Yaml Front Matter

Parses yaml or json at the front of a file. Places the parsed content plus the rest of the file's content into an object literal.


name: Derek Worthen
age: young
 address: some location
 - cat
 - dog
 - bat
match: !!js/regexp /pattern/gim
run: !!js/function function() { }
Some Other content


{ name: 'Derek Worthen',
  age: 'young',
  contact: { email: '', address: 'some location' },
  pets: [ 'cat', 'dog', 'bat' ],
  match: /pattern/gim,
  run: [Function],
  __content: 'Some Other Content' }

May also use JSON

"name": "Derek Worthen",
"age": "young",
"anArray": ["one","two"],
"subObj":{"field1": "one"}
$ npm install gulp -g && npm install --dev && npm start

Then visit localhost:3000.

$ npm install yaml-front-matter

Use the -g flag if you plan on using the command line tool.

$ npm install yaml-front-matter -g

Include one of the client files from dist/.

<script src="js-yaml-front-client.min.js"></script>
  // parse front matter with jsyaml.loadFront(String); 

Outputs client files in dist/.

$ npm install gulp -g && npm install --dev && gulp build
Usage: js-yaml-front.js [options] <file>


-h, --help            output usage information
-V, --version         output the version number
-c, --content [name]  set the property name for the files contents [__content]

Yaml front matter is a wrapper to js-yaml. Therefore yaml front matter supports the same api as js-yaml plus a more so pay js-yaml a visit. You can directly access js-yaml in the command line by running $ js-yaml (note this will run the actual js-yaml parser and will not be able to parse input intended for yaml-front-matter).

var yamlFront = require('yaml-front-matter')
  , input = '---\npost: title one\n';
    input += 'anArray:\n - one\n - two\n';
    input += 'subObject:\n prop1: cool\n prop2: two';
    input += '\nreg: !!js/regexp /pattern/gim';
    input += '\nfun: !!js/function function() {  }\n---\n';
    input += 'content\nmore';
var results = yamlFront.loadFront(input);

the above will produce the following in the console.

{ post: 'title one',
  anArray: [ 'one''two' ],
  subObject: { obj1: 'cool', obj2: 'two' },
  reg: /pattern/gim,
  fun: [Function],
  __content: '\ncontent\nmore' }

The front matter is optional:

// somefile.ext
Hello World!

// another location
var frontMatter = require('yaml-front-matter')
  , results = frontMatter.loadFront('somefile.ext', 'myContent');

Will produce

{ myContent: "Hello World!" }

Content all together is optional

// will produce { __content: '' }

NOTE: This behavior differs from previos versions as previous versions returned undefined when the input did not contain yaml front matter.

NOTE: The --- are required to denote the start and end of front matter. There must be a newline after the opening --- and a newline preceding the closing ---.

  • Now supports parsing JSON front matter.
  • A newline must come after the opening --- and precede the closing ---.
  • Front matter is optional.
  • Browser API

Then run

$ npm install --dev && npm install mocha -g && npm test