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!

parse-md's People

Contributors

alexghr avatar dependabot[bot] avatar rpearce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

parse-md's Issues

Can't use properly inside a node ESM package

Hi,

Thanks for the great library! I'm having some trouble importing it in a project uses native node ESM.

Importing the default export seems to return an object with a default key inside of it. Here's an example:

$ cat test.mjs
import parseMD from "parse-md";
console.log(parseMD);
$ node test.mjs
{ default: [Function: parseMD] }

Directly calling parseMD (as per docs) at this point would obviously crash node because parseMD is an object, not a function.

Having looked at package.json I think it's using an older/deprecated way of specifying ESM exports. According to Node 16's docs we should use the exports key in package.json now: https://nodejs.org/api/packages.html#exports

I'm happy to contribute a fix to this if you're willing to publish a new version to npm for it ๐Ÿ˜„

Should ParseMD support passing a filename and reading the contents?

Should reading a file be a thing, or should this be unopinionated?

If so, then how would that look?

/*
 * note that this API decision would conflict with
 * how it works currently where it just parses the string that is passed to it
 */
ParseMD('path/to/file.md')

Should it be able to accept multiple?

// array
ParseMD(['path/to/file.md', 'path/to/file2.md'])

// variadic
ParseMD('path/to/file.md', 'path/to/file2.md')

Or would passing it an object with named keys make more sense?

ParseMD({ file: 'path/to/file.md' }, { file2: 'path/to/file2.md' })

Deep thoughts...

TypeError: parseMD is not a function

i tried to use the example listed in the readme and i get TypeError: parseMD is not a function

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..."

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.