Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/blakewilson/markdown-posts

A simple API to transform a directory of markdown files with frontmatter into data
https://github.com/blakewilson/markdown-posts

markdown matter next remark

Last synced: 15 days ago
JSON representation

A simple API to transform a directory of markdown files with frontmatter into data

Awesome Lists containing this project

README

        

# Markdown Posts

A simple API to transform a directory of markdown files with frontmatter into data.

## Usage

### Installation

Install the package:

```bash
npm install markdown-posts
```

### Usage

By default, `markdown-posts` looks for all markdown files in the `posts` directory.

Below is a basic example in Next.js:

List all posts on home page:

**`pages/index.tsx`**

```tsx
import { getAllPosts } from "markdown-posts";

export default function Home({ posts }) {
return (


    {posts.map((post, index) => (
  • {post.title}

  • ))}

);
}

export async function getStaticProps() {
const posts = await getAllPosts(["title", "date", "slug"]);

return {
props: {
posts,
},
};
}
```

Display a single post:

**`pages/[slug].tsx`**

```tsx
import { getPostBySlug, getAllPosts } from "markdown-posts";

export default function SinglePost({ post }) {
return (

{post.title}



);
}

export async function getStaticProps({ params }) {
const { slug } = params;
const post = await getPostBySlug(slug, ["title", "date", "slug"]);

return {
props: {
post,
},
};
}

export async function getStaticPaths() {
const posts = await getAllPosts(["slug"]);

return {
paths: posts.map((post) => {
return {
params: {
slug: post.slug,
},
};
}),
fallback: false,
};
}
```

## License

This project is licensed under the [MIT license](https://github.com/blakewilson/markdown-posts/blob/master/LICENSE)