Skip to content
Parse Markdown file's metadata from its content
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
source
test
.all-contributorsrc
.eslintignore
.eslintrc.js
.gitignore
.prettierrc.js
.travis.yml
AUTHORS
CHANGELOG.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
README.md
ava.config.js
babel.config.js
husky.config.js
lint-staged.config.js
nyc.config.js
package.json
rollup.config.js
yarn.lock

README.md

parseMD

All Contributors npm version npm downloads Build Status Coverage Status Maintainability

This library exists as a way to pass a markdown file's content and have its metadata and markdown returned as an object containing metadata and content keys.

Note that it is not trying to do anything but solve the markdown metadata vs. content parsing problem and is not parsing the markdown body, itself. You can use something like marked for that.

What It Does

For example,

---
title: This is a test
description: Once upon a time...
---
# Title of my great post
Lorem ipsum dolor...

would be parsed as

{
  metadata: {
    title: "This is a test",
    description: "Once upon a time..."
  },
  content: "# Title of my great post\nLorem ipsum dolor..."
}

Note: This tool expects that your Markdown metadata has --- boundaries, as shown above.

Usage

Node/CommonJS

First, install it via NPM and save it to your project:

$ npm i parse-md

Import it where you need it:

import parseMD from 'parse-md'

or if you are using commonjs,

const parseMD = require('parse-md').default

and then pass it a Markdown file's content. Here is one method by which you might do so:

const fileContents = fs.readFileSync('posts/first.md', 'utf8')
const { metadata, content } = parseMD(fileContents)

console.log(metadata); // { title: 'Great first post', description: 'This is my first great post. Rawr' }
console.log(content); // "# My first post..."

Links

Contributors

Thanks goes to these wonderful people (emoji key):


Robert Pearce

💻 📖 💡 🤔 ⚠️

Justin Chan

🐛

This project follows the all-contributors specification. Contributions of any kind welcome!

You can’t perform that action at this time.