Preliminaries — a front matter parser for Node.js

· 142 words · 1 minute read

Preliminaries is a small Node.js library for parsing front matter in Markdown documents.

You can easily parse YAML, JSON and TOML front matter, with custom delimiters if needed:

var preliminaries = require('preliminaries')(true);

// `preliminaries.parse` returns an object `{data: {}, content: ''}` with the front
// matter data and content
preliminaries.parse('---json\n{\nname: Joseph\n}\n---\nContent');
preliminaries.parse('---\nname: Joseph\n---\nContent');
preliminaries.parse('---yaml\nname: Joseph\n---\nContent');
preliminaries.parse('+++\nname = "Joseph"\n+++\nContent');
preliminaries.parse('~~~\nname: Joseph\n~~~\nContent', {delims: '~~~', lang: 'yaml'});

You can also stringify a JavaScript object and a content string back out:

preliminaries.stringify('Content', {name: 'Joseph'}, {lang: 'yaml'});
preliminaries.stringify('Content', {name: 'Joseph'}, {lang: 'toml'});
preliminaries.stringify('Content', {name: 'Joseph'}, {lang: 'json'});

Or test if a string contains front matter:

preliminaries.test('{\n"abc": "xyz"\n}');
preliminaries.test('---\nabc: xyz\n---');
preliminaries.test('+++\nabc = "xyz"\n+++');
preliminaries.test('~~~\nabc = "xyz"\n~~~');

The core preliminaries library has zero dependencies, all supported formats except JSON are optional and are provided via plugins.

Install preliminaries using npm or check it out on GitHub.