gray-matter
gray-matter is a library for parsing front-matter from strings or files, extracting the metadata section (typically YAML, JSON, or TOML) from the beginning of c…
Installation
npm install gray-matter
yarn add gray-matter
pnpm add gray-matter
Import
import matter from 'gray-matter';
Quick Example
import matter from 'gray-matter';
const file = `---
title: Hello World
date: 2024-01-15
tags: [javascript, web]
---
# Content here`;
const { data, content } = matter(file);
console.log(data.title); // 'Hello World'
console.log(content); // '# Content here'About gray-matter
gray-matter is a library for parsing front-matter from strings or files, extracting the metadata section (typically YAML, JSON, or TOML) from the beginning of content files. Front-matter is a convention used in static site generators, blog platforms, and content management systems where metadata like title, date, author, tags, and custom properties are embedded at the top of Markdown or other content files between delimiter lines (--- for YAML). gray-matter parses this metadata into a JavaScript object and provides the remaining content separately, making it easy to process content files with embedded configuration. The library supports YAML (default), JSON, TOML, and CoffeeScript front-matter formats, with the ability to define custom parsers. gray-matter handles various edge cases including empty front-matter, excerpts (content summaries delimited by a separator), and files without any front-matter. The library is used internally by popular tools including Gatsby, Astro, Next.js MDX plugin, Jekyll, Metalsmith, and VuePress for processing content files. gray-matter works with both strings and file paths and provides both sync and async APIs for reading files.
Quick Facts
| Package | gray-matter |
| Category | Utility |
| Weekly Downloads | 4M+ |
| License | MIT |
| Install | npm install gray-matter |
Related Packages
Marked is a fast, low-level Markdown parser and compiler built for speed, providing a simple API for…
markdown-it is a fast and extensible Markdown parser that converts Markdown text to HTML with 100% C…
Astro is a modern web framework designed for building content-driven websites like blogs, documentat…
Next.js is the leading React framework for building full-stack web applications, developed and maint…
Gatsby is a React-based static site generator and framework that leverages GraphQL for data manageme…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.