text-metadata-parser
Text Metadata Parser was created for two reasons: to parse content and content metadata out of a single string or file, and to try to come up with the most boring library name ever!
Last updated 5 years ago by tehshrike .
Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install text-metadata-parser 
SYNC missed versions from official npm registry.

Build Status

Text Metadata Parser is a Jekyll Front Matter-like parser.

It was created in 2013, before Jekyll was widespread enough for me to just use Front Matter instead.

At this point, text-metadata-parser uses js-yaml internally, meaning that it supports all the YAML features that the other library supports.

In addition, for backwards compatibility, it also allows you to use a much looser syntax. If you do not enclose the metadata section in --- three dashes, they will be inferred. When parsing metadata in "loose mode", key: value sections that have colons in the value string will be allowed, even if they are not enclosed in double quotes.

API

parse(text, [options])

  • text is a string that contains a metadata section at the top
  • options is an optional object with options to create a weak-type-wizard to cast the metadata to whatever types you like

Install

npm install text-metadata-parser

Example

Say you wrote some random blog posts in a drunken stupor one night, and saved them all in text files in this format:

title:    Last will and testament
date:	    2019-09-13
attn:     Secret Family
lovers:   3
deceased: true

I leave everything to Janet.

Except my boots.  Those are *mine.*

Parse:

var my_drunken_ramblings = parse(that_big_string_from_earlier_in_the_example, {
	number: ['lovers', 'bagels'],
	string: ['title', 'attn'],
	date: ['date'],
	boolean: 'deceased',
	default: { lovers: 5, bagels: 3}
});

PROFIT!!!11

> my_drunken_ramblings.metadata.title
'Last will and testament'

> my_drunken_ramblings.metadata.date
Fri Sep 13 2019 00:00:00 GMT+0000 (Coordinated Universal Time)

> my_drunken_ramblings.metadata.lovers
3

> my_drunken_ramblings.metadata.bagels
3

> my_drunken_ramblings.content
'I leave everything to Janet.\n\nExcept my boots.  Those are *mine.*'

License

WTFPL

Current Tags

  • 3.0.4                                ...           latest (5 years ago)

10 Versions

  • 3.0.4                                ...           5 years ago
  • 3.0.3                                ...           5 years ago
  • 3.0.2                                ...           5 years ago
  • 3.0.1                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.0.3                                ...           6 years ago
  • 2.0.1                                ...           7 years ago
  • 2.0.0                                ...           7 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (2)
Dev Dependencies (1)

Copyright 2014 - 2016 © taobao.org |