Coder Social home page Coder Social logo

dworthen / js-yaml-front-matter Goto Github PK

View Code? Open in Web Editor NEW
192.0 4.0 17.0 1.52 MB

Parses yaml or json from the beginning of a string or file

Home Page: https://dworthen.github.io/js-yaml-front-matter/

License: MIT License

JavaScript 100.00%
js-yaml json yaml yaml-frontmatter

js-yaml-front-matter's Introduction

Yaml Front Matter

Parses yaml or json at the front of a string. Places the parsed content, plus the rest of the string content, into an object literal.

Online Demo.

Breaking Changes

This readme is for the 4.x release, which introduces breaking changes. View the changelog for more information.

3.x readme

Example

This

---
name: Derek Worthen
age: 127
contact:
  email: [email protected]
  address: some location
pets:
  - cat
  - dog
  - bat
match: !!js/regexp /pattern/gim
run: !!js/function function() { }
---
Some Other content
var fs = require('fs');
var yamlFront = require('yaml-front-matter');

fs.readFile('./some/file.txt', 'utf8', function(fileContents) {
    console.log(yamlFront.loadFront(fileContents));
});

outputs

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

May also use JSON

---
{
    "name": "Derek Worthen",
    "age": "young",
    "anArray": ["one","two"],
    "subObj":{"field1": "one"}
}
---
Some content

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

Install

npm

$ npm install yaml-front-matter

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

$ npm install yaml-front-matter -g

Node or client with module bundler (webpack or browsify)

var yamlFront = require('yaml-front-matter');

Browser Bundle

The dist/yamlFront.js client script will expose the yaml-front-matter library as a global, yamlFront. The client script for js-yaml is also required. May need to load espirma for some use cases. See js-yaml for more information.

<script src="https://unpkg.com/[email protected]/dist/js-yaml.js"></script>
<script src="yamlFront.js"></script>
<script>
  // parse front matter with yamlFront.loadFront(String);
</script>

Note: yaml-front-matter is delivered as a umd package so it should work within commonjs, amd and browser (as a global) environments.

Running Browser Example

$ npm install --dev && npm start

Then visit localhost:8080.

Building from source

Outputs build files to dist/.

$ npm install --dev && npm run build

Running Tests

npm install --dev && npm test

Command Line

Usage: yaml-front-matter [options] <yaml-front-matter content>

Options:

-h, --help            output usage information
-v, --version         output the version number
-c, --content [name]  set the property name for the files contents [__content]
--pretty              formats json output with spaces. 

Note The cli uses safeLoadFront and therefore will not parse yaml containing regexps, functions or undefined values.

Example

# Piping content from one file, through yaml parser and into another file
cat ./some/file.txt | yaml-front-matter > output.txt

JS-YAML

Yaml front matter wraps js-yaml to support parsing yaml front-matter.

API

loadFront(string, [options])

var input = [
        '---\npost: title one\n',
        'anArray:\n - one\n - two\n',
        'subObject:\n prop1: cool\n prop2: two',
        '\nreg: !!js/regexp /pattern/gim',
        '\nfun: !!js/function function() {  }\n---\n',
        'content\nmore'
    ].join('');

var results = yamlFront.loadFront(input);
console.log(results);

outputs

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

Front-matter is optional.

yamlFront.loadFront('Hello World');
// => { __content: "Hello World!" }

Content is optional

yamlFront.loadFront('');
// => { __content: '' }

safeLoadFront(string, [options])

Same api as loadFront except it does not support regexps, functions or undefined. See js-yaml for more information.

Options

The options object supports the same options available to js-yaml and adds support for an additional key.

  • options.contentKeyName: Specify the object key where to store content not parsed by yaml-front-matter. defaults to __content.
yamlFront.loadFront('Hello World', {
    contentKeyName: 'fileContents' 
});
// => { fileContents: "Hello World" }

js-yaml-front-matter's People

Contributors

dworthen avatar jshanley avatar lzilioli avatar mikl avatar thepont avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

js-yaml-front-matter's Issues

Why use [\w\W] instead of . in regex-parser?

When browsing the source code I noticed in the regex used for extracting the front matter that you use [\w\W]:

let re = /^(-{3}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{3})?([\w\W]*)*/

Why do you do that, wouldn't . be equal? I.e.:

let re = /^(-{3}(?:\n|\r)(.+?)(?:\n|\r)-{3})?(.*)*/ 

This is a honest question, I just want to understand :-)

Cannot load in browser with require.js

Attempting to load js-yaml-front-matter in the browser with require.js results in Uncaught TypeError: Cannot set property 'parse' of undefined.

js-yaml-front-client.min.js

image

js-yaml-front-client.js

image

docs is error

image

fs.readFile('./some/file.txt', 'utf8', function(fileContents) {
console.log(yamlFront.loadFront(fileContents));
});
===>
fs.readFile('./some/file.txt', 'utf8', function(_, fileContents) {
console.log(yamlFront.loadFront(fileContents));
});

Cannot install yaml-front-matter

$ npm install --save yaml-front-matter
npm ERR! Error: ENOENT, chmod '/Users/glavin/Documents/Project Dev/atom-beautify/node_modules/yaml-front-matter/node_modules/js-yaml/bin/js-yaml.js'
npm ERR! If you need help, you may report this *entire* log,
npm ERR! including the npm and node versions, at:
npm ERR!     <http://github.com/npm/npm/issues>

npm ERR! System Darwin 14.0.0
npm ERR! command "/Users/glavin/.nvm/v0.10.32/bin/node" "/Users/glavin/.nvm/v0.10.32/bin/npm" "install" "--save" "yaml-front-matter"
npm ERR! cwd /Users/glavin/Documents/Project Dev/atom-beautify
npm ERR! node -v v0.10.32
npm ERR! npm -v 1.4.28
npm ERR! path /Users/glavin/Documents/Project Dev/atom-beautify/node_modules/yaml-front-matter/node_modules/js-yaml/bin/js-yaml.js
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! error rolling back Error: Refusing to delete: /Users/glavin/Documents/Project Dev/atom-beautify/node_modules/.bin/js-yaml not in /Users/glavin/Documents/Project Dev/atom-beautify/node_modules/yaml-front-matter
npm ERR! error rolling back     at clobberFail (/Users/glavin/.nvm/v0.10.32/lib/node_modules/npm/lib/utils/gently-rm.js:57:12)
npm ERR! error rolling back     at next (/Users/glavin/.nvm/v0.10.32/lib/node_modules/npm/lib/utils/gently-rm.js:43:14)
npm ERR! error rolling back     at /Users/glavin/.nvm/v0.10.32/lib/node_modules/npm/lib/utils/gently-rm.js:52:12
npm ERR! error rolling back     at Object.oncomplete (evalmachine.<anonymous>:107:15)
npm ERR! error rolling back  { [Error: Refusing to delete: /Users/glavin/Documents/Project Dev/atom-beautify/node_modules/.bin/js-yaml not in /Users/glavin/Documents/Project Dev/atom-beautify/node_modules/yaml-front-matter]
npm ERR! error rolling back   code: 'EEXIST',
npm ERR! error rolling back   path: '/Users/glavin/Documents/Project Dev/atom-beautify/node_modules/.bin/js-yaml' }
npm ERR! not ok code 0

I think https://github.com/dworthen/js-yaml-front-matter/blob/master/package.json#L25 is the culprit and should be removed.

I already have js-yaml as a dependency, so this conflict may be causing this problem in my case.

Make content optional

Sometimes you may just want the frontmatter data, and not the content of the file. You should be able to disable the content field.

Define __content from API

Is there a way to define the __content name when using the API ?

I see there's a -c that can be used from the command line.

Thanks.

Parse json into head matter?

Hello!

Is there a way to parse json (like the output you get from parsing a head matter file) back to head matter? Let's say I would like to add some properties to an object and overwrite it with the added values.

Thanks!

Example is wrong

yaml-front-matter is currently incorrectly parsing the __content portion with a superfluous newline, i.e. '\nSome Other content'

Add types

Would you mind add types for this project? I'm willing to create a pr for this.

Document auto-detected formats like date

Hello!

It is not yet documented that dates are automatically turned into a Date object.
Are there other formats that are detected and turned into respective objects?

Thanks in advance :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.