gray-matter-from-file

Extracts only the front matter from a file stream, and passes to the well known gray-matter library for parsing.


Keywords
grey, gray, greymatter, graymatter, frontmatter, front, matter, from, file, stream, async, markdown, parser, parse
License
GPL-3.0
Install
npm install gray-matter-from-file@1.0.6

Documentation

Gray Matter From File

Build Status

Reads the front matter portion of a markdown file (denoted by --- above and below the front matter contents) and passes this to Gray Matter for parsing.

This only reads the front matter of a file and does not load the remaining contents into memory, this results in fast and asynchronous parsing.

Usage

A Markdown file with some front matter and contents underneath.

---
hello: world
---

The remaining contents of this markdown file would be ignored, this module simply retrieves the front matter above.

Use grayMatterFromFile to obtain this front matter, and parse it via the gray-matter module.

const path = require('path');
const grayMatterFromFile = require('gray-matter-from-file').default;
// import grayMatterFromFile from 'gray-matter-from-file';

const filepath = path.resolve(__dirname, 'my-file.md');

grayMatterFromFile(filepath)
    .then(grayMatter => console.log(grayMatter)) // { hello: 'world' }
    .catch(error => console.log(error))

Why Use It

This module uses fs.readFile and readline to stream the front matter from a markdown file line by line, before passing the result to Gray Matter and returning the parsed object.

This ensures that:

  • There are no synchronous file reads to obtain the front matter
  • Only the front matter is ever read into memory, the rest of the file is ignored
  • This should be performant for a large number of front matter reads across many files, perhaps useful for indexing or processing a large set of markdown files in a static site generator

Support

This has only been transpiled for Node 8 (and above) in order to help enourage the community to be on the latest, more secure and more performant versions of Node. I may remove support for Node 8 when it is no longer LTS.