{"id":13615581,"url":"https://github.com/dworthen/js-yaml-front-matter","last_synced_at":"2025-04-12T21:31:10.485Z","repository":{"id":15160873,"uuid":"17888512","full_name":"dworthen/js-yaml-front-matter","owner":"dworthen","description":"Parses yaml or json from the beginning of a string or file","archived":false,"fork":false,"pushed_at":"2023-01-06T17:15:27.000Z","size":1559,"stargazers_count":193,"open_issues_count":20,"forks_count":17,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-04T01:08:26.201Z","etag":null,"topics":["js-yaml","json","yaml","yaml-frontmatter"],"latest_commit_sha":null,"homepage":"https://dworthen.github.io/js-yaml-front-matter/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dworthen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-19T01:37:38.000Z","updated_at":"2025-02-19T10:44:52.000Z","dependencies_parsed_at":"2023-01-13T18:17:06.108Z","dependency_job_id":null,"html_url":"https://github.com/dworthen/js-yaml-front-matter","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dworthen%2Fjs-yaml-front-matter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dworthen%2Fjs-yaml-front-matter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dworthen%2Fjs-yaml-front-matter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dworthen%2Fjs-yaml-front-matter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dworthen","download_url":"https://codeload.github.com/dworthen/js-yaml-front-matter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248634906,"owners_count":21137140,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["js-yaml","json","yaml","yaml-frontmatter"],"created_at":"2024-08-01T20:01:15.389Z","updated_at":"2025-04-12T21:31:10.441Z","avatar_url":"https://github.com/dworthen.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Yaml Front Matter\n\nParses yaml or json at the front of a string. Places the parsed content, plus the rest of the string content, into an object literal.\n\n[Online Demo](https://dworthen.github.io/js-yaml-front-matter/).\n\n#### Breaking Changes\n\nThis readme is for the 4.x release, which introduces breaking changes. View the [changelog](CHANGELOG.md) for more information.\n\n[3.x readme](https://github.com/dworthen/js-yaml-front-matter/tree/v3.4.0)\n\n## Example\n\nThis\n\n```yaml\n---\nname: Derek Worthen\nage: 127\ncontact:\n  email: email@domain.com\n  address: some location\npets:\n  - cat\n  - dog\n  - bat\nmatch: !!js/regexp /pattern/gim\nrun: !!js/function function() { }\n---\nSome Other content\n```\n\n```js\nvar fs = require('fs');\nvar yamlFront = require('yaml-front-matter');\n\nfs.readFile('./some/file.txt', 'utf8', function(fileContents) {\n    console.log(yamlFront.loadFront(fileContents));\n});\n\n```\n\noutputs\n\n```js\n{ \n    name: 'Derek Worthen',\n    age: 127,\n    contact: { email: 'email@domain.com', address: 'some location' },\n    pets: [ 'cat', 'dog', 'bat' ],\n    match: /pattern/gim,\n    run: [Function],\n    __content: '\\nSome Other Content' \n}\n```\n\nMay also use JSON\n\n```json\n---\n{\n    \"name\": \"Derek Worthen\",\n    \"age\": \"young\",\n    \"anArray\": [\"one\",\"two\"],\n    \"subObj\":{\"field1\": \"one\"}\n}\n---\nSome content\n```\n\n\u003e __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 `---`.\n\n## Install\n\n### npm\n\n```shell\n$ npm install yaml-front-matter\n```\n\nUse the `-g` flag if you plan on using the command line tool.\n\n```shell\n$ npm install yaml-front-matter -g\n```\n\n### Node or client with module bundler (webpack or browsify)\n\n```js\nvar yamlFront = require('yaml-front-matter');\n```\n    \n### Browser Bundle\n\nThe [dist/yamlFront.js](dist/yamlFront.js) client script will expose the yaml-front-matter library as a global, `yamlFront`. The client script for [js-yaml](https://github.com/nodeca/js-yaml) is also required. May need to load espirma for some use cases. See [js-yaml](https://github.com/nodeca/js-yaml) for more information.\n\n```html\n\u003cscript src=\"https://unpkg.com/js-yaml@3.10.0/dist/js-yaml.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"yamlFront.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  // parse front matter with yamlFront.loadFront(String);\n\u003c/script\u003e\n```\n\n\u003e **Note**: yaml-front-matter is delivered as a umd package so it should work within commonjs, amd and browser (as a global) environments.\n\n## Running Browser Example\n\n```shell\n$ npm install --dev \u0026\u0026 npm start\n```\n\nThen visit `localhost:8080`.\n\n## Building from source\n\nOutputs build files to `dist/`.\n\n```shell\n$ npm install --dev \u0026\u0026 npm run build\n```\n\n## Running Tests\n\n```shell\nnpm install --dev \u0026\u0026 npm test\n```\n\n## Command Line\n\n```shell\nUsage: yaml-front-matter [options] \u003cyaml-front-matter content\u003e\n\nOptions:\n\n-h, --help            output usage information\n-v, --version         output the version number\n-c, --content [name]  set the property name for the files contents [__content]\n--pretty              formats json output with spaces. \n```\n\n\u003e **Note** The cli uses `safeLoadFront` and therefore will not parse yaml containing regexps, functions or undefined values.\n\n### Example\n\n```shell\n# Piping content from one file, through yaml parser and into another file\ncat ./some/file.txt | yaml-front-matter \u003e output.txt\n```\n\n## JS-YAML\n\nYaml front matter wraps [js-yaml](https://github.com/nodeca/js-yaml) to support parsing yaml front-matter.\n\n## API\n\n### loadFront(string, [options])\n\n```js\nvar input = [\n        '---\\npost: title one\\n',\n        'anArray:\\n - one\\n - two\\n',\n        'subObject:\\n prop1: cool\\n prop2: two',\n        '\\nreg: !!js/regexp /pattern/gim',\n        '\\nfun: !!js/function function() {  }\\n---\\n',\n        'content\\nmore'\n    ].join('');\n\nvar results = yamlFront.loadFront(input);\nconsole.log(results);\n```\n\noutputs\n\n```shell\n{ post: 'title one',\n  anArray: [ 'one', 'two' ],\n  subObject: { obj1: 'cool', obj2: 'two' },\n  reg: /pattern/gim,\n  fun: [Function],\n  __content: '\\ncontent\\nmore' }\n```\n\nFront-matter is optional.\n\n```js\nyamlFront.loadFront('Hello World');\n// =\u003e { __content: \"Hello World!\" }\n```\n\nContent is optional\n\n```js\nyamlFront.loadFront('');\n// =\u003e { __content: '' }\n```\n\n### safeLoadFront(string, [options])\n\nSame api as loadFront except it does not support regexps, functions or undefined. See [js-yaml](https://github.com/nodeca/js-yaml) for more information.\n\n### Options\n\nThe options object supports the same options available to [js-yaml](https://github.com/nodeca/js-yaml) and adds support for an additional key.\n\n- `options.contentKeyName`: Specify the object key where to store content not parsed by yaml-front-matter. defaults to `__content`.\n\n```js\nyamlFront.loadFront('Hello World', {\n    contentKeyName: 'fileContents' \n});\n// =\u003e { fileContents: \"Hello World\" }\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdworthen%2Fjs-yaml-front-matter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdworthen%2Fjs-yaml-front-matter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdworthen%2Fjs-yaml-front-matter/lists"}