Giter Site home page Giter Site logo

rpearce / parse-md Goto Github PK

View Code? Open in Web Editor NEW
36.0 3.0 3.0 866 KB

Parse Markdown file's metadata from its content

Home Page: https://github.com/rpearce/parse-md

License: MIT License

JavaScript 39.63% TypeScript 60.37%
markdown-metadata markdown markdown-parser markdown-yaml parsing metadata

parse-md's Introduction

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.