matter
Flexible front matter parser.
matter;// => {foo: 'bar'} matteryaml; matter;// => {foo: 'bar'} matterjson;
What can it do?
- Parse front matters embedded in comments (or any configurable tokens)
- Allow custom parsers to be plugged in
- Keep the parsing as configurable as possible
API
matter.yaml(string, [options])
Alias to matter.parse.yaml
.
console;// => {foo: 'bar'}
matter.yaml.inFile(filepath, [options], callback)
Alias to matter.parse.yaml.inFile
.
matteryaml;
matter.json(string, [options])
Alias to matter.parse.json
.
console;// => {foo: 'bar'}
matter.json.inFile(filepath, [options], callback)
Alias to matter.parse.json.inFile
.
matterjson;
matter.parse.<parserName>(string, [options])
string
: A string containing the front matter to be parsedoptions
: An object of optionsignore
: A regex or string representing tokens to be ignored when extracting the front matter. Used to extract the front matter. The tokens ignored by default are//
,/*
,*/
,#
.indicator
(default=/---/
): A regex or string to use as the front matter head and tail indicatorsindicators
: An object of options to use for the front matter head and tail indicators:head
: A regex or string to use as the front matter's head indicator. Defaults toindicator
's value.tail
: A regex or string to use as the front matter's tail indicator. Defaults toindicators
's value
Parses a string containing a front matter using the parser registered as parserName
.
console;// => {foo: 'bar'}
matter.parse.<parserName>.inFile(filepath, [options], callback)
filename
: A relative or absolute path to the file with the front matter to be parsedoptions
: An object of options:encoding
(default='utf8'
): Theencoding
to be used when reading the fileignore
: A regex or string representing tokens to be ignored when extracting the front matter. Used to extract the front matter. The tokens ignored by default are//
,/*
,*/
,#
.indicator
(default=/---/
): A regex or string to use as the front matter head and tail indicatorsindicators
: An object of options to use for the front matter head and tail indicators:head
: A regex or string to use as the front matter's head indicator. Defaults toindicator
's value.tail
: A regex or string to use as the front matter's tail indicator. Defaults toindicators
's value
- [
fs.readFile
options]
callback(err, metadata, data)
: A function to be called when the file has been read and the front matter has been parsed.err
: An error object for an error which may have occured while reading and parsing the file.null
if no error occured.metadata
: An object containing the extracted and parsed front matter datadata
: A string of the actual data contained in the file
Parses a file containing a front matter using the parser registered as parserName
.
matterparseyaml;
matter.parsers.register(name, fn)
name
: The name of the parser. Once registered, the parser becomes a property ofmatter.parse
.fn(data)
: The function to convert the extracted front matter string into a JSON object.data
: A string containing the extracted front matter data.matter
tries clean the data up as best it can without breaking the format.
Registers a new front matter parser.
matterparsers; console;// => {foo: 'bar', baz: 'qux'});
matter.parsers.unregister(name)
name
: The name of the parser to unregister.
Unregisters a parser.
matterparsers;
matter.parsers.defaults(overrides)
overrides
: An object containing the new defaults.matter
uses the following defaults:encoding
(default='utf8'
): Theencoding
to be used when reading filesignore
: A regex or string representing tokens to be ignored when extracting the front matter. Used to extract the front matter. The tokens ignored by default are//
,/*
,*/
,#
.indicator
(default=/---/
): A regex or string to use as the front matter head and tail indicatorsindicators
: An object of options to use for the front matter head and tail indicators:head
: A regex or string to use as the front matter's head indicator. Defaults toindicator
's value.tail
: A regex or string to use as the front matter's tail indicator. Defaults toindicators
's value
Sets new defaults for parsing.
matterparsers;
matter.parsers.defaults()
Returns a shallow copy of the parsing defaults.
console;