🛠️
Utility4M+/wkMIT

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
npm install gray-matter
yarn
yarn add gray-matter
pnpm
pnpm add gray-matter

Import

ESM
import matter from 'gray-matter';

Quick Example

usage
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

Packagegray-matter
CategoryUtility
Weekly Downloads4M+
LicenseMIT
Installnpm install gray-matter

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.