front-matter vs gray-matter vs remark-frontmatter vs yaml-front-matter
Front Matter Parsing Libraries
front-mattergray-matterremark-frontmatteryaml-front-matterSimilar Packages:
Front Matter Parsing Libraries

Front matter parsing libraries are tools used in web development to extract metadata from files, typically Markdown or text files. This metadata is often placed at the beginning of the file, enclosed by a specific delimiter (like --- for YAML). These libraries parse the front matter, convert it into a usable format (like JSON), and allow developers to access this data programmatically. This is particularly useful for static site generators, content management systems, and any application that needs to handle structured data within text files. front-matter is a simple and lightweight library for parsing front matter from files, while gray-matter is a more feature-rich alternative that supports multiple formats (YAML, JSON, TOML) and provides additional functionality like parsing the content separately. remark-frontmatter is a plugin for the remark ecosystem that specifically handles front matter in Markdown files, making it ideal for projects that use remark for processing Markdown. yaml-front-matter focuses on parsing YAML front matter and provides a straightforward API for extracting the metadata and content, making it a good choice for projects that specifically use YAML.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
front-matter4,783,882690-326 years agoMIT
gray-matter2,816,4244,309-685 years agoMIT
remark-frontmatter1,911,11930921.2 kB02 years agoMIT
yaml-front-matter72,395194-215 years agoMIT
Feature Comparison: front-matter vs gray-matter vs remark-frontmatter vs yaml-front-matter

Front Matter Format Support

  • front-matter:

    front-matter supports only YAML front matter, making it simple but limited in terms of format flexibility.

  • gray-matter:

    gray-matter supports multiple formats, including YAML, JSON, and TOML, providing much greater flexibility for different use cases.

  • remark-frontmatter:

    remark-frontmatter is designed to work with any front matter format, but it is primarily used in conjunction with remark for Markdown, allowing for flexibility in format choice.

  • yaml-front-matter:

    yaml-front-matter supports only YAML front matter, similar to front-matter, but is optimized for YAML parsing.

Content Parsing

  • front-matter:

    front-matter extracts front matter and content but does not provide advanced content parsing features.

  • gray-matter:

    gray-matter provides built-in support for extracting and parsing content separately from the front matter, making it more versatile.

  • remark-frontmatter:

    remark-frontmatter focuses on front matter parsing within Markdown files and works alongside remark for content processing, but does not parse content itself.

  • yaml-front-matter:

    yaml-front-matter extracts front matter and content but does not offer advanced content parsing capabilities.

Ease of Use

  • front-matter:

    front-matter is very easy to use with a simple API for parsing front matter from files.

  • gray-matter:

    gray-matter is also user-friendly, with a clear API that supports more complex use cases due to its additional features.

  • remark-frontmatter:

    remark-frontmatter is easy to use for those familiar with the remark ecosystem, but may have a learning curve for new users.

  • yaml-front-matter:

    yaml-front-matter offers a straightforward API for parsing YAML front matter, making it easy to integrate into projects.

Performance

  • front-matter:

    front-matter is lightweight and performs well for parsing front matter, with minimal overhead.

  • gray-matter:

    gray-matter is slightly heavier due to its additional features, but still performs efficiently for most use cases.

  • remark-frontmatter:

    remark-frontmatter performance depends on the remark processing pipeline, but it is generally efficient for handling front matter in Markdown.

  • yaml-front-matter:

    yaml-front-matter is lightweight and performs well for parsing YAML front matter.

Code Example

  • front-matter:

    Example of using front-matter

    const frontMatter = require('front-matter');
    const data = frontMatter('---
    title: My Post
    ---
    Hello, world!');
    console.log(data.attributes); // { title: 'My Post' }
    console.log(data.body); // 'Hello, world!'
    
  • gray-matter:

    Example of using gray-matter

    const grayMatter = require('gray-matter');
    const { data, content } = grayMatter('---
    title: My Post
    ---
    Hello, world!');
    console.log(data); // { title: 'My Post' }
    console.log(content); // 'Hello, world!'
    
  • remark-frontmatter:

    Example of using remark-frontmatter

    const remark = require('remark');
    const frontmatter = require('remark-frontmatter');
    const file = remark()
      .use(frontmatter)
      .processSync('---
    title: My Post
    ---
    Hello, world!');
    console.log(file.contents);
    
  • yaml-front-matter:

    Example of using yaml-front-matter

    const yamlFront = require('yaml-front-matter');
    const data = yamlFront.load('---
    title: My Post
    ---
    Hello, world!');
    console.log(data); // { title: 'My Post' }
    
How to Choose: front-matter vs gray-matter vs remark-frontmatter vs yaml-front-matter
  • front-matter:

    Choose front-matter if you need a lightweight and straightforward solution for parsing front matter from files. It is easy to use and integrates well into projects without adding much overhead.

  • gray-matter:

    Choose gray-matter if you require more flexibility and features, such as support for multiple front matter formats (YAML, JSON, TOML) and the ability to parse content separately. It is ideal for more complex applications where additional functionality is needed.

  • remark-frontmatter:

    Choose remark-frontmatter if you are already using the remark ecosystem for Markdown processing and need a plugin that specifically handles front matter. It is designed to work seamlessly with remark and is great for projects that leverage this framework.

  • yaml-front-matter:

    Choose yaml-front-matter if you are specifically working with YAML front matter and want a simple and efficient way to parse it. This package is focused on YAML, making it a good choice for projects that use YAML exclusively.

README for front-matter

front-matter

build coverage npm github

Sauce Test Status

Extract meta data (front-matter) from documents.

This modules does not do any IO (file loading or reading), only extracting and parsing front matter from strings.

This concept that was originally introduced to me through the jekyll blogging system and is pretty useful where you want to be able to easily add meta-data to content without the need for a database. YAML is extracted from the the top of a file between matching separators of "---" or "= yaml =". It will also extract YAML between a separator and "...".

Install

With npm do:

npm install front-matter

Example

So you have a file example.md:

---
title: Just hack'n
description: Nothing to see here
---

This is some text about some stuff that happened sometime ago

NOTE: As of front-matter@2.0.0 valid front matter is considered to have the starting separator on the first line.

Then you can do this:

var fs = require('fs')
  , fm = require('front-matter')

fs.readFile('./example.md', 'utf8', function(err, data){
  if (err) throw err

  var content = fm(data)

  console.log(content)
})

And end up with an object like this:

{
    attributes: {
        title: 'Just hack\'n',
        description: 'Nothing to see here'
    },
    body: 'This is some text about some stuff that happened sometime ago',
    bodyBegin: 6,
    frontmatter: 'title: Just hack\'n\ndescription: Nothing to see here'
}

Methods

var fm = require('front-matter')

fm(string, { allowUnsafe: false })

Return a content object with two properties:

  • content.attributes contains the extracted yaml attributes in json form
  • content.body contains the string contents below the yaml separators
  • content.bodyBegin contains the line number the body contents begins at
  • content.frontmatter contains the original yaml string contents

NOTE: By default fm() uses ys-yaml's safeLoad unless you set allowUnsafe in the options object to true.

fm.test(string)

Check if a string contains a front matter header of "---" or "= yaml =". Primarily used internally but is useful outside of the module.

Returns true or false

    fm.test(string) #=> true || false

Contributing

front-matter is an OPEN Source Project so please help out by reporting bugs or forking and opening pull requests when possible.

standard

All code is linted/formatted using standard style, any non-conforming code can be automatically formatted using the the fmt make task: make fmt.

Maintainers

Contributors

This module is awesome because of all the folks who submitted pull requests:

CHANGELOG

3.0.0

  • CI only tests Node versions >= v6 (drops v4, and v5)

LICENSE (MIT)

Copyright (c) Jason Campbell ("Author")

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.