Introduction to Gray Matter
Gray Matter is a robust library that allows developers to parse front matter from strings or files. It’s especially useful in static site generators, markdown previewers, and similar applications. This article will provide a comprehensive guide on various APIs offered by Gray Matter along with code snippets.
Installation
To begin using Gray Matter, you need to install it via npm or yarn:
npm install gray-matter
// or
yarn add gray-matter
Basic Usage
Here’s a simple example of using Gray Matter to parse the front matter from a string.
const matter = require('gray-matter');
const fileContents = `
---
title: Sample Post
date: 2023-10-15
description: This is a sample post.
---
# Hello World
This is the content of the post.`;
const parsed = matter(fileContents);
console.log(parsed);
API Examples
Gray Matter comes with a variety of useful methods and options:
1. Reading Front Matter from a File
const fs = require('fs');
const matter = require('gray-matter');
const file = fs.readFileSync('path/to/your/file.md', 'utf8');
const parsed = matter(file);
console.log(parsed);
2. Custom Delimiters
You can customize the delimiters if your front matter doesn’t use the default “—” markers:
const matter = require('gray-matter');
const fileContents = `
;;;
title: Custom Delimiters
date: 2023-10-15
;;;
This is the content.`;
const parsed = matter(fileContents, { delimiters: ';;;' });
console.log(parsed);
3. Stringify Method
The stringify
method is used to convert an object back into front matter and content:
const matter = require('gray-matter');
const parsed = {
data: {
title: 'Sample Post',
date: '2023-10-15'
},
content: 'This is the content of the post.'
};
const stringified = matter.stringify(parsed.content, parsed.data);
console.log(stringified);
4. Safe Load
Using the safeLoad
option, you can prevent the extraction of content outside of front matter:
const matter = require('gray-matter');
const fileContents = `
---
title: Safe Load
date: 2023-10-15
---
`;
const parsed = matter(fileContents, { safeLoad: true });
console.log(parsed.content); // should strip out the unsafe script tag
Example Application
Finally, let’s see a small example application that lists blog posts with their metadata.
const matter = require('gray-matter');
const fs = require('fs');
const path = require('path');
const getPostData = (fileName) => {
const filePath = path.join(__dirname, 'posts', fileName);
const fileContent = fs.readFileSync(filePath, 'utf8');
const parsed = matter(fileContent);
return { ...parsed.data, content: parsed.content };
};
const posts = fs.readdirSync(path.join(__dirname, 'posts'))
.filter(fileName => fileName.endsWith('.md'))
.map(fileName => getPostData(fileName));
console.log(posts);
Conclusion
Gray Matter simplifies the process of parsing front matter, making it a quintessential tool for developers working with markdown or any content-driven projects. By integrating its diverse API, you can enhance the readability and manageability of your project’s content.
Hash: fb3647d72d7157659fc8d687e79881cfe9fa2ca394a6ef10a36c700b2dc5bede