gray-matter
Parser extracting YAML/JSON front matter from markdown and text files.
2. Maintained
github.com/jonschlinkert/gray-matter3. Used by agents' projects
Dependency in 98 public GitHub projects set up for AI coding agents that we scanned (1.6%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
From the package registry and GitHub, checked daily. Tap a verdict to see its source.How we checkedReport an error
What gray-matter does
gray-matter does one small job: it splits a file into a <code>data</code> object and a <code>content</code> string, without regex, and can write the data back out as front matter.
gray-matter popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How gray-matter is used
- gray-matter is a dependency in 98 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (2.0% of the 4,867 JavaScript/TypeScript ones), #5 of 149 in markdown, editors and content.
- Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 247; those are left out of every share on this page.
- Projects that use it most often also use Next.js (56.1%), remark-gfm (40.8%), TypeScript (94.9%).
By coding agent
Often used with
gray-matter in practice
Strengths
The main function takes a string and returns an object. Most code only needs <code>data</code> and <code>content</code> from it.
The maintainers chose not to parse with regex. They list fenced code blocks that contain sample YAML front matter as a case that other parsers get wrong.
<code>matter.stringify</code> turns an object back into a front matter block and puts it in front of the content. The README notes this is useful for linting and for updating properties.
If you write <code>---toml</code> as the first delimiter, gray-matter uses the TOML engine for that file without any options being set.
Watch out for
Writing TOML or other formats back requires an engine that supplies its own <code>stringify</code> method.
With <code>excerpt: true</code>, the excerpt ends at the next <code>---</code>. The excerpt also stays inside <code>content</code>. Set <code>excerpt_separator</code> if your body uses <code>---</code> for other things.
The file helper blocks while it reads from disk. For async code, read the file yourself and pass the string to <code>matter()</code>.
Parse a string with front matter
const matter = require('gray-matter');
console.log(matter('---\ntitle: Home\n---\nOther stuff'));
//=> { data: { title: 'Home'}, content: 'Other stuff' }More about gray-matter
Funding
- GitHub Sponsors: https://github.com/jonschlinkert
- 16 GitHub sponsors
Packages around it
- next: React framework
- remark-gfm: GitHub Markdown for remark
- typescript: Typed JavaScript
- react: UI library
- clsx: className helper
- react-markdown: Markdown to React
More in Markdown, editors and content
Alternatives
gray-matter This page
Get started
Install it.
npm install gray-matterRules for your agent
## gray-matter
- Import with `const matter = require('gray-matter')`, or in TypeScript with `import * as matter from 'gray-matter'`.
- Read metadata from `file.data` and the body from `file.content`. `orig`, `language`, `matter` and `stringify` are non-enumerable debug properties.
- Use `delimiters`, `language` and `engines`. Do not use the deprecated `delims`, `lang` and `parsers`.
- To update metadata, call `matter.stringify(content, data)`. Only YAML and JSON stringify by default; other formats need an engine with a `stringify` method.
- `matter.read(filepath)` is synchronous. In async code, read the file yourself and pass the string to `matter()`.
- When using `excerpt: true`, set `excerpt_separator` if the body may contain `---`. The excerpt is not removed from `content`.
- Check `file.isEmpty` or call `matter.test(str)` before relying on `data` fields.