Skip to content

rpearce/parse-md

Repository files navigation

parseMD

All Contributors npm version npm downloads bundlephobia size

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

Installation:

npm i parse-md

Import it where you need it, and then pass it a Markdown file's content:

import fs from 'fs'
import parseMD from 'parse-md'

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

Note about CommonJS

If you need to CommonJS module support, use version 2.x, and require it like this:

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

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Robert Pearce

💻 📖 💡 🤔 ⚠️

Justin Chan

🐛

Alex Gherghisan

💻 🐛 🤔

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